Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct the User-Agent casing #881

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 96 additions & 96 deletions sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
"""
This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Python.
For more information on this library, see the README on GitHub.
https://github.com/sendgrid/sendgrid-python
For more information on the Twilio SendGrid v3 API, see the v3 docs:
http://sendgrid.com/docs/API_Reference/api_v3.html
For the user guide, code examples, and more, visit the main docs page:
http://sendgrid.com/docs/index.html
This file provides the Twilio SendGrid API Client.
"""
import os
import python_http_client
class SendGridAPIClient(object):
"""The Twilio SendGrid API Client.
Use this object to interact with the v3 API. For example:
sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
...
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
For examples and detailed use instructions, see
https://github.com/sendgrid/sendgrid-python
"""
def __init__(
self,
api_key=None,
host='https://api.sendgrid.com',
impersonate_subuser=None):
"""
Construct the Twilio SendGrid v3 API object.
Note that the underlying client is being set up during initialization,
therefore changing attributes in runtime will not affect HTTP client
behaviour.
:param api_key: Twilio SendGrid API key to use. If not provided, key will be
read from environment variable "SENDGRID_API_KEY"
:type api_key: string
:param impersonate_subuser: the subuser to impersonate. Will be passed
by "On-Behalf-Of" header by underlying
client. See
https://sendgrid.com/docs/User_Guide/Settings/subusers.html
for more details
:type impersonate_subuser: string
:param host: base URL for API calls
:type host: string
"""
from . import __version__
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
self.impersonate_subuser = impersonate_subuser
self.host = host
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)
self.client = python_http_client.Client(
host=self.host,
request_headers=self._default_headers,
version=3)
@property
def _default_headers(self):
"""Set the default header for a Twilio SendGrid v3 API call"""
headers = {
"Authorization": 'Bearer {}'.format(self.api_key),
"User-agent": self.useragent,
"Accept": 'application/json'
}
if self.impersonate_subuser:
headers['On-Behalf-Of'] = self.impersonate_subuser
return headers
def reset_request_headers(self):
self.client.request_headers = self._default_headers
def send(self, message):
"""Make a Twilio SendGrid v3 API request with the request body generated by
the Mail object
:param message: The Twilio SendGrid v3 API request body generated by the Mail
object
:type message: Mail
"""
if isinstance(message, dict):
response = self.client.mail.send.post(request_body=message)
else:
response = self.client.mail.send.post(request_body=message.get())
return response
"""
This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via Python.

For more information on this library, see the README on GitHub.
https://github.com/sendgrid/sendgrid-python
For more information on the Twilio SendGrid v3 API, see the v3 docs:
http://sendgrid.com/docs/API_Reference/api_v3.html
For the user guide, code examples, and more, visit the main docs page:
http://sendgrid.com/docs/index.html

This file provides the Twilio SendGrid API Client.
"""

import os

import python_http_client


class SendGridAPIClient(object):
"""The Twilio SendGrid API Client.

Use this object to interact with the v3 API. For example:
sg = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
...
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())

For examples and detailed use instructions, see
https://github.com/sendgrid/sendgrid-python
"""

def __init__(
self,
api_key=None,
host='https://api.sendgrid.com',
impersonate_subuser=None):
"""
Construct the Twilio SendGrid v3 API object.
Note that the underlying client is being set up during initialization,
therefore changing attributes in runtime will not affect HTTP client
behaviour.

:param api_key: Twilio SendGrid API key to use. If not provided, key will be
read from environment variable "SENDGRID_API_KEY"
:type api_key: string
:param impersonate_subuser: the subuser to impersonate. Will be passed
by "On-Behalf-Of" header by underlying
client. See
https://sendgrid.com/docs/User_Guide/Settings/subusers.html
for more details
:type impersonate_subuser: string
:param host: base URL for API calls
:type host: string
"""
from . import __version__
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
self.impersonate_subuser = impersonate_subuser
self.host = host
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)

self.client = python_http_client.Client(
host=self.host,
request_headers=self._default_headers,
version=3)

@property
def _default_headers(self):
"""Set the default header for a Twilio SendGrid v3 API call"""
headers = {
"Authorization": 'Bearer {}'.format(self.api_key),
"User-Agent": self.useragent,
"Accept": 'application/json'
}
if self.impersonate_subuser:
headers['On-Behalf-Of'] = self.impersonate_subuser

return headers

def reset_request_headers(self):

self.client.request_headers = self._default_headers

def send(self, message):
"""Make a Twilio SendGrid v3 API request with the request body generated by
the Mail object

:param message: The Twilio SendGrid v3 API request body generated by the Mail
object
:type message: Mail
"""
if isinstance(message, dict):
response = self.client.mail.send.post(request_body=message)
else:
response = self.client.mail.send.post(request_body=message.get())
return response
4 changes: 2 additions & 2 deletions test/test_sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def test_host(self):
def test_get_default_headers(self):
headers = self.sg._default_headers
self.assertIn('Authorization', headers)
self.assertIn('User-agent', headers)
self.assertIn('User-Agent', headers)
self.assertIn('Accept', headers)
self.assertNotIn('On-Behalf-Of', headers)

self.sg.impersonate_subuser = '[email protected]'
headers = self.sg._default_headers
self.assertIn('Authorization', headers)
self.assertIn('User-agent', headers)
self.assertIn('User-Agent', headers)
self.assertIn('Accept', headers)
self.assertIn('On-Behalf-Of', headers)

Expand Down