Sessions — Aquilia Documentation
Comprehensive guide and documentation for Sessions in the Aquilia framework. View API reference, examples, and implementation patterns.
HTTP Client / Sessions HTTP Sessions Sessions provide persistent configuration, cookie storage, and connection reuse across multiple HTTP requests. What is a Session? An HTTPSession provides stateful, persistent context across multiple requests with automatic resource management: ))} from aquilia.http import HTTPSession async with HTTPSession() as session: # First request sets authentication cookie await session.post("/login", json= ) # Subsequent requests automatically send cookies + reuse connections profile = await session.get("/profile") settings = await session.get("/settings") await session.post("/update", json=data) Creating Sessions from aquilia.http import HTTPSession, HTTPClientConfig, TimeoutConfig, PoolConfig from aquilia.http import BasicAuth, LoggingInterceptor, CookieMiddleware # Basic session with auto-generated config session = HTTPSession() # Session with comprehensive configuration config = HTTPClientConfig( base_url="https://api.example.com", timeout=TimeoutConfig(total=30.0, connect=10.0), pool=PoolConfig(max_connections=50, max_connections_per_host=10), default_headers= , follow_redirects=True, max_redirects=10, raise_for_status=True, ) session = HTTPSession( config=config, cookies=None, interceptors=[ BasicAuth("user", "password"), ], ) Base URL session = HTTPSession(HTTPClientConfig( base_url="https://api.github.com" )) # Relative paths are appended to base_url users = await session.get("/users") # https://api.github.com/users repos = await session.get("/repos") # https://api.github.com/repos # Absolute URLs override base_url external = await session.get("https://httpbin.org/get") Cookie Management Sessions manage cookies compliant with RFC 6265, handling SameSite, Secure, and HttpOnly attributes: Automatic Cookie Handling session = HTTPSession() # Server sends Set-Cookie header response = await session.post("/login", json= ) # Session automatically stores cookie in CookieJar profile = await session.get("/profile") # Sends Cookie header # Cookies are domain/path scoped await session.get("https://other-domain.com/api") # No cookie sent Connection Management Connections are reused to reduce handshake overhead and TLS setup latency: from aquilia.http import HTTPSession, PoolConfig, HTTPClientConfig session = HTTPSession(HTTPClientConfig( pool=PoolConfig( max_connections=100, max_connections_per_host=10, keepalive_expiry=60.0, ) )) await session.get("https://api.example.com/users") # Establishes TCP connection await session.get("https://api.example.com/posts") # Reuses TCP connection Session vs HTTPClient While both make async requests, choose them based on lifetime requirements: Use HTTPSession when: Making multiple sequential calls to the same host, keeping login cookies, or sharing a connection pool. Use HTTPClient when: Performing isolated, one-off operations or accessing diverse external endpoints with unique request configurations. HTTPClient Transport Layer )
Go to Homepage