|
| 1 | +import json |
| 2 | +from .version import __version__ |
| 3 | +from socket import timeout |
| 4 | +try: |
| 5 | + import urllib.request as urllib_request |
| 6 | + from urllib.parse import urlencode |
| 7 | + from urllib.error import HTTPError |
| 8 | +except ImportError: # Python 2 |
| 9 | + import urllib2 as urllib_request |
| 10 | + from urllib2 import HTTPError |
| 11 | + from urllib import urlencode |
| 12 | + |
| 13 | +from .exceptions import SendGridClientError, SendGridServerError |
| 14 | +from resources.apikeys import APIKeys |
| 15 | + |
| 16 | +class SendGridAPIClient(object): |
| 17 | + |
| 18 | + """SendGrid API.""" |
| 19 | + |
| 20 | + def __init__(self, apikey, **opts): |
| 21 | + """ |
| 22 | + Construct SendGrid API object. |
| 23 | +
|
| 24 | + Args: |
| 25 | + apikey: SendGrid API key |
| 26 | + opts: You can pass in host or proxies |
| 27 | + """ |
| 28 | + self._apikey = apikey |
| 29 | + self.useragent = 'sendgrid/' + __version__ + ';python_v3' |
| 30 | + self.host = opts.get('host', 'https://api.sendgrid.com') |
| 31 | + # urllib cannot connect to SSL servers using proxies |
| 32 | + self.proxies = opts.get('proxies', None) |
| 33 | + |
| 34 | + self.apikeys = APIKeys(self) |
| 35 | + |
| 36 | + @property |
| 37 | + def apikey(self): |
| 38 | + return self._apikey |
| 39 | + |
| 40 | + @apikey.setter |
| 41 | + def apikey(self, value): |
| 42 | + self._apikey = value |
| 43 | + |
| 44 | + def _build_request(self, url, json_header=False, method='GET', data=None): |
| 45 | + if self.proxies: |
| 46 | + proxy_support = urllib_request.ProxyHandler(self.proxies) |
| 47 | + opener = urllib_request.build_opener(proxy_support) |
| 48 | + urllib_request.install_opener(opener) |
| 49 | + req = urllib_request.Request(url) |
| 50 | + req.get_method = lambda: method |
| 51 | + req.add_header('User-Agent', self.useragent) |
| 52 | + req.add_header('Authorization', 'Bearer ' + self.apikey) |
| 53 | + if json_header: |
| 54 | + req.add_header('Content-Type', 'application/json') |
| 55 | + try: |
| 56 | + if data: |
| 57 | + response = urllib_request.urlopen(req, json.dumps(data)) |
| 58 | + else: |
| 59 | + response = urllib_request.urlopen(req, timeout=10) |
| 60 | + except HTTPError as e: |
| 61 | + if 400 <= e.code < 500: |
| 62 | + raise SendGridClientError(e.code, e.read()) |
| 63 | + elif 500 <= e.code < 600: |
| 64 | + raise SendGridServerError(e.code, e.read()) |
| 65 | + else: |
| 66 | + assert False |
| 67 | + except timeout as e: |
| 68 | + raise SendGridClientError(408, 'Request timeout') |
| 69 | + body = response.read() |
| 70 | + return response, body |
| 71 | + |
| 72 | + def get(self, api): |
| 73 | + url = self.host + api.base_endpoint |
| 74 | + response, body = self._build_request(url, False, 'GET') |
| 75 | + return response.getcode(), body |
| 76 | + |
| 77 | + def post(self, api, data): |
| 78 | + url = self.host + api.endpoint |
| 79 | + response, body = self._build_request(url, True, 'POST', data) |
| 80 | + return response.getcode(), body |
| 81 | + |
| 82 | + def delete(self, api): |
| 83 | + url = self.host + api.endpoint |
| 84 | + response, body = self._build_request(url, False, 'DELETE') |
| 85 | + return response.getcode(), body |
| 86 | + |
| 87 | + def patch(self, api, data): |
| 88 | + url = self.host + api.endpoint |
| 89 | + response, body = self._build_request(url, True, 'PATCH', data) |
| 90 | + return response.getcode(), body |
0 commit comments