Created
January 20, 2015 21:27
-
-
Save nrjones8/ce960b9436ecb60e613c to your computer and use it in GitHub Desktop.
A few examples of how a client would interact with ApiClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MatchedResponseCode(object): | |
def __init__(self, expected_status_codes, status_code): | |
self.expected_status_codes = expected_status_codes | |
self.status_code = status_code | |
self.codes_handled = set() | |
def __enter__(self): | |
return self | |
def __exit__(self, exc_type, exc_value, exc_tb): | |
# Note that if an exception happened in the `with` statement, then it's reraised | |
if exc_value is None and self.unhandled_status_code(): | |
raise Exception('You never handled the status code ({code}) you received!'.format( | |
code=self.status_code | |
) | |
) | |
def code_equals(self, code): | |
if code in self.expected_status_codes: | |
self.codes_handled.add(code) | |
return code == self.status_code | |
else: | |
raise Exception('Attempting to handle an unexpected status code ({code})'.format( | |
code=code) | |
) | |
def unhandled_status_code(self): | |
""" | |
Returns whether or not we've handled the actual status code of this response | |
""" | |
return self.status_code not in self.codes_handled | |
class Response(object): | |
""" | |
Tiny fake implementation of what an Response class might look like | |
""" | |
def __init__(self, status_code, expected_status_codes, body): | |
self.status_code = status_code | |
self.body = body | |
self.expected_status_codes = expected_status_codes | |
def matched_status_code(self): | |
return MatchedResponseCode(self.expected_status_codes, self.status_code) | |
def request(expected_status_codes, status_code_to_return): | |
""" | |
Returns a Response object with status of `status_code_to_return` | |
""" | |
return Response(status_code_to_return, expected_status_codes, 'body of response') | |
def example_with_actual(actual): | |
response = request([201, 401, 504], actual) | |
# We passed 504 above, but don't handle it here | |
with response.matched_status_code() as status: | |
if status.code_equals(201): | |
print('Handled 201') | |
elif status.code_equals(401): | |
print('Handled 401') | |
print('Exited context successfully') | |
print('-' * 30) | |
# Other case is: we didn't pass some status to the `request`, but we try to handle it | |
def example_with_unpassed_status(actual): | |
response = request([201], actual) | |
# We try to handle a 404 below, but it wasn't passed above | |
with response.matched_status_code() as status: | |
if status.code_equals(404): | |
print('Handled a 404, but we did not mean to!') | |
elif status.code_equals(201): | |
print('Handled 201') | |
# Should work fine, and print that it was handled | |
example_with_actual(201) | |
example_with_actual(401) | |
# Both should raise Exceptions | |
example_with_actual(504) | |
example_with_unpassed_status(201) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment