SendGrid: convert long to str in headers, metadata

SendGrid requires extra headers and metadata values be strings.
Anymail has always coerced int and float; this treats Python 2's
`long` integer type the same.

Fixes #74
This commit is contained in:
medmunds
2017-09-14 11:45:17 -07:00
parent 168d46a254
commit 3b9cb963ef
5 changed files with 25 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ from requests.structures import CaseInsensitiveDict
from .base_requests import AnymailRequestsBackend, RequestsPayload
from ..exceptions import AnymailConfigurationError, AnymailRequestsAPIError, AnymailWarning
from ..message import AnymailRecipientStatus
from ..utils import get_anymail_setting, timestamp, update_deep, parse_address_list
from ..utils import BASIC_NUMERIC_TYPES, get_anymail_setting, timestamp, update_deep, parse_address_list
class EmailBackend(AnymailRequestsBackend):
@@ -240,7 +240,7 @@ class SendGridPayload(RequestsPayload):
# SendGrid requires header values to be strings -- not integers.
# We'll stringify ints and floats; anything else is the caller's responsibility.
self.data["headers"].update({
k: str(v) if isinstance(v, (int, float)) else v
k: str(v) if isinstance(v, BASIC_NUMERIC_TYPES) else v
for k, v in headers.items()
})
@@ -290,7 +290,7 @@ class SendGridPayload(RequestsPayload):
# if they're not.)
# We'll stringify ints and floats; anything else is the caller's responsibility.
self.data["custom_args"] = {
k: str(v) if isinstance(v, (int, float)) else v
k: str(v) if isinstance(v, BASIC_NUMERIC_TYPES) else v
for k, v in metadata.items()
}

View File

@@ -5,7 +5,7 @@ from requests.structures import CaseInsensitiveDict
from ..exceptions import AnymailConfigurationError, AnymailRequestsAPIError, AnymailWarning
from ..message import AnymailRecipientStatus
from ..utils import get_anymail_setting, timestamp
from ..utils import BASIC_NUMERIC_TYPES, get_anymail_setting, timestamp
from .base_requests import AnymailRequestsBackend, RequestsPayload
@@ -238,7 +238,7 @@ class SendGridPayload(RequestsPayload):
# We'll stringify ints and floats; anything else is the caller's responsibility.
# (This field gets converted to json in self.serialize_data)
self.data["headers"].update({
k: str(v) if isinstance(v, (int, float)) else v
k: str(v) if isinstance(v, BASIC_NUMERIC_TYPES) else v
for k, v in headers.items()
})

View File

@@ -18,6 +18,10 @@ from six.moves.urllib.parse import urlsplit, urlunsplit
from .exceptions import AnymailConfigurationError, AnymailInvalidAddress
BASIC_NUMERIC_TYPES = six.integer_types + (float,) # int, float, and (on Python 2) long
UNSET = object() # Used as non-None default value