|
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 |
0 commit comments