Skip to main content

General REST api client with built in request caching and retries.

Project description

OWPHydroTools :: REST Client

Subpackage of objects and utilities suitable for quickly developing robust REST api libraries.

Main features:

  • RestClient: a class which offers asynchronous performance via a convenient serial wrapper, sqlite database GET request caching, GET request backoff, batch GET requests, and flexible url argument encoding.

  • Url: Treat urls like to pathlib.Paths. Supports appending paths with / and appending url query parameters with +.

  • ClientSession: Extension of aiohttp_client_cache that adds exponential backoff.

  • Variadic: Join list or tuple of objects on some delimiter. Useful when query parameters utilize a non-key=value&key=value2 pattern.

  • Alias and AliasGroup: Immutable utility classes that simplify common API library development patterns (i.e. separation of backend uris from front end interfaces).

See the REST Client Documentation for a complete list and description of the currently available methods. To report bugs or request new features, submit an issue through the OWPHydroTools Issue Tracker on GitHub.

Installation

In accordance with the python community, we support and advise the usage of virtual environments in any workflow using python. In the following installation guide, we use python's built-in venv module to create a virtual environment in which the tool will be installed. Note this is just personal preference, any python virtual environment manager should work just fine (conda, pipenv, etc. ).

# Create and activate python environment, requires python >= 3.8
$ python3 -m venv venv
$ source venv/bin/activate
$ python3 -m pip install --upgrade pip

# Install _restclient
$ python3 -m pip install hydrotools._restclient

Usage

The following example demonstrates how one might use hydrotools._restclient to write a simple REST api interface.

Code

from hydrotools import _restclient


class BaconIpsum:
    """ Python API to baconipsum.com. """

    _base_url = "https://baconipsum.com/api/"

    # Return "meat-and-filler" if "mixed-meat" or "mixed" passed like:
    # _meat_and_filler["mixed-meat"] or _meat_and_filler.get("mixed")
    _meat_and_filler = _restclient.Alias("meat-and-filler", ["mixed-meat", "mixed"])
    _all_meat = _restclient.Alias("all-meat", ["all-meat", "all"])

    # Treat multiple alias as one alias
    _meat_options = _restclient.AliasGroup([_meat_and_filler, _all_meat])

    def __init__(self):
        self._restclient = _restclient.RestClient(
            self._base_url,
            cache_filename="bacon_ipsum_cache",
            cache_expire_after=43200, # in seconds
            n_retries=5,
        )

    @classmethod
    def get(cls, type: str):
        """
        Get either 'mixed-meat' / 'mixed' or 'all-meat' / 'all'
        Mixed contains filler 'lorem ipsum' text. All is just the meats!
        """
        inst = cls()

        params = {"type": inst._meat_options[type], "sentences": 1, "format": "json"}

        response = inst._restclient.get(parameters=params)
        return response.json()


if __name__ == "__main__":
    print(BaconIpsum.get("all-meat"))

    print(BaconIpsum.get("mixed"))

Output

['Short loin tongue drumstick chuck beef meatball brisket ball tip pork loin doner kielbasa flank.']
['Andouille excepteur in, shankle irure laboris voluptate et ad incididunt.']

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

hydrotools_restclient-3.1.1.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

hydrotools._restclient-3.1.1-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file hydrotools_restclient-3.1.1.tar.gz.

File metadata

  • Download URL: hydrotools_restclient-3.1.1.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.10

File hashes

Hashes for hydrotools_restclient-3.1.1.tar.gz
Algorithm Hash digest
SHA256 a8b80bc8eff5b8cae532f973526f6a16b8f5f1e254a7fb8dfe2cbe2d5b9edcdc
MD5 f90e7d45bb59a4aaab214a0c09cbd87b
BLAKE2b-256 b18ab8ebbd05bf1d9081484504094d7b90084e0349e6de7cf2af64a16814592a

See more details on using hashes here.

File details

Details for the file hydrotools._restclient-3.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for hydrotools._restclient-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89b1ed72cd9255def2c91716da8ef003204a95fa79f8d67c7a2f749c35b91902
MD5 44538f52a04f3535a890b3d846a0c019
BLAKE2b-256 f206c68b4e8e887e2070a651c31b897734cf92d2d605df5a7a154a2258bd81ff

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