-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5a444f
commit cae232b
Showing
5 changed files
with
55 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### Define a base URL for all your endpoints only once | ||
...using `BaseUrlSession` from [requests_toolbelt](https://toolbelt.readthedocs.io/) package |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Create your own data parser | ||
|
||
To convert your payloads and responses to your favourite format. A typical use case for this could be to deserialize | ||
responses as python classes. It may be a good place to convert from snake case and camelCase (and viceversa). | ||
|
||
In this library we have a basic example using NamedTuples: `NamedTupleParser`. | ||
|
||
```python | ||
from api_client_framework.protocols import Parser | ||
from typing import Type, NamedTuple, Any | ||
|
||
|
||
class NamedTupleParser(Parser): | ||
def __init__(self, named_tuple_class: Type[NamedTuple]): | ||
self.named_tuple_class = named_tuple_class | ||
|
||
def to_dict(self, instance: NamedTuple) -> dict: | ||
try: | ||
return instance._asdict() | ||
except AttributeError: | ||
return instance | ||
|
||
def to_class(self, dictionary: dict) -> Any: | ||
return self.named_tuple_class(**dictionary) | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### Convert HTTP exceptions in your custom exceptions | ||
|
||
Usually it's a good idea to hide implementation details between layers. We can convert `requests` exceptions | ||
to our custom exceptions by creating an `ExceptionHandler`. | ||
|
||
```python | ||
from api_client_framework.protocols import ExceptionHandler | ||
|
||
|
||
class MyCustomPermissionDenied(Exception): | ||
... | ||
|
||
|
||
class CustomExceptionHandler(ExceptionHandler): | ||
def handle_exception(self, exception, response): | ||
# Convert this specific error to our custom error | ||
if exception.status_code == 400 and response.data["error_code"] == "permission_denied": | ||
raise MyCustomPermissionDenied() | ||
|
||
# Keep other exceptions untouched | ||
raise exception | ||
``` |
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
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