Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Ascender HTTP Client (aschttp)

ascender-httpclient (aschttp) is a powerful and extendable HTTP client for the Ascender Framework. It supports synchronous, asynchronous, and streaming HTTP requests with clean dependency injection, interceptors, and multi-instance configurations.

Features

  • Dependency Injection: Easily inject HTTPClient into any service.
  • Interceptor Support: Create and manage interceptors to customize request handling.
  • Multiple HTTPClient Instances: Use custom HTTP client classes alongside the default singleton.
  • Async and Streaming Requests: Built-in support for asynchronous and streaming HTTP requests.
  • Command Line Integration: Generate interceptors with a simple CLI command.

Installation

To install the package, in your project type:

poetry add ascender-httpclient

Ensure that the Ascender Framework is installed and properly configured.


Initialization

To initialize aschttp, configure ProvideHTTPClient() in your bootstrap.py:

...
from aschttp import ProvideHTTPClient

appBootstrap: IBootstrap = {
    "providers": [
        DatabaseProvider(ORMEnum.SQLALCHEMY, DATABASE_CONNECTION),
        ProvideControllers([
            ControllersModule
        ]),
        ProvideHTTPClient()
    ]
}

Usage

Injecting HTTPClient

To inject the HTTP client into any service:

from ascender.common import Injectable
from ascender.contrib.services import Service
from aschttp import HTTPClient

@Injectable()
class MainService(Service):
    def __init__(self, http: HTTPClient):
        self.http = http

Making Requests

GET Request

from controllers.dtos.test_dto import TestDTO

@Injectable()
class MainService(Service):
    def __init__(self, http: HTTPClient):
        self.http = http
    
    async def make_get_request(self):
        response = await self.http.get(TestDTO, url="http://api.example.com/get")
        assert isinstance(response, TestDTO)
        return response

POST Request

from controllers.dtos.test_dto import TestDTO

@Injectable()
class MainService(Service):
    def __init__(self, http: HTTPClient):
        self.http = http
    
    async def make_post_request(self, dto: TestDTO):
        response = await self.http.post(str, url="http://api.example.com/post", content=dto)
        assert isinstance(response, str)
        return response

Streaming Request

@Injectable()
class MainService(Service):
    def __init__(self, http: HTTPClient):
        self.http = http
    
    def make_streaming_request(self):
        self.http.stream(str, method="GET", url="http://api.example.com/stream").subscribe(
            on_next=lambda r: print(r),
            on_error=lambda err: print(err)
        )

Custom HTTP Clients

To use multiple HTTP client instances:

  1. Define a custom HTTP client class:
from aschttp import HTTPClient

class MyHTTP(HTTPClient):
    pass
  1. Register the custom HTTP client in bootstrap.py:
from myclient import MyHTTP

appBootstrap: IBootstrap = {
    "providers": [
        ProvideHTTPClient(),
        ProvideHTTPClient(client_instance=MyHTTP)
    ]
}

Interceptors

Interceptors allow you to modify HTTP requests before they are sent.

Generate an Interceptor

Use the Ascender CLI to generate an interceptor:

ascender run aschttp:generate interceptor interceptors/custom

Output:

$ CREATE interceptors/custom_interceptor.py (164 bytes)

Implementing an Interceptor

Example custom interceptor:

from httpx import Request
from aschttp.types.interceptors import Interceptor

class CustomInterceptor(Interceptor):
    def __init__(self) -> None:  # for dependency injection, REQUIRED
        pass

    async def handle_request(self, request: Request) -> Request:
        # Modify the request here...
        print("custom interceptor works!")
        return request

Registering an Interceptor

Register the interceptor in bootstrap.py:

from processes.custom_interceptor import MyInterceptor

appBootstrap: IBootstrap = {
    "providers": [
        ProvideHTTPClient(interceptors=[
            MyInterceptor
        ])
    ]
}

Key Concepts

Singleton HTTPClient

The HTTPClient is a singleton by default, ensuring a single instance throughout the framework. However, you can register multiple clients using the ProvideHTTPClient factory, all you need is to change it's client_instance parameter into your custom, example provided in Custom HTTP Clients section.

Interceptors

Interceptors allow request modification before sending. Use them for:

  • Adding headers
  • Logging requests
  • Transforming request payloads

Streaming

The stream() method uses an observable pattern to handle streaming responses efficiently utilizing RxPY.


Command Reference

Generate Interceptor

ascender run aschttp:generate interceptor <path/to/interceptor>

Examples Repository

Find more examples and best practices in the official Ascender Framework examples repository.


Contribution

Contributions are welcome! Submit issues, feature requests, or pull requests via GitHub.


License

The ascender-httpclient package is licensed under the MIT License.

Metadata

Release files for ascender-httpclient 0.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 ascender-httpclient 0.1.0
File Size Uploaded Provenance Actions
ascender_httpclient-0.1.0.tar.gz 5.9 kB Without provenance Details for ascender_httpclient-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ascender-httpclient 0.1.0
File Interpreter ABI Platform Actions
ascender_httpclient-0.1.0-py3-none-any.whl Python 3 none any Details for ascender_httpclient-0.1.0-py3-none-any.whl

Total release size: 13.8 kB

Release files / ascender_httpclient-0.1.0.tar.gz

Download URL ascender_httpclient-0.1.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
dc017ce8da3fbec39c7bd0d7dc46885a5ccd46fd634746c376984ccde734ef51
BLAKE2b-256 checksum
How to use checksums
631f6ff266f6bd0cce68a5333934bd63c44c6d54110518c06d97c96b4cff8729
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.10 Darwin/23.6.0

Release files / ascender_httpclient-0.1.0-py3-none-any.whl

Download URL ascender_httpclient-0.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5029c6aee97935d89c1b542f30f01204fbc4702b977d2c2c7ab97fb8c30f9ad1
BLAKE2b-256 checksum
How to use checksums
dab0249bec5fd23633252f31eaddbb374d43f636cbea746ab74d7cb345180788
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.10 Darwin/23.6.0

Security for ascender-httpclient 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
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