api-wrapper-boilerplate/README.md

35 lines
1.4 KiB
Markdown
Raw Normal View History

2023-08-26 16:15:48 +00:00
# API Wrapper Boilerplate
2023-08-26 16:25:45 +00:00
## Example Code
The example code in `main.py` uses the [JSONPlaceholder](https://jsonplaceholder.typicode.com/) demo API, which
is a great resource for learning how to work with JSON based REST APIs.
The example code also demonstrates how the wrapper will raise exceptions by initializing the `ApiWrapper` with
a broken proxy.
## Development
The ApiWrapper class includes private class convenience methods that utilize the internal `requests.Session`
object to persist session data across all API requests. When developing public methods, use the appropriate
convenience methods:
- `self.__get`
- `self.__post`
- `self.__put`
2023-09-09 01:30:54 +00:00
- `self.__patch`
2023-08-26 16:25:45 +00:00
- `self.__delete`
## Error Handling
The ApiWrapper class defines custom exceptions for handling API request and response errors.
2023-08-26 16:26:26 +00:00
- `ApiRequestException` is raised when there is an issue with the API request.
- `InvalidApiResponse` is raised when the API response is not a valid JSON.
2023-08-26 16:25:45 +00:00
You can catch these exceptions and handle them accordingly in your code.
2023-09-09 01:29:06 +00:00
## Debugging
To turn on debugging, initialize the wrapper with `debug=True`. This will set the log level of
2023-09-09 01:33:09 +00:00
`urllib3` to `logging.DEBUG`.
If a global logger is not configured, a logger will be initialized with the basic configuration:
```
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): jsonplaceholder.typicode.com:443
DEBUG:urllib3.connectionpool:https://jsonplaceholder.typicode.com:443 "GET /users HTTP/1.1" 200 None
```