mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
Allow requests session customization, document use for automatic retries
* Refactor create_session() out of AnymailRequestsBackend * Document automatic retries with Requests
This commit is contained in:
@@ -25,17 +25,12 @@ class AnymailRequestsBackend(AnymailBaseBackend):
|
||||
return False # already exists
|
||||
|
||||
try:
|
||||
self.session = requests.Session()
|
||||
except requests.RequestException:
|
||||
self.session = self.create_session()
|
||||
except Exception:
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
else:
|
||||
self.session.headers["User-Agent"] = "django-anymail/{version}-{esp} {orig}".format(
|
||||
esp=self.esp_name.lower(), version=__version__,
|
||||
orig=self.session.headers.get("User-Agent", ""))
|
||||
if self.debug_api_requests:
|
||||
self.session.hooks['response'].append(self._dump_api_request)
|
||||
return True
|
||||
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
if self.session is None:
|
||||
@@ -57,6 +52,23 @@ class AnymailRequestsBackend(AnymailBaseBackend):
|
||||
"or you are incorrectly calling _send directly.)".format(class_name=class_name))
|
||||
return super()._send(message)
|
||||
|
||||
def create_session(self):
|
||||
"""Create the requests.Session object used by this instance of the backend.
|
||||
If subclassed, you can modify the Session returned from super() to give
|
||||
it your own configuration.
|
||||
|
||||
This must return an instance of requests.Session."""
|
||||
session = requests.Session()
|
||||
|
||||
session.headers["User-Agent"] = "django-anymail/{version}-{esp} {orig}".format(
|
||||
esp=self.esp_name.lower(), version=__version__,
|
||||
orig=session.headers.get("User-Agent", ""))
|
||||
|
||||
if self.debug_api_requests:
|
||||
session.hooks['response'].append(self._dump_api_request)
|
||||
|
||||
return session
|
||||
|
||||
def post_to_esp(self, payload, message):
|
||||
"""Post payload to ESP send API endpoint, and return the raw response.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user