Skip to main content

Async HTTP client with clean API and automatic error handling

Project description

astra-client

Async HTTP client for Python with clean API and automatic error handling

PyPI version Python License: MIT CI


Key Features

  • Zero-boilerplate URLs — write "google.com" instead of "https://google.com"
  • Automatic error handling — 4xx/5xx responses raise typed exceptions, no manual checks needed
  • Async context managers — clean resource lifecycle with async with
  • Full HTTP verb coverage — GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
  • Typed exception hierarchy — catch exactly what you need, from NotFoundError to AstraHttpError
  • Python 3.10+ — modern type hints throughout (X | None, dict[str, Any])

Installation

pip install astra-client
uv add astra-client

Quick Start

import asyncio
import astra

async def main():
    async with astra.ClientSession() as session:
        async with session.get("httpbin.org/get") as response:
            print(response.status)
            data = await response.json()
            print(data)

asyncio.run(main())

No scheme? No problem. astra-client automatically prepends https:// if missing.

POST with JSON

async with astra.ClientSession() as session:
    async with session.post(
        "httpbin.org/post",
        json={"name": "astra", "fast": True},
    ) as response:
        print(await response.json())

Custom headers & base URL

async with astra.ClientSession(
    base_url="https://api.example.com",
    headers={"Authorization": "Bearer TOKEN"},
    timeout=10.0,
) as session:
    async with session.get("/users/me") as response:
        user = await response.json()

Error Handling

astra-client raises typed exceptions automatically — no need to call raise_for_status() manually.

import astra

async def fetch():
    try:
        async with astra.ClientSession() as session:
            async with session.get("httpbin.org/status/404") as response:
                ...
    except astra.NotFoundError:
        print("Resource not found")
    except astra.UnauthorizedError:
        print("Invalid credentials")
    except astra.TooManyRequestsError:
        print("Rate limit hit — slow down")
    except astra.TimeoutError:
        print("Request timed out")
    except astra.ConnectionError:
        print("Could not reach the server")
    except astra.AstraHttpError as e:
        print(f"Unexpected error: {e}")

Exception Hierarchy

AstraHttpError
├── NetworkError
│   ├── ConnectionError
│   ├── TimeoutError
│   ├── TooManyRedirectsError
│   └── InvalidURLError
└── HTTPStatusError
    ├── ClientError          (4xx)
    │   ├── BadRequestError          400
    │   ├── UnauthorizedError        401
    │   ├── ForbiddenError           403
    │   ├── NotFoundError            404
    │   ├── MethodNotAllowedError    405
    │   ├── UnprocessableEntityError 422
    │   └── TooManyRequestsError     429
    └── ServerError          (5xx)
        ├── InternalServerError      500
        ├── BadGatewayError          502
        ├── ServiceUnavailableError  503
        └── GatewayTimeoutError      504

Response API

async with session.get("httpbin.org/json") as response:
    response.status        # int — HTTP status code
    response.headers       # httpx.Headers

    text = await response.text()   # str
    data = await response.json()   # Any
    raw  = await response.read()   # bytes

Requirements

  • Python 3.10+
  • httpx >= 0.28.1

License

astra-client is released under the MIT License.

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

astra_client-0.1.1.tar.gz (37.4 kB view details)

Uploaded Source

Built Distribution

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

astra_client-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file astra_client-0.1.1.tar.gz.

File metadata

  • Download URL: astra_client-0.1.1.tar.gz
  • Upload date:
  • Size: 37.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for astra_client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7beb1f79428f6b1aa3cf31525765ded411560056b25813812e5a135e33c96338
MD5 7f86a99e0df17594b36d298b3e686fbd
BLAKE2b-256 92c27915d9b30bb1a4c0ba79e093ca3be88ac230bb8ebef04584278dbfb8831d

See more details on using hashes here.

File details

Details for the file astra_client-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: astra_client-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for astra_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4dba8ee0de37b832c413b470011b0e0067d30d809c37444e8672d10811e1e79
MD5 758ad83afc385148c156a94c8a579919
BLAKE2b-256 10d6d27b0c31f95564d4f86161df3ffa10fb8ca5d11cfcdfcf87cc7982fc88dc

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