Skip to main content

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.']

Metadata

Release files for hydrotools.-restclient 4.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hydrotools.-restclient 4.1.0
File Size Uploaded
hydrotools_restclient-4.1.0.tar.gz 31.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hydrotools.-restclient 4.1.0
File Interpreter ABI Platform
hydrotools_restclient-4.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 58.5 kB

Release files / hydrotools_restclient-4.1.0.tar.gz

Download URL hydrotools_restclient-4.1.0.tar.gz
Size 31.6 kB
Tags Source
SHA-256 checksum
How to use checksums
743c58632f2f4a900a5c7b6d81f76673c244d4aa0ba6a10e1b7d1e189fad3cb3
BLAKE2b-256 checksum
How to use checksums
507d3c856c890e067ded6e801753cd297d9bb87d96ee815d08087c8a60a58d87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / hydrotools_restclient-4.1.0-py3-none-any.whl

Download URL hydrotools_restclient-4.1.0-py3-none-any.whl
Size 26.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6160d0909700da0c14272ad63c3a50fd2bdc915efd7a6ad3dacd7f1930872b52
BLAKE2b-256 checksum
How to use checksums
84a6b704ebbed3e1393e86f48363d3e9e4a353240bc59504d6250fccc925350e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

4.1.0 This release

2 release files

4.0.0

2 release files

3.1.1

2 release files

3.1.0

1 release file

3.0.5

2 release files

3.0.4

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page