Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sendgrid/base_interface.py: 86%
22 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import python_http_client
4class BaseInterface(object):
5 def __init__(self, auth, host, impersonate_subuser):
6 """
7 Construct the Twilio SendGrid v3 API object.
8 Note that the underlying client is being set up during initialization,
9 therefore changing attributes in runtime will not affect HTTP client
10 behaviour.
12 :param auth: the authorization header
13 :type auth: string
14 :param impersonate_subuser: the subuser to impersonate. Will be passed
15 by "On-Behalf-Of" header by underlying
16 client. See
17 https://sendgrid.com/docs/User_Guide/Settings/subusers.html
18 for more details
19 :type impersonate_subuser: string
20 :param host: base URL for API calls
21 :type host: string
22 """
23 from . import __version__
24 self.auth = auth
25 self.host = host
26 self.impersonate_subuser = impersonate_subuser
27 self.version = __version__
28 self.useragent = 'sendgrid/{};python'.format(self.version)
30 self.client = python_http_client.Client(
31 host=self.host,
32 request_headers=self._default_headers,
33 version=3)
35 @property
36 def _default_headers(self):
37 """Set the default header for a Twilio SendGrid v3 API call"""
38 headers = {
39 "Authorization": self.auth,
40 "User-Agent": self.useragent,
41 "Accept": 'application/json'
42 }
43 if self.impersonate_subuser: 43 ↛ 44line 43 didn't jump to line 44, because the condition on line 43 was never true
44 headers['On-Behalf-Of'] = self.impersonate_subuser
46 return headers
48 def reset_request_headers(self):
49 self.client.request_headers = self._default_headers
51 def send(self, message):
52 """Make a Twilio SendGrid v3 API request with the request body generated by
53 the Mail object
55 :param message: The Twilio SendGrid v3 API request body generated by the Mail
56 object
57 :type message: Mail
58 """
59 if not isinstance(message, dict): 59 ↛ 62line 59 didn't jump to line 62, because the condition on line 59 was never false
60 message = message.get()
62 return self.client.mail.send.post(request_body=message)