服务层¶
CORSMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware that adds permissive CORS headers to all responses.
Source code in aloha/service/http/plain_http_handler.py
DefaultHandler404
¶
Default 404 response handler for FastAPI services.
Source code in aloha/service/handlers.py
handle(request=None)
async
¶
Return a JSON response for unmatched routes.
Source code in aloha/service/handlers.py
Service application bootstrap utilities for FastAPI.
Application
¶
Bootstrap and run an aloha FastAPI web service.
Source code in aloha/service/app.py
__init__(*args, **kwargs)
¶
start()
¶
Start the FastAPI app using uvicorn.
Source code in aloha/service/app.py
FastAPI web application assembly for aloha services.
FastAPIApplication
¶
FastAPI application that loads routes from configured service modules.
Source code in aloha/service/web.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
Base HTTP client helpers for aloha API clients using httpx.
AbstractApiClient
¶
Bases: ABC
Common client behavior for aloha HTTP APIs using httpx.
Source code in aloha/service/http/base_api_client.py
__init__(url_endpoint=None, *args, **kwargs)
¶
Store the endpoint used by the client.
call(api_url, data=None, timeout=5, **kwargs)
¶
Call a remote API and return the parsed JSON response (sync wrapper).
Source code in aloha/service/http/base_api_client.py
get_headers(*args, **kwargs)
¶
Build the default request headers used by aloha clients.
get_http_client(total_retries=3, *args, **kwargs)
¶
Create an httpx async client with retry support via custom transport.
Source code in aloha/service/http/base_api_client.py
wrap_request_data(data)
abstractmethod
¶
Transform the request payload before sending it.
Base FastAPI dependencies and request helpers for aloha services.
AbstractApiHandler
¶
Bases: ABC
Shared request parsing and response helpers for JSON APIs.
This is a base class that provides utility methods for API handlers. Subclasses should inherit from this and implement the response() method.
Source code in aloha/service/http/base_api_handler.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
request_body
property
¶
Parse the request body as JSON or multipart form data.
request_header_content_type
property
¶
Return the request content type with a JSON default.
request_id
property
¶
Return or create a request identifier for tracing.
request_param
property
¶
Parse query/body arguments into a JSON-friendly dict.
__init__()
¶
Initialize request state used by subclasses.
Source code in aloha/service/http/base_api_handler.py
finish(data, status_code=200)
¶
Create a JSON response with proper content type.
Source code in aloha/service/http/base_api_handler.py
get_request_files()
¶
response(*args, **kwargs)
¶
set_header(key, value)
¶
create_handler_route(handler_class)
¶
Create a FastAPI route wrapper for a handler class.
Source code in aloha/service/http/base_api_handler.py
FastAPI middleware and dependencies with permissive CORS defaults.
CORSMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware that adds permissive CORS headers to all responses.
Source code in aloha/service/http/plain_http_handler.py
CORSResponse
¶
Bases: JSONResponse
JSON response with permissive CORS headers for simple APIs.
Source code in aloha/service/http/plain_http_handler.py
add_cors_headers(response)
¶
Add permissive CORS headers to a response.
Source code in aloha/service/http/plain_http_handler.py
Helpers for handling multipart upload files and remote file inputs using httpx.
iter_over_request_files(request, url_files)
async
¶
Yield uploaded files and optional remote files as normalized tuples.
Each yielded item is (field_name, file_name, content_type, body_bytes).
Files can come from multipart form uploads or from URLs listed in
url_files.
Args: request: FastAPI request object with files attribute url_files: List of URLs to download files from
Source code in aloha/service/http/files.py
iter_over_request_files_sync(request, url_files)
¶
Synchronous version of iter_over_request_files for backward compatibility.
This is a sync wrapper that uses httpx sync client.
Source code in aloha/service/http/files.py
Client helper for OpenAPI-style services protected by tokens.
OpenApiClient
¶
Simple HTTP client that acquires and caches an access token.
Source code in aloha/service/openapi/client.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
__init__(url_oauth_get_token, client_id, client_secret, grant_type='client_credentials')
¶
Store OAuth-style client credentials and token endpoint.
Source code in aloha/service/openapi/client.py
get(url_api, body, headers=None, timeout=5)
¶
Send a GET request to the remote API.
Source code in aloha/service/openapi/client.py
get_access_token()
¶
Fetch or refresh the cached access token.
Source code in aloha/service/openapi/client.py
get_request_session(total_retries=10, *args, **kwargs)
classmethod
¶
Create a retry-enabled requests session.
Source code in aloha/service/openapi/client.py
post(url_api, body, headers=None, timeout=5)
¶
Send a POST request to the remote API.
Source code in aloha/service/openapi/client.py
Version 0 JSON API helpers for FastAPI.
This module defines the simplest request/response protocol used by aloha:
request bodies are passed directly to the handler method and the response is
serialized as a JSON object with a code and message field.
APICaller
¶
Bases: AbstractApiClient
Client helper for v0 endpoints.
The payload is sent as-is, without signature wrapping or token exchange.
Source code in aloha/service/api/v0.py
APIHandler
¶
Bases: AbstractApiHandler, ABC
Base handler for v0 JSON endpoints using FastAPI.
Subclasses implement :meth:response, which receives parsed request data
and returns a Python object that can be JSON-serialized.
Source code in aloha/service/api/v0.py
get(*args, **kwargs)
async
¶
Handle GET request (useful for some v0 endpoints).
Source code in aloha/service/api/v0.py
post(*args, **kwargs)
async
¶
Parse the request body, call :meth:response, and return JSON.
Source code in aloha/service/api/v0.py
create_v0_router(handler_class)
¶
Create FastAPI routes for a v0 API handler class.
Args: handler_class: A class inheriting from APIHandler
Returns: A function that registers routes on a FastAPI app
Source code in aloha/service/api/v0.py
Version 1 signed JSON API helpers for FastAPI.
Version 1 adds request signing with app_id, salt_uuid, and sign fields.
Handlers validate the signature before dispatching to the service logic.
APICaller
¶
Bases: AbstractApiClient
Client helper that wraps payloads with v1 signing metadata.
Source code in aloha/service/api/v1.py
wrap_request_data(data, app_id=None, app_key=None, salt_uuid=None, sign=None, sign_method=None)
¶
Wrap the payload with signature fields expected by v1 handlers.
Source code in aloha/service/api/v1.py
APIHandler
¶
Bases: AbstractApiHandler, ABC
Signed API handler for v1 endpoints.
Source code in aloha/service/api/v1.py
post()
async
¶
Validate the signature and dispatch the wrapped payload.
Source code in aloha/service/api/v1.py
create_v1_router(handler_class)
¶
Create FastAPI routes for a v1 API handler class with signing validation.
Args: handler_class: A class inheriting from APIHandler
Returns: An async function that handles v1 signed requests
Source code in aloha/service/api/v1.py
sign_check(salt_uuid, app_id, sign, data, sign_method=None, date_time=None)
¶
Validate a v1 request signature.
Source code in aloha/service/api/v1.py
sign_data(salt_uuid, app_id, app_key, data, sign_method=None)
¶
Generate the v1 signature for a payload.
The signature is based on app_id + salt_uuid + data + app_key.
Source code in aloha/service/api/v1.py
Version 2 token-based JSON API helpers for FastAPI.
Version 2 uses an access token in the request header and a request-id header for tracing. It keeps the same request/response shape as the earlier API generations while adding header-based authentication.
APICaller
¶
Bases: AbstractApiClient
Client helper that adds v2 access-token headers automatically.
Source code in aloha/service/api/v2.py
get_headers(app_id=None, app_key=None)
¶
Build the HTTP headers expected by v2 handlers.
Source code in aloha/service/api/v2.py
APIHandler
¶
Bases: AbstractApiHandler, ABC
Token-authenticated API handler for v2 endpoints.
Source code in aloha/service/api/v2.py
get(*args, **kwargs)
async
¶
Handle GET requests with query-string arguments.
Source code in aloha/service/api/v2.py
post(*args, **kwargs)
async
¶
Handle POST requests with JSON request bodies.
Source code in aloha/service/api/v2.py
prepare()
async
¶
Validate the access token before handling the request.
Source code in aloha/service/api/v2.py
create_v2_router(handler_class)
¶
Create FastAPI routes for a v2 API handler class with JWT token validation.
Args: handler_class: A class inheriting from APIHandler
Returns: Tuple of (handle_post, handle_get) functions for the routes
Source code in aloha/service/api/v2.py
verify_v2_token(request)
¶
Dependency to verify v2 access token.
Returns the decoded token payload if valid, otherwise raises HTTPException.