AsyncServiceClient¶
The AsyncServiceClient provides asynchronous HTTP client functionality for interacting with REST APIs.
- class django_api_orm.async_client.AsyncServiceClient(base_url, auth_token=None, timeout=30.0, verify_ssl=True, follow_redirects=True, max_retries=3, http2=False)[source]¶
Bases:
objectAsynchronous HTTP client using httpx.
This client provides async/await interface for making HTTP requests to REST APIs with automatic error handling, authentication, connection pooling, and optional HTTP/2 support.
- Parameters:
base_url (str) – Base URL for the API
auth_token (str | None) – Optional authentication token
timeout (float) – Request timeout in seconds (default: 30.0)
verify_ssl (bool) – Whether to verify SSL certificates (default: True)
follow_redirects (bool) – Whether to follow redirects (default: True)
max_retries (int) – Maximum number of retries for failed requests (default: 3)
http2 (bool) – Enable HTTP/2 support (default: False)
Example
>>> async with AsyncServiceClient(base_url="https://api.example.com") as client: ... response = await client.get("/api/v1/users/") ... print(response.data)
- __init__(base_url, auth_token=None, timeout=30.0, verify_ssl=True, follow_redirects=True, max_retries=3, http2=False)[source]¶
Initialize the async service client.
Example Usage¶
Basic Client¶
from django_api_orm import AsyncServiceClient
# Create a client
client = AsyncServiceClient(
base_url="https://api.example.com",
auth_token="your-token-here"
)
# Use as async context manager (recommended)
async with AsyncServiceClient(base_url="https://api.example.com") as client:
# Client automatically closed on exit
pass
Configuration Options¶
client = AsyncServiceClient(
base_url="https://api.example.com",
auth_token="your-token-here",
timeout=30.0, # Request timeout in seconds
max_retries=3, # Number of retries for failed requests
retry_delay=1.0, # Delay between retries in seconds
http2=True, # Enable HTTP/2 support
max_connections=100, # Maximum number of connections
max_keepalive_connections=20, # Maximum keep-alive connections
headers={"X-Custom-Header": "value"} # Additional headers
)
Making Requests¶
async with AsyncServiceClient(base_url="https://api.example.com") as client:
# GET request
response = await client.get("/users/", params={"active": True})
# POST request
response = await client.post("/users/", data={"name": "Alice"})
# PUT request
response = await client.put("/users/1/", data={"name": "Alice Updated"})
# PATCH request
response = await client.patch("/users/1/", data={"email": "new@example.com"})
# DELETE request
response = await client.delete("/users/1/")
HTTP/2 Support¶
Enable HTTP/2 for better performance:
async with AsyncServiceClient(
base_url="https://api.example.com",
http2=True # Requires httpx[http2]
) as client:
# Requests use HTTP/2 when supported by server
response = await client.get("/users/")
Concurrent Requests¶
Make multiple requests concurrently:
import asyncio
async with AsyncServiceClient(base_url="https://api.example.com") as client:
# Concurrent requests
responses = await asyncio.gather(
client.get("/users/1/"),
client.get("/users/2/"),
client.get("/users/3/")
)
# Process responses
for response in responses:
print(response.data)
Response Object¶
All request methods return a Response object:
response = await client.get("/users/1/")
# Access response data
print(response.status_code) # HTTP status code
print(response.data) # Parsed JSON data (dict or list)
print(response.headers) # Response headers
See Also¶
ServiceClientfor sync operationsAsync Support for async patterns and examples
Models for using the client with models