|
9 | 9 | from StringIO import StringIO |
10 | 10 | except ImportError: # Python 3 |
11 | 11 | from io import StringIO |
12 | | -import logging |
| 12 | +try: |
| 13 | + import urllib.request as urllib_request |
| 14 | + from urllib.parse import urlencode |
| 15 | + from urllib.error import HTTPError |
| 16 | +except ImportError: # Python 2 |
| 17 | + import urllib2 as urllib_request |
| 18 | + from urllib2 import HTTPError |
| 19 | + from urllib import urlencode |
13 | 20 |
|
14 | 21 | import sendgrid |
15 | 22 | from sendgrid import SendGridClient, Mail |
|
21 | 28 | SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY' |
22 | 29 |
|
23 | 30 | class MockSendGridAPIClientRequest(SendGridAPIClient): |
| 31 | + def __init__(self, apikey, **opts): |
| 32 | + super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts) |
| 33 | + self._req = None |
| 34 | + |
24 | 35 | def _build_request(self, url=None, json_header=False, method='GET', data=None): |
| 36 | + req = urllib_request.Request(url) |
| 37 | + req.get_method = lambda: method |
| 38 | + req.add_header('User-Agent', self.useragent) |
| 39 | + req.add_header('Authorization', 'Bearer ' + self.apikey) |
| 40 | + if json_header: |
| 41 | + req.add_header('Content-Type', 'application/json') |
| 42 | + print "url= " + req._Request__original |
| 43 | + print "headers= " + str(req.headers) |
| 44 | + print "data= " + json.dumps(data) |
25 | 45 | response = 200 |
26 | | - body = {"message": "success"} |
| 46 | + body = {"mock": "success"} |
27 | 47 | return response, body |
28 | 48 |
|
29 | 49 | class TestSendGridAPIClient(unittest.TestCase): |
|
0 commit comments