Skip to content

Commit bf215b8

Browse files
author
Matt Bernier
authored
Merge branch 'master' into master
2 parents f8b6fa6 + 949dc21 commit bf215b8

File tree

10 files changed

+132
-15
lines changed

10 files changed

+132
-15
lines changed

.env_sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export SENDGRID_API_KEY=''

.github/PULL_REQUEST_TEMPLATE

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
**Description of the change**:
1+
<!--
2+
We appreciate the effort for this pull request but before that please make sure you read the contribution guidelines given above, then fill out the blanks below.
23

3-
**Reason for the change**:
44

5+
Please enter each Issue number you are resolving in your PR after one of the following words [Fixes, Closes, Resolves]. This will auto-link these issues and close them when this PR is merged!
6+
e.g.
7+
Fixes #1
8+
Closes #2
9+
-->
10+
# Fixes #
511

12+
### Checklist
13+
- [ ] I have made a material change to the repo (functionality, testing, spelling, grammar)
14+
- [ ] I have read the [Contribution Guide] and my PR follows them.
15+
- [ ] I updated my branch with the master branch.
16+
- [ ] I have added tests that prove my fix is effective or that my feature works
17+
- [ ] I have added necessary documentation about the functionality in the appropriate .md file
18+
- [ ] I have added in line documentation to the code I modified
19+
20+
### Short description of what this PR does:
21+
-
22+
-
23+
24+
If you have questions, please send an email to [Sendgrid](mailto:[email protected]), or file a Github Issue in this repository.

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 SendGrid, Inc.
3+
Copyright (c) 2012 - 2017 SendGrid, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ or
4545
easy_install python_http_client
4646
```
4747

48+
## API Key
49+
50+
Store your SendGrid API key in a .env file
51+
52+
```bash
53+
cp .env_sample .env
54+
```
55+
56+
Edit the `.env` file and add your API key.
57+
4858
<a name="quick-start"></a>
4959
# Quick Start
5060

python_http_client/client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ def _build_url(self, query_params):
117117
if query_params:
118118
url_values = urlencode(sorted(query_params.items()), True)
119119
url = '{0}?{1}'.format(url, url_values)
120+
120121
if self._version:
121122
url = self._build_versioned_url(url)
122123
else:
123124
url = self.host + url
125+
124126
return url
125127

126128
def _update_headers(self, request_headers):
@@ -210,6 +212,7 @@ def http_request(*_, **kwargs):
210212
if 'request_body' not in kwargs:
211213
data = None
212214
else:
215+
213216
# Don't serialize to a JSON formatted str
214217
# if we don't have a JSON Content-Type
215218
if 'Content-Type' in self.request_headers:
@@ -226,16 +229,23 @@ def http_request(*_, **kwargs):
226229
params = kwargs['query_params']
227230
else:
228231
params = None
232+
229233
opener = urllib.build_opener()
230234
request = urllib.Request(self._build_url(params), data=data)
231235
if self.request_headers:
232236
for key, value in self.request_headers.items():
233237
request.add_header(key, value)
234-
if data and not ('Content-Type' in self.request_headers):
238+
if data and ('Content-Type' not in self.request_headers):
235239
request.add_header('Content-Type', 'application/json')
236240
request.get_method = lambda: method
237241
return Response(self._make_request(opener, request))
238242
return http_request
239243
else:
240244
# Add a segment to the URL
241245
return self._(name)
246+
247+
def __getstate__(self):
248+
return self.__dict__
249+
250+
def __setstate__(self, state):
251+
self.__dict__ = state

register.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import pypandoc
22
import os
33

4+
45
output = pypandoc.convert('README.md', 'rst')
5-
f = open('README.txt', 'w+')
6-
f.write(output)
7-
f.close()
8-
9-
readme_rst = open('./README.txt').read()
10-
replace = '[SendGrid Logo]\n' + /
11-
'(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)'
12-
replacement = '|SendGrid Logo|\n\n.. |SendGrid Logo| image:: ' + /
13-
'https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png\n ' + /
14-
':target: https://www.sendgrid.com'
6+
7+
with open('README.txt', 'w+') as f:
8+
f.write(output)
9+
10+
with open('./README.txt') as f:
11+
readme_rst = f.read()
12+
13+
replace = ('[SendGrid Logo]\n'
14+
'(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)')
15+
16+
replacement = ('|SendGrid Logo|\n\n.. |SendGrid Logo| image:: '
17+
'https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png'
18+
'\n :target: https://www.sendgrid.com')
19+
1520
final_text = readme_rst.replace(replace, replacement)
21+
1622
with open('./README.txt', 'w') as f:
1723
f.write(final_text)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from setuptools import setup
44

5+
56
long_description = 'Please see our GitHub README'
67
if os.path.exists('README.txt'):
78
long_description = open('README.txt').read()

tests/test_daterange.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import time
3+
4+
try:
5+
import unittest2 as unittest
6+
except ImportError:
7+
import unittest
8+
9+
10+
class DateRangeTest(unittest.TestCase):
11+
def setUp(self):
12+
self.openlicensefile = os.path.join(
13+
os.path.dirname(__file__),
14+
'../LICENSE.txt')
15+
self.pattern = 'Copyright (c) 2012 - %s SendGrid, Inc.' % (
16+
time.strftime("%Y"))
17+
self.licensefile = open(self.openlicensefile).read()
18+
19+
def test__daterange(self):
20+
self.assertTrue(self.pattern in self.licensefile)

tests/test_repofiles.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from os import path
2+
try:
3+
import unittest2 as unittest
4+
except ImportError:
5+
import unittest
6+
7+
8+
class RepoFiles(unittest.TestCase):
9+
FILES = [
10+
['./Dockerfile', './docker/Dockerfile'],
11+
['./docker-compose.yml', './docker/docker-compose.yml'],
12+
['./.env_sample'],
13+
['./.gitignore'],
14+
['./.travis.yml'],
15+
['./.codeclimate.yml'],
16+
['./CHANGELOG.md'],
17+
['./CODE_OF_CONDUCT.md'],
18+
['./CONTRIBUTING.md'],
19+
['./.github/ISSUE_TEMPLATE'],
20+
['./LICENSE.md', './LICENSE.txt'],
21+
['./.github/PULL_REQUEST_TEMPLATE'],
22+
['./README.md'],
23+
['./TROUBLESHOOTING.md'],
24+
['./USAGE.md'],
25+
['./USE_CASES.md']
26+
]
27+
28+
def _all_file(self, files):
29+
'''
30+
Checks the list of files and sees if they exist. If all of them don't
31+
exist, returns False. Otherwise, return True.
32+
'''
33+
return all(map(lambda f: not path.isfile(f), files))
34+
35+
def test_file_existance(self):
36+
missing = list(filter(self._all_file, self.FILES))
37+
self.assertTrue(len(missing) == 0,
38+
"Files %s aren't found" % str(missing))

tests/test_unit.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import pickle
23
from os import path
34
try:
45
import unittest2 as unittest
@@ -30,6 +31,7 @@
3031

3132

3233
class MockException(HTTPError):
34+
3335
def __init__(self, code):
3436
self.code = code
3537
self.reason = 'REASON'
@@ -61,17 +63,19 @@ def __init__(self, host, response_code):
6163
Client.__init__(self, host)
6264

6365
def _make_request(self, opener, request):
66+
6467
if 200 <= self.response_code < 299: # if successsful code
6568
return MockResponse(self.response_code)
6669
else:
6770
raise handle_error(MockException(self.response_code))
6871

6972

7073
class TestClient(unittest.TestCase):
74+
7175
def setUp(self):
7276
self.host = 'http://api.test.com'
7377
self.client = Client(host=self.host)
74-
self.api_key = "SENDGRID_API_KEY"
78+
self.api_key = 'SENDGRID_API_KEY'
7579
self.request_headers = {
7680
'Content-Type': 'application/json',
7781
'Authorization': 'Bearer ' + self.api_key
@@ -112,11 +116,13 @@ def test__build_url(self):
112116
self.client._url_path = self.client._url_path + ['there']
113117
self.client._url_path = self.client._url_path + [1]
114118
self.client._version = 3
119+
115120
url = '{0}/v{1}{2}'.format(self.host,
116121
str(self.client._version),
117122
'/here/there/1?hello=0&' +
118123
'world=1&ztest=0&ztest=1')
119124
query_params = {'hello': 0, 'world': 1, 'ztest': [0, 1]}
125+
120126
built_url = self.client._build_url(query_params)
121127
self.assertEqual(built_url, url)
122128

@@ -186,5 +192,11 @@ def test__getattr__(self):
186192
self.assertRaises(HTTPError, mock_client.delete)
187193

188194

195+
def test_client_pickle_unpickle(self):
196+
pickled_client = pickle.dumps(self.client)
197+
unpickled_client = pickle.loads(pickled_client)
198+
self.assertDictEqual(self.client.__dict__, unpickled_client.__dict__, "original client and unpickled client must have the same state")
199+
200+
189201
if __name__ == '__main__':
190202
unittest.main()

0 commit comments

Comments
 (0)