Skip to main content

A

Project description

Python Rest API Client

pypi coverage


Glues pydantic and httpx to provide a simple REST API client, with dynamic generated methods. It supports both synchronous and asynchronous formats.

Roadmap:

  • Adds Authentication support
  • Auto generate models from OpenAPI (3) Spec
  • Export generated source code

Usage

CHUCK_BASE_URL = "https://api.chucknorris.io/jokes"

# Imports
from pydantic import BaseModel, HttpUrl
from rest_api_client.lib import RestAPI, Endpoint, HTTPMethod
import httpx

# Optionally declare your model classes
class JokeModel(BaseModel):
    id: str
    created_at: str
    updated_at: str
    icon_url: str
    categories: list
    url: str
    value: str


# Declare your API endpoints

endpoints = [

    # Fully descriptive declaration.
    Endpoint(
        name="get_joke",
        path="/random",
        method=HTTPMethod.GET,
        model=JokeModel,
        query_parameters=[("category", str)],
    ),

    # No model provided, result comes back as a dictionary.
    Endpoint(
        name="get_categories",
        path="/categories",
        method=HTTPMethod.GET,
    ),
    
    # Omit HTTP Method, it gets inferred from the endpoint name.
    Endpoint(name="get_search", path="/search", query_parameters=[("query", str)]),
]

# Instantiate your HTTP client session. Should also work with requests

with httpx.Client() as client:
    api = RestAPI(api_url=CHUCK_BASE_URL, driver=client)
    api.register_endpoints(endpoints)

    joke = api.call_endpoint("get_joke")
    joke2 = api.get_joke()

    categories = api.get_categories()

    search = api.get_search(query="something")

    print(joke)
    print(joke2)
    print(search)

Another example for pastry (see integration tests).

api = RestAPI(
    api_url="https://getpantry.cloud/apiv1",
    driver=client,
    # One can also pass endpoints to the constructor
    endpoints=[
        Endpoint(name="get_pantry", path="/pantry/{pantry_id}"),
        Endpoint(
            name="get_basket",
            # Path parameters can be provided in the format {variable_name}
            path="/pantry/{pantry_id}/basket/{basket_id}",
        ),
        Endpoint(
            # 'create_' is interpreted as POST
            name="create_basket",
            path="/pantry/{pantry_id}/basket/{basket_id}",
        ),
        Endpoint(
            # 'update_' is interpreted as PUT
            name="update_basket",
            path="/pantry/{pantry_id}/basket/{basket_id}",
        ),
        Endpoint(
            name="delete_basket",
            path="/pantry/{pantry_id}/basket/{basket_id}",
        ),
    ],
)
pantries = api.get_pantry(pantry_id="123")

# Create/update/patch methods have an additional parameter called data
# Which is passed as the BODY of the request
api.create_basket(pantry_id="123", basket_id="234", data={"key": "value"})
api.update_basket(pantry_id="123", basket_id="234", data={"key2": "value2"})

basket = api.get_basket(pantry_id="123", basket_id="234")
api.delete_basket(pantry_id="123", basket_id="234")

If pantry supported PATCH, we would declare the endpoint as:

Endpoint(
    name="patch_basket",  # No alias for patch exists
    path="/pantry/{pantry_id}/basket/{basket_id}",
),

Async methods

By default, async methods are created with the prefix _async. For instance:

await api.async_get_pantry(pantry_id="123")
await api.async_create_basket(
    pantry_id="123", basket_id="234", data={"key": "value"}
)
await api.async_update_basket(
    pantry_id="123", basket_id="234", data={"key2": "value2"}
)
await api.async_get_basket(pantry_id="123", basket_id="234")
await api.async_delete_basket(pantry_id="123", basket_id="234")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-rest-api-client-0.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

py_rest_api_client-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file py-rest-api-client-0.1.0.tar.gz.

File metadata

  • Download URL: py-rest-api-client-0.1.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.2

File hashes

Hashes for py-rest-api-client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 371d54223e2ac8fcbb80215848a734a4aa640fcf713bd1997d015f2a1408c406
MD5 3c76a59afb1ba43fc18bc968ca73fc5d
BLAKE2b-256 b6e3bed636248f76a69adbe24b2fe82c18128a6d523a3e9c4408ee315207b750

See more details on using hashes here.

File details

Details for the file py_rest_api_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_rest_api_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.2

File hashes

Hashes for py_rest_api_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47290ccc6faee1015cad453b7d1bb36b5d2d6c27915785e71e510c145c0aa868
MD5 730aebb5a19caa6995f3f9439199b58e
BLAKE2b-256 84c09a043ef20940963dcb94b5f98f875904f69f5b076111683445052c76f1dc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page