Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhayes committed Dec 8, 2016
2 parents 1f860a0 + 400aa82 commit 262e928
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Release 0.4.0 - Friday 9 December 07:17:38 AEDT 2016

- Added test to ensure the session can be manipulated in get_request_kwargs
- Added test to ensure the method can be manipulated in get_request_kwargs
- Cleanup repetition of content-type headers in test_http
- Allow the URL used to make the API request to be manipulated in get_request_kwargs (#5)

# Release 0.3.2 - Friday 9 December 06:15:46 AEDT 2016

- Fixed issue where request_model was incorrectly included in signature to request method. (#4)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.2
0.4.0
2 changes: 1 addition & 1 deletion eater/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'VersionInfo', ('major', 'minor', 'micro', 'releaselevel', 'serial'),
)

VERSION = VersionInfo(0, 3, 2, '', '')
VERSION = VersionInfo(0, 4, 0, '', '')
__version__ = '{0.major}.{0.minor}.{0.micro}{0.releaselevel}'.format(VERSION)
__author__ = 'Alex Hayes'
__contact__ = 'alex@alution.com'
Expand Down
9 changes: 6 additions & 3 deletions eater/api/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ def request(self, **kwargs) -> Model:
this method uses.
"""
kwargs = self.get_request_kwargs(request_model=self.request_model, **kwargs)
method = kwargs.pop('method', self.method)
session = kwargs.pop('session', self.session)

# get_request_kwargs can permanently alter the url, method and session
self.url = kwargs.pop('url', self.url)
self.method = kwargs.pop('method', self.method)
self.session = kwargs.pop('session', self.session)

try:
response = getattr(session, method)(self.url, **kwargs)
response = getattr(self.session, self.method)(self.url, **kwargs)
return self.create_response_model(response, self.request_model)

except requests.Timeout:
Expand Down
92 changes: 77 additions & 15 deletions eater/tests/api/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Tests on :py:mod:`eater.api.http`
"""
from typing import Union

import pytest
import requests
Expand All @@ -17,6 +18,10 @@

from eater import HTTPEater, EaterTimeoutError, EaterConnectError, EaterUnexpectedError

JSON_HEADERS = CaseInsensitiveDict({
'Content-Type': 'application/json'
})


def test_can_subclass():
class PersonAPI(HTTPEater):
Expand Down Expand Up @@ -78,9 +83,7 @@ class PersonAPI(HTTPEater):
mock.get(
api.url,
json=expected_person.to_primitive(),
headers=CaseInsensitiveDict({
'Content-Type': 'application/json'
})
headers=JSON_HEADERS
)

actual_person = api()
Expand Down Expand Up @@ -115,9 +118,7 @@ class UpdatePersonAPI(HTTPEater):
mock.post(
api.url,
json=expected_response.to_primitive(),
headers=CaseInsensitiveDict({
'Content-Type': 'application/json'
})
headers=JSON_HEADERS
)

actual_response = api()
Expand Down Expand Up @@ -148,9 +149,7 @@ class PersonAPI(HTTPEater):
mock.get(
api.url,
json=expected_person.to_primitive(),
headers=CaseInsensitiveDict({
'Content-Type': 'application/json'
})
headers=JSON_HEADERS
)

actual_person = api()
Expand Down Expand Up @@ -198,9 +197,7 @@ class GetPersonAPI(HTTPEater):
mock.get(
expected_url,
json={'name': 'John'},
headers=CaseInsensitiveDict({
'Content-Type': 'application/json'
})
headers=JSON_HEADERS
)
response = api()
assert response.name == 'John'
Expand All @@ -227,14 +224,79 @@ def get_url(self) -> str:
mock.get(
'http://example.com/person/John/',
json={'name': 'John'},
headers=CaseInsensitiveDict({
'Content-Type': 'application/json'
})
headers=JSON_HEADERS
)
response = api()
assert response.name == 'John'


def test_get_request_kwargs_url():
"""
Test that get_request_kwargs can manipulate the url.
"""
class URLManipulatingAPI(HTTPEater):
request_cls = Model
response_cls = Model
url = 'http://not-the-real-url.com/'

def get_request_kwargs(self, request_model: Union[Model, None], **kwargs):
kwargs['url'] = 'http://the-real-url.com'
return kwargs

expected_url = 'http://the-real-url.com'

api = URLManipulatingAPI()

with requests_mock.Mocker() as mock:
mock.get(expected_url, json={}, headers=JSON_HEADERS)
api()
assert api.url == expected_url


def test_get_request_kwargs_method():
"""
Test that get_request_kwargs can manipulate the method.
"""
class MethodManipulatingAPI(HTTPEater):
request_cls = Model
response_cls = Model
url = 'http://example.com/'

def get_request_kwargs(self, request_model: Union[Model, None], **kwargs):
kwargs['method'] = 'post'
return kwargs

api = MethodManipulatingAPI()

with requests_mock.Mocker() as mock:
mock.post(api.url, json={}, headers=JSON_HEADERS)
api()
assert api.method == 'post'


def test_get_request_kwargs_session():
"""
Test that get_request_kwargs can manipulate the session.
"""
class SessionManipulatingAPI(HTTPEater):
request_cls = Model
response_cls = Model
url = 'http://example.com/'

def get_request_kwargs(self, request_model: Union[Model, None], **kwargs):
session = requests.Session()
session.auth = ('john', 's3cr3t')
kwargs['session'] = session
return kwargs

api = SessionManipulatingAPI()

with requests_mock.Mocker() as mock:
mock.get(api.url, json={}, headers=JSON_HEADERS)
api()
assert api.session.auth == ('john', 's3cr3t')


def test_requests_parameter():
class GetPersonAPI(HTTPEater):
request_cls = Model
Expand Down

0 comments on commit 262e928

Please sign in to comment.