Skip to main content

apiwrappers is a library for building API clients that work both with regular and async code

Project description

Build Status Documentation Status Coverage Status Checked with mypy PyPI Package latest release Supported versions MIT License

apiwrappers is a library for building API clients that work both with regular and async code.

Features

  • DRY - support both regular and async code with one implementation

  • Flexible - middleware mechanism to customize request/response

  • Typed - library is fully typed and it’s relatively easy to get fully typed wrappers

  • Modern - decode JSON with no effort using dataclasses and type annotations

  • Unified interface - work with different python HTTP client libraries in the same way. Currently supported:

Installation

pip install 'apiwrappers[aiohttp,requests]'

Note: extras are mainly needed for the final user of your API client

QuickStart

Making request is rather straightforward:

from dataclasses import dataclass
from typing import List

from apiwrappers import Request, fetch, make_driver

@dataclass
class Repo:
    name: str

url = "https://api.github.com/users/unmade/repos"
request = Request("GET", url)

driver = make_driver("requests")
fetch(driver, request)  # Response(..., status_code=200, ...)
fetch(driver, request, model=List[Repo])  # [Repo(name='am-date-picker'), ...]

driver = make_driver("aiohttp")
await fetch(driver, request)  # Response(..., status_code=200, ...)
await fetch(driver, request, model=List[Repo])  # [Repo(name='am-date-picker'), ...]

Writing a Simple API Client

With apiwrappers you can bootstrap clients for different API pretty fast and easily.

Here is how a typical API client would look like:

from __future__ import annotations

from dataclasses import dataclass
from typing import Awaitable, Generic, List, TypeVar, overload

from apiwrappers import AsyncDriver, Driver, Request, Url, fetch

T = TypeVar("T", Driver, AsyncDriver)


@dataclass
class Repo:
    id: int
    name: str


class GitHub(Generic[T]):
    def __init__(self, host: str, driver: T):
        self.url = Url(host)
        self.driver: T = driver

    @overload
    def get_repos(self: Github[Driver], username: str) -> List[Repo]:
        ...

    @overload
    def get_repos(self: Github[AsyncDriver], username: str) -> Awaitable[List[Repo]]:
        ...

    def get_repos(self, username: str):
        url = self.url("/users/{username}/repos", username=username)
        request = Request("GET", url)
        return fetch(self.driver, request, model=List[Repo])

This is small, but fully typed, API client for one of the api.github.com endpoints to get all user repos by username:

Here we defined Repo dataclass that describes what we want to get from response and pass it to the fetch function. fetch will then make a request and will cast response to that type.

Note how we create URL:

url = self.url("/users/{username}/repos", username=username)

Sometimes, it’s useful to have an URL template, for example, for logging or for aggregating metrics, so instead of formatting immediately, we provide a template and replacement fields.

Using the API Client

Here how we can use it:

>>> from apiwrappers import make_driver
>>> driver = make_driver("requests")
>>> github = GitHub("https://api.github.com", driver=driver)
>>> github.get_repos("unmade")
[Repo(id=47463599, name='am-date-picker'),
 Repo(id=231653904, name='apiwrappers'),
 Repo(id=144204778, name='conway'),
 ...
]

To use it with asyncio all we need to do is provide a proper driver and don’t forget to await method call:

Use IPython or Python 3.8+ with python -m asyncio to try this code interactively

>>> from apiwrappers import make_driver
>>> driver = make_driver("aiohttp")
>>> github = GitHub("https://api.github.com", driver=driver)
>>> await github.get_repos("unmade")
[Repo(id=47463599, name='am-date-picker'),
 Repo(id=231653904, name='apiwrappers'),
 Repo(id=144204778, name='conway'),
 ...
]

Documentation

Documentation for apiwrappers can be found at Read The Docs.

Check out Extended Client Example.

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

See contributing guide to learn more.

Currently the code and the issues are hosted on GitHub.

The project is licensed under MIT.

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

apiwrappers-0.4.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

apiwrappers-0.4.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file apiwrappers-0.4.0.tar.gz.

File metadata

  • Download URL: apiwrappers-0.4.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.0.0-1032-azure

File hashes

Hashes for apiwrappers-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9f4cde22914d83b83654fe3c9273216292b7b59fa57b92f01e76faf06a4fa97b
MD5 8e206d5c9578146a5e959e9d378e5b1b
BLAKE2b-256 c93db52c01dbc73c675a4b67365b129e71d43b8d6c9751472cfa2fff1cf4c516

See more details on using hashes here.

File details

Details for the file apiwrappers-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: apiwrappers-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.2 Linux/5.0.0-1032-azure

File hashes

Hashes for apiwrappers-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 130e93c2669e91be10fb8f1c2c253b4e099422b59f6c25ac254848dfae5bae7b
MD5 0519325fd42b272573372c014cba790d
BLAKE2b-256 e9d64c6529e49b3f11d3bf61a923723d8e6d8f878e0efa7ba391eca807056c17

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