Skip to main content

This project allows you to execute a list of http operations asynchronously from within a synchronous context.

Project description

CI Workflow Ruff Poetry PyPI version MIT License

Overview

This project allows you to execute a list of http operations asynchronously from within a synchronous context.

It does not care whether you should do this. It simply allows you to do so if you desire.

Installing

The package is available via pip.

pip install loamy

If you're not on Windows, install the uvloop extra to increase performance.

pip install "loamy[uvloop]"

Usage

The package can be imported as shown:

from loamy.session import Clump, RequestMap, RequestResponse
Class Description
Clump Container object that stores collection of requests (type RequestMap) to send
RequestMap Container object that stores all info about an individual request to send
RequestResponse Container object that stores the request response and any exception raised for each individual request

Example

# Create RequestMap objects
req1 = RequestMap(
    url="https://baconipsum.com/api",
    http_op="GET",
    query_params={"type": "meat-and-filler", "format": "json"},
)
req2 = RequestMap(
    url="https://baconipsum.com/api",
    http_op="GET",
    query_params={"type": "all-meat", "format": "json"},
)
req3 = RequestMap(
    url="https://baconipsum.com/api",
    http_op="GET",
    query_params={"type": "meat-and-filler", "format": "json"},
)

# Create Clump and call send_requests()
session = Clump(requests=[req1, req2, req3])
responses: list[RequestResponse] = session.send_requests(return_exceptions=True)


# Handle responses for individual requests
for resp in responses:
    http_verb = resp.request_map.http_op
    print(f"Evaluating response for {http_verb} request to {resp.request_map.url}")
    if resp.error is not None:
        print("Exception raised for request")
    else:
        print(f"Status Code: {resp.status_code}")
        if resp.body is not None:
            print(resp.body)

RequestMap Class

class RequestMap(msgspec.Struct):
    url: str
    http_op: Literal["GET", "POST", "PUT", "PATCH", "OPTIONS", "DELETE"]
    body: dict | None = None
    query_params: dict[str, str] | None = None
    headers: dict[str, str] | None = None

RequestResponse Class

class RequestResponse(msgspec.Struct):
    request_map: RequestMap
    status_code: int
    body: dict | None = None
    error: BaseException | None = None

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

loamy-0.0.8.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

loamy-0.0.8-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file loamy-0.0.8.tar.gz.

File metadata

  • Download URL: loamy-0.0.8.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for loamy-0.0.8.tar.gz
Algorithm Hash digest
SHA256 6ae5593896620fab13855d41047a0cf1dadace5e398c089f1872570d26970ab3
MD5 5b0ac2e0c8352388866d918de3afcd99
BLAKE2b-256 4e76376526bab5c36e1d31eed654f1c3877ddb183acd732a8ce3dbc050151aa3

See more details on using hashes here.

File details

Details for the file loamy-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: loamy-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for loamy-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a783e0b6d1cc94f5ee30056dbbcaa7801bd89baf3af8bd80dcf3acf6fc1b9ea1
MD5 da2a9525a8193ea05af6c09fb0fd7545
BLAKE2b-256 df3d9ab3c65942409e8b8637d87940232d6bb8b5c5bf983eeb8f9aa867cef813

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