Class-based helper for constructing API clients
Project description
API Consumer
Create clients for consuming endpoints in a class-based way.
Describe the endpoints using a class
from api_client_framework.requests import RequestsEndpoint
from api_client_framework.requests import Methods
from api_client_framework.parsers import NamedTupleParser
from collections import namedtuple
User = namedtuple("User", ["id", "uid", "password","first_name", ...], rename=True)
class UsersEndpoint(RequestsEndpoint):
method = Methods.GET
url = "https://random-data-api.com/api/v2/users"
params = {"response_type": "json"}
parser = NamedTupleParser(User)
Create your client
import requests
from examples.random_data_api.endpoints import UsersEndpoint
from examples.random_data_api.models import User
from api_client_framework.requests import RequestsClient
class RandomDataAPI(RequestsClient):
"""Client for random-data-api.com"""
def __init__(self):
self.session = requests.Session()
def get_user(self) -> User:
"""Retrieve a single random user"""
return self._perform_request(UsersEndpoint())
Advanced
Create your own data parsers
...using Parser
protocol.
Convert HTTP exceptions in your custom exceptions
...by creating your own exception_handler
Define a base URL for all your endpoints only once
...using BaseUrlSession
from requests_toolbelt package
Log responses
...by adding a hook in your requests.Session
instance
def print_response(response, *args, **kwargs):
print(response.url)
session = requests.Session()
session.hooks.setdefault("response", [])
session.hooks["response"].append(print_response)
Examples
There's a small example in this repo, under examples/random_data_api
, which implements some endpoints of random-data-api.com
Made with python-api-consumer
- PyWegowAPI - A client for the public, undocumented, Wegow API
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for api_client_framework-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67561ac74cce9856bad91f6c40211dcecc2868b89beb5f55cff15d84466ab184 |
|
MD5 | 9d44192ec9c00b48c92cfdff938055e7 |
|
BLAKE2b-256 | 2fa6f0cb3da419adde66a01e15b2bd2c809f14f5f0c72ea68eb8f0df986e25ef |
Close
Hashes for api_client_framework-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e42f6e9c9ea83c772eb360b955ca492cbc59efa8877b4844449680c045e64561 |
|
MD5 | 36ade3dc62e3b396be7ab3a4bf920100 |
|
BLAKE2b-256 | cdcdf870c63522b2b54dabbd50f555dd269a9d2a8f8794946035ec6b5b2e444b |