Skip to content

Fix code style issues #54

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

Merged
merged 2 commits into from
Nov 17, 2017
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
1 change: 0 additions & 1 deletion examples/live_sendgrid_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,3 @@
response = client.api_keys._(api_key_id).delete()
print(response.status_code)
print(response.headers)

1 change: 0 additions & 1 deletion python_http_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
ServiceUnavailableError,
GatewayTimeoutError
)

22 changes: 13 additions & 9 deletions python_http_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def _build_url(self, query_params):
if query_params:
url_values = urlencode(sorted(query_params.items()), True)
url = '{0}?{1}'.format(url, url_values)
url = self._build_versioned_url(url) if self._version else self.host + url
url = (self._build_versioned_url(url) if self._version
else self.host + url)
return url

def _update_headers(self, request_headers):
Expand Down Expand Up @@ -207,21 +208,24 @@ def http_request(*_, **kwargs):
if 'request_body' not in kwargs:
data = None
else:
# Don't serialize to a JSON formatted str if we don't have a JSON Content-Type
if 'Content-Type' in self.request_headers:
if self.request_headers['Content-Type'] != 'application/json':
data = kwargs['request_body'].encode('utf-8')
# Don't serialize to a JSON formatted str if
# we don't have a JSON Content-Type
content_type = self.request_headers.get('Content-Type')
if content_type is not None:
if content_type != 'application/json':
data = kwargs['request_body']
else:
data = json.dumps(kwargs['request_body']).encode('utf-8')
data = json.dumps(kwargs['request_body'])
else:
data = json.dumps(kwargs['request_body']).encode('utf-8')
params = kwargs['query_params'] if 'query_params' in kwargs else None
data = json.dumps(kwargs['request_body'])
data = data.encode('utf-8')
params = kwargs.get('query_params')
opener = urllib.build_opener()
request = urllib.Request(self._build_url(params), data=data)
if self.request_headers:
for key, value in self.request_headers.items():
request.add_header(key, value)
if data and not ('Content-Type' in self.request_headers):
if data and ('Content-Type' not in self.request_headers):
request.add_header('Content-Type', 'application/json')
request.get_method = lambda: method
return Response(self._make_request(opener, request))
Expand Down
27 changes: 18 additions & 9 deletions register.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import pypandoc
import os


output = pypandoc.convert('README.md', 'rst')
f = open('README.txt','w+')
f.write(output)
f.close()

readme_rst = open('./README.txt').read()
replace = '[SendGrid Logo]\n(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)'
replacement = '|SendGrid Logo|\n\n.. |SendGrid Logo| image:: https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png\n :target: https://www.sendgrid.com'
final_text = readme_rst.replace(replace,replacement)

with open('README.txt', 'w+') as f:
f.write(output)

with open('./README.txt') as f:
readme_rst = f.read()

replace = ('[SendGrid Logo]\n'
'(https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)')

replacement = ('|SendGrid Logo|\n\n.. |SendGrid Logo| image:: '
'https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png'
'\n :target: https://www.sendgrid.com')

final_text = readme_rst.replace(replace, replacement)

with open('./README.txt', 'w') as f:
f.write(final_text)
f.write(final_text)
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
import os
from setuptools import setup


long_description = 'Please see our GitHub README'
if os.path.exists('README.txt'):
long_description = open('README.txt').read()


def getRequires():
deps = []
if (2, 6) <= sys.version_info < (2, 7):
deps.append('unittest2')
return deps


base_url = 'https://github.com/sendgrid/'
version = '3.0.0'
setup(
Expand Down
17 changes: 10 additions & 7 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@


class MockException(HTTPError):
def __init__(self,code):

def __init__(self, code):
self.code = code
self.reason = 'REASON'
self.hdrs = 'HEADERS'

def read(self):
return 'BODY'

Expand All @@ -60,18 +62,18 @@ def __init__(self, host, response_code):
Client.__init__(self, host)

def _make_request(self, opener, request):
if 200 <= self.response_code <299: # if successsful code
if 200 <= self.response_code < 299: # if successsful code
return MockResponse(self.response_code)
else:
raise handle_error(MockException(self.response_code))



class TestClient(unittest.TestCase):

def setUp(self):
self.host = 'http://api.test.com'
self.client = Client(host=self.host)
self.api_key = "SENDGRID_API_KEY"
self.api_key = 'SENDGRID_API_KEY'
self.request_headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + self.api_key
Expand Down Expand Up @@ -112,9 +114,10 @@ def test__build_url(self):
self.client._url_path = self.client._url_path + ['there']
self.client._url_path = self.client._url_path + [1]
self.client._version = 3
url = '{0}/v{1}{2}'.format(self.host,
str(self.client._version),
'/here/there/1?hello=0&world=1&ztest=0&ztest=1')
url = '{0}/v{1}{2}'.format(
self.host,
str(self.client._version),
'/here/there/1?hello=0&world=1&ztest=0&ztest=1')
query_params = {'hello': 0, 'world': 1, 'ztest': [0,1]}
built_url = self.client._build_url(query_params)
self.assertEqual(built_url, url)
Expand Down