From feee8b5c5ad6d24441ddf40e234b4ec491a513e6 Mon Sep 17 00:00:00 2001 From: medmunds Date: Fri, 11 Sep 2020 12:06:23 -0700 Subject: [PATCH] Cleanup: simplify requests backend raise_for_status Treat all 2xx (not just 200) as success in base AnymailRequestsBackend.raise_for_status, eliminating some unnecessary subclass overrides. --- anymail/backends/base_requests.py | 7 ++++--- anymail/backends/sendgrid.py | 7 +------ anymail/backends/sendinblue.py | 9 --------- 3 files changed, 5 insertions(+), 18 deletions(-) diff --git a/anymail/backends/base_requests.py b/anymail/backends/base_requests.py index 4ad46ea..64848b6 100644 --- a/anymail/backends/base_requests.py +++ b/anymail/backends/base_requests.py @@ -87,9 +87,10 @@ class AnymailRequestsBackend(AnymailBaseBackend): (though should defer parsing/deserialization of the body to parse_recipient_status) """ - if response.status_code != 200: - raise AnymailRequestsAPIError(email_message=message, payload=payload, response=response, - backend=self) + if response.status_code < 200 or response.status_code >= 300: + raise AnymailRequestsAPIError( + email_message=message, payload=payload, + response=response, backend=self) def deserialize_json_response(self, response, payload, message): """Deserialize an ESP API response that's in json. diff --git a/anymail/backends/sendgrid.py b/anymail/backends/sendgrid.py index acb8c05..1d9c260 100644 --- a/anymail/backends/sendgrid.py +++ b/anymail/backends/sendgrid.py @@ -5,7 +5,7 @@ from email.utils import quote as rfc822_quote from requests.structures import CaseInsensitiveDict from .base_requests import AnymailRequestsBackend, RequestsPayload -from ..exceptions import AnymailConfigurationError, AnymailRequestsAPIError, AnymailWarning +from ..exceptions import AnymailConfigurationError, AnymailWarning from ..message import AnymailRecipientStatus from ..utils import BASIC_NUMERIC_TYPES, Mapping, get_anymail_setting, update_deep @@ -52,11 +52,6 @@ class EmailBackend(AnymailRequestsBackend): def build_message_payload(self, message, defaults): return SendGridPayload(message, defaults, self) - def raise_for_status(self, response, payload, message): - if response.status_code < 200 or response.status_code >= 300: - raise AnymailRequestsAPIError(email_message=message, payload=payload, response=response, - backend=self) - def parse_recipient_status(self, response, payload, message): # If we get here, the send call was successful. # (SendGrid uses a non-2xx response for any failures, caught in raise_for_status.) diff --git a/anymail/backends/sendinblue.py b/anymail/backends/sendinblue.py index 8722853..4bfff08 100644 --- a/anymail/backends/sendinblue.py +++ b/anymail/backends/sendinblue.py @@ -35,15 +35,6 @@ class EmailBackend(AnymailRequestsBackend): def build_message_payload(self, message, defaults): return SendinBluePayload(message, defaults, self) - def raise_for_status(self, response, payload, message): - if response.status_code < 200 or response.status_code >= 300: - raise AnymailRequestsAPIError( - email_message=message, - payload=payload, - response=response, - backend=self, - ) - def parse_recipient_status(self, response, payload, message): # SendinBlue doesn't give any detail on a success # https://developers.sendinblue.com/docs/responses