Skip to content

Commit 1ff5de6

Browse files
author
childish-sambino
authored
fix: correct the User-Agent casing (#881)
1 parent f5a0dc7 commit 1ff5de6

File tree

2 files changed

+98
-98
lines changed

2 files changed

+98
-98
lines changed

sendgrid/sendgrid.py

+96-96
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
1-
"""
2-
This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Python.
3-
4-
For more information on this library, see the README on GitHub.
5-
https://github.com/sendgrid/sendgrid-python
6-
For more information on the Twilio SendGrid v3 API, see the v3 docs:
7-
http://sendgrid.com/docs/API_Reference/api_v3.html
8-
For the user guide, code examples, and more, visit the main docs page:
9-
http://sendgrid.com/docs/index.html
10-
11-
This file provides the Twilio SendGrid API Client.
12-
"""
13-
14-
import os
15-
16-
import python_http_client
17-
18-
19-
class SendGridAPIClient(object):
20-
"""The Twilio SendGrid API Client.
21-
22-
Use this object to interact with the v3 API. For example:
23-
sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
24-
...
25-
mail = Mail(from_email, subject, to_email, content)
26-
response = sg.client.mail.send.post(request_body=mail.get())
27-
28-
For examples and detailed use instructions, see
29-
https://github.com/sendgrid/sendgrid-python
30-
"""
31-
32-
def __init__(
33-
self,
34-
api_key=None,
35-
host='https://api.sendgrid.com',
36-
impersonate_subuser=None):
37-
"""
38-
Construct the Twilio SendGrid v3 API object.
39-
Note that the underlying client is being set up during initialization,
40-
therefore changing attributes in runtime will not affect HTTP client
41-
behaviour.
42-
43-
:param api_key: Twilio SendGrid API key to use. If not provided, key will be
44-
read from environment variable "SENDGRID_API_KEY"
45-
:type api_key: string
46-
:param impersonate_subuser: the subuser to impersonate. Will be passed
47-
by "On-Behalf-Of" header by underlying
48-
client. See
49-
https://sendgrid.com/docs/User_Guide/Settings/subusers.html
50-
for more details
51-
:type impersonate_subuser: string
52-
:param host: base URL for API calls
53-
:type host: string
54-
"""
55-
from . import __version__
56-
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
57-
self.impersonate_subuser = impersonate_subuser
58-
self.host = host
59-
self.version = __version__
60-
self.useragent = 'sendgrid/{};python'.format(self.version)
61-
62-
self.client = python_http_client.Client(
63-
host=self.host,
64-
request_headers=self._default_headers,
65-
version=3)
66-
67-
@property
68-
def _default_headers(self):
69-
"""Set the default header for a Twilio SendGrid v3 API call"""
70-
headers = {
71-
"Authorization": 'Bearer {}'.format(self.api_key),
72-
"User-agent": self.useragent,
73-
"Accept": 'application/json'
74-
}
75-
if self.impersonate_subuser:
76-
headers['On-Behalf-Of'] = self.impersonate_subuser
77-
78-
return headers
79-
80-
def reset_request_headers(self):
81-
82-
self.client.request_headers = self._default_headers
83-
84-
def send(self, message):
85-
"""Make a Twilio SendGrid v3 API request with the request body generated by
86-
the Mail object
87-
88-
:param message: The Twilio SendGrid v3 API request body generated by the Mail
89-
object
90-
:type message: Mail
91-
"""
92-
if isinstance(message, dict):
93-
response = self.client.mail.send.post(request_body=message)
94-
else:
95-
response = self.client.mail.send.post(request_body=message.get())
96-
return response
1+
"""
2+
This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Python.
3+
4+
For more information on this library, see the README on GitHub.
5+
https://github.com/sendgrid/sendgrid-python
6+
For more information on the Twilio SendGrid v3 API, see the v3 docs:
7+
http://sendgrid.com/docs/API_Reference/api_v3.html
8+
For the user guide, code examples, and more, visit the main docs page:
9+
http://sendgrid.com/docs/index.html
10+
11+
This file provides the Twilio SendGrid API Client.
12+
"""
13+
14+
import os
15+
16+
import python_http_client
17+
18+
19+
class SendGridAPIClient(object):
20+
"""The Twilio SendGrid API Client.
21+
22+
Use this object to interact with the v3 API. For example:
23+
sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
24+
...
25+
mail = Mail(from_email, subject, to_email, content)
26+
response = sg.client.mail.send.post(request_body=mail.get())
27+
28+
For examples and detailed use instructions, see
29+
https://github.com/sendgrid/sendgrid-python
30+
"""
31+
32+
def __init__(
33+
self,
34+
api_key=None,
35+
host='https://api.sendgrid.com',
36+
impersonate_subuser=None):
37+
"""
38+
Construct the Twilio SendGrid v3 API object.
39+
Note that the underlying client is being set up during initialization,
40+
therefore changing attributes in runtime will not affect HTTP client
41+
behaviour.
42+
43+
:param api_key: Twilio SendGrid API key to use. If not provided, key will be
44+
read from environment variable "SENDGRID_API_KEY"
45+
:type api_key: string
46+
:param impersonate_subuser: the subuser to impersonate. Will be passed
47+
by "On-Behalf-Of" header by underlying
48+
client. See
49+
https://sendgrid.com/docs/User_Guide/Settings/subusers.html
50+
for more details
51+
:type impersonate_subuser: string
52+
:param host: base URL for API calls
53+
:type host: string
54+
"""
55+
from . import __version__
56+
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
57+
self.impersonate_subuser = impersonate_subuser
58+
self.host = host
59+
self.version = __version__
60+
self.useragent = 'sendgrid/{};python'.format(self.version)
61+
62+
self.client = python_http_client.Client(
63+
host=self.host,
64+
request_headers=self._default_headers,
65+
version=3)
66+
67+
@property
68+
def _default_headers(self):
69+
"""Set the default header for a Twilio SendGrid v3 API call"""
70+
headers = {
71+
"Authorization": 'Bearer {}'.format(self.api_key),
72+
"User-Agent": self.useragent,
73+
"Accept": 'application/json'
74+
}
75+
if self.impersonate_subuser:
76+
headers['On-Behalf-Of'] = self.impersonate_subuser
77+
78+
return headers
79+
80+
def reset_request_headers(self):
81+
82+
self.client.request_headers = self._default_headers
83+
84+
def send(self, message):
85+
"""Make a Twilio SendGrid v3 API request with the request body generated by
86+
the Mail object
87+
88+
:param message: The Twilio SendGrid v3 API request body generated by the Mail
89+
object
90+
:type message: Mail
91+
"""
92+
if isinstance(message, dict):
93+
response = self.client.mail.send.post(request_body=message)
94+
else:
95+
response = self.client.mail.send.post(request_body=message.get())
96+
return response

test/test_sendgrid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ def test_host(self):
5151
def test_get_default_headers(self):
5252
headers = self.sg._default_headers
5353
self.assertIn('Authorization', headers)
54-
self.assertIn('User-agent', headers)
54+
self.assertIn('User-Agent', headers)
5555
self.assertIn('Accept', headers)
5656
self.assertNotIn('On-Behalf-Of', headers)
5757

5858
self.sg.impersonate_subuser = '[email protected]'
5959
headers = self.sg._default_headers
6060
self.assertIn('Authorization', headers)
61-
self.assertIn('User-agent', headers)
61+
self.assertIn('User-Agent', headers)
6262
self.assertIn('Accept', headers)
6363
self.assertIn('On-Behalf-Of', headers)
6464

0 commit comments

Comments
 (0)