Skip to content

Commit

Permalink
client: allow custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsobreira committed Mar 5, 2018
1 parent 8df83fb commit f86f494
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
23 changes: 12 additions & 11 deletions leadrouter/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,28 @@ def __init__(self, host, user, token):
self.host = prefix_http(host)
self.auth = requests.auth.HTTPBasicAuth(user, token)

def create_lead(self, site_uuid, lead):
def create_lead(self, site_uuid, lead, headers={}):
url = '/rest/sites/{0}/leads'.format(site_uuid)
return self._request('post', url, lead)
return self._request('post', url, lead, headers=headers)

def update_lead(self, site_uuid, lead_uuid, lead):
def update_lead(self, site_uuid, lead_uuid, lead, headers={}):
url = '/rest/sites/{0}/leads/{1}'.format(site_uuid, lead_uuid)
return self._request('patch', url, lead)
return self._request('patch', url, lead, headers=headers)

def add_activities(self, site_uuid, lead_uuid, activities):
def add_activities(self, site_uuid, lead_uuid, activities, headers={}):
url = '/rest/sites/{0}/leads/{1}/activities'.format(site_uuid, lead_uuid)
return self._request('post', url, activities)
return self._request('post', url, activities, headers=headers)

def create_potential_seller_lead(self, site_uuid, lead):
def create_potential_seller_lead(self, site_uuid, lead, headers={}):
url = '/rest/sites/{0}/potential-seller-leads'.format(site_uuid)
return self._request('post', url, lead)
return self._request('post', url, lead, headers=headers)

def _request(self, method, url, body):
def _request(self, method, url, body, headers={}):
hdrs = {'Content-Type': 'application/json; charset=utf-8'}
hdrs.update(headers)
try:
resp = requests.request(method, self.host+url, data=json.dumps(body),
timeout=2, auth=self.auth,
headers={'content-type': 'application/json; charset=utf-8'})
timeout=2, auth=self.auth, headers=hdrs)
resp.raise_for_status()
except requests.exceptions.RequestException as ex:
raise wrap_requests_exception(ex)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run_tests(self):

setup(
name='leadrouter',
version='2.0.1',
version='2.0.2',

description='Python Client to Real Geeks REST API',
url='https://github.com/RealGeeks/lead_router.py',
Expand Down
69 changes: 65 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ def test_create_lead_request():
c.create_lead('123', {'email': 'lead@gmail.com'})

c._request.assert_called_once_with('post', '/rest/sites/123/leads',
{'email': 'lead@gmail.com'})
{'email': 'lead@gmail.com'},
headers={})

def test_create_lead_request_with_custom_headers():
c = Client('api.com', 'user', 'token')
c._request = mock.Mock()

c.create_lead('123', {'email': 'lead@gmail.com'}, headers={'User-Agent': 'Me'})

c._request.assert_called_once_with('post', '/rest/sites/123/leads',
{'email': 'lead@gmail.com'},
headers={'User-Agent': 'Me'})

def test_update_lead_request():
c = Client('api.com', 'user', 'token')
Expand All @@ -23,7 +34,18 @@ def test_update_lead_request():
c.update_lead('123', '432', {'email': 'lead@gmail.com'})

c._request.assert_called_once_with('patch', '/rest/sites/123/leads/432',
{'email': 'lead@gmail.com'})
{'email': 'lead@gmail.com'},
headers={})

def test_update_lead_request_with_custom_headers():
c = Client('api.com', 'user', 'token')
c._request = mock.Mock()

c.update_lead('123', '432', {'email': 'lead@gmail.com'}, headers={'User-Agent': 'Me'})

c._request.assert_called_once_with('patch', '/rest/sites/123/leads/432',
{'email': 'lead@gmail.com'},
headers={'User-Agent': 'Me'})

def test_add_activities_request():
c = Client('api.com', 'user', 'token')
Expand All @@ -32,7 +54,18 @@ def test_add_activities_request():
c.add_activities('123', '543', [{'type': 'one'}, {'type': 'two'}])

c._request.assert_called_once_with('post', '/rest/sites/123/leads/543/activities',
[{'type': 'one'}, {'type': 'two'}])
[{'type': 'one'}, {'type': 'two'}],
headers={})

def test_add_activities_request_with_custom_headers():
c = Client('api.com', 'user', 'token')
c._request = mock.Mock()

c.add_activities('123', '543', [{'type': 'one'}, {'type': 'two'}], headers={'User-Agent': 'Me'})

c._request.assert_called_once_with('post', '/rest/sites/123/leads/543/activities',
[{'type': 'one'}, {'type': 'two'}],
headers={'User-Agent': 'Me'})

def test_create_potential_seller_lead_request():
c = Client('api.com', 'user', 'token')
Expand All @@ -41,8 +74,18 @@ def test_create_potential_seller_lead_request():
c.create_potential_seller_lead('123', {'id': '123'})

c._request.assert_called_once_with('post', '/rest/sites/123/potential-seller-leads',
{'id': '123'})
{'id': '123'},
headers={})

def test_create_potential_seller_lead_request_with_custom_headers():
c = Client('api.com', 'user', 'token')
c._request = mock.Mock()

c.create_potential_seller_lead('123', {'id': '123'}, headers={'User-Agent': 'Me'})

c._request.assert_called_once_with('post', '/rest/sites/123/potential-seller-leads',
{'id': '123'},
headers={'User-Agent': 'Me'})

# _request() is the method used by all methods of Client(), so we test it
# exhaustively
Expand All @@ -65,6 +108,24 @@ def test_request_sent():
assert request.headers['Authorization'] == 'Basic dXNlcjp0b2tlbg=='
assert request.headers['Content-Type'] == 'application/json; charset=utf-8'

@httpretty.activate
def test_request_sent_with_custom_headers():
"Ensure the request sent by _request() is built correctly"

httpretty.register_uri(httpretty.PUT, re.compile(r'.*'))

c = Client('api.com', 'user', 'token')
c._request('put', '/endpoint', {"email":"lead@gmail.com"}, headers={
'User-Agent': 'me :)',
'X-Custom-Header': 'custom',
'Content-Type': 'application/json', # can override the default
})

request = httpretty.last_request()

assert request.headers['user-agent'] == 'me :)'
assert request.headers['x-custom-header'] == 'custom'
assert request.headers['content-type'] == 'application/json'

@httpretty.activate
def test_request_returns_invalid_status_code_with_error_in_body():
Expand Down

0 comments on commit f86f494

Please sign in to comment.