ServiceClient¶
The ServiceClient provides synchronous HTTP client functionality for interacting with REST APIs.
- class django_api_orm.client.ServiceClient(base_url, auth_token=None, timeout=30.0, verify_ssl=True, follow_redirects=True, max_retries=3)[source]¶
Bases:
objectSynchronous HTTP client using httpx.
This client provides a simple interface for making HTTP requests to REST APIs with automatic error handling, authentication, and connection pooling.
- 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)
Example
>>> with ServiceClient(base_url="https://api.example.com") as client: ... response = 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)[source]¶
Initialize the service client.
Example Usage¶
Basic Client¶
from django_api_orm import ServiceClient
# Create a client
client = ServiceClient(
base_url="https://api.example.com",
auth_token="your-token-here"
)
# Use as context manager (recommended)
with ServiceClient(base_url="https://api.example.com") as client:
# Client automatically closed on exit
pass
Configuration Options¶
client = ServiceClient(
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
max_connections=100, # Maximum number of connections
max_keepalive_connections=20, # Maximum keep-alive connections
headers={"X-Custom-Header": "value"} # Additional headers
)
Making Requests¶
with ServiceClient(base_url="https://api.example.com") as client:
# GET request
response = client.get("/users/", params={"active": True})
# POST request
response = client.post("/users/", data={"name": "Alice"})
# PUT request
response = client.put("/users/1/", data={"name": "Alice Updated"})
# PATCH request
response = client.patch("/users/1/", data={"email": "new@example.com"})
# DELETE request
response = client.delete("/users/1/")
Response Object¶
All request methods return a Response object:
response = 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¶
AsyncServiceClientfor async operationsModels for using the client with models