Skip to main content

Simple Pydantic AIOHTTP Client Sessions

Project description

SPACS: Simple Pydantic AIOHTTP Client Sessions

A package to assist in managing and using long-lived AIOHTTP client sessions with simplicity. Built to handle Pydantic objects.

Features

  • Handles request params and bodies as either Pydantic objects or native Python dictionaries, converting items to JSON-safe format.
  • Abstracts away internals of managing the request/response objects, instead either returning parsed response content on success, or raising a specialized error object.
  • Automatically manages persistent connections to be shared over extended lifespan across application, cleaning up all open connections on teardown.
  • Utilizes modern Python type hinting.

Installation

Using poetry (preferred):

poetry add spacs

Using pip:

pip install spacs

Usage

from spacs import SpacsClient, SpacsRequest, SpacsRequestError
from pydantic import BaseModel

...

example_client = SpacsClient(base_url="http://example.com")

# Basic request with error handling
try:
    request = SpacsRequest(path="/fruit/apple", params={"cultivar": "honeycrisp"})
    apples = await example_client.get(request)
except SpacsRequestError as error:
    print({"code": error.status_code, "reason": error.reason})

# Sending Pydantic objects via HTTP POST
class MyModel(BaseModel):
    name: str
    age: int

example_object = MyModel(name="James", age=25)
request = SpacsRequest(path="/person", body=example_object, response_model=MyModel)
created_person = await example_client.post(request)

# Manually closing a session
await example_client.close()
# Alternatively, to close all open sessions:
await SpacsClient.close_all()

Building

poetry build

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

spacs-0.0.7.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spacs-0.0.7-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file spacs-0.0.7.tar.gz.

File metadata

  • Download URL: spacs-0.0.7.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Windows/10

File hashes

Hashes for spacs-0.0.7.tar.gz
Algorithm Hash digest
SHA256 f94a5d612e01b2a34b6e524fde86f4d6b531c881db56ad12ae04cf8aea225232
MD5 bf9af3427d87753c30cc2bf5e6f1fa40
BLAKE2b-256 b41eb243a3beed0e0eeea54726a76af2e7f117d88edc83ed2973de7f3b9a13d6

See more details on using hashes here.

File details

Details for the file spacs-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: spacs-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.1 Windows/10

File hashes

Hashes for spacs-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 69d9b0c5d92c6bbe831e9697886e5533639f480a7db8d7a9e2658ee372ce5b54
MD5 883c1481219d28e3b4596e59b86c4e86
BLAKE2b-256 8b56315b9eb3eee68dd65630679de2ab6e0804dc034043da5f026c74be045beb

See more details on using hashes here.

Supported by

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