How gugilla-aakash/hakiapi works
Purpose and public entry points
HakiAPI is a Python 3.10+ SDK foundation for building resilient HTTP API clients, with bundled GitHub, Gmail, and Google Calendar clients. Public imports are organized through hakiapi/__init__.py; consumers either instantiate bundled clients or subclass the synchronous base client. Runnable usage references are example_oauth.py and examples/example_github.py.
Synchronous request core
BaseAPIClient centralizes session lifecycle, base-URL request routing, authentication attachment, timeouts, response parsing, raw-response access, and HTTP-status mapping. Endpoint methods in bundled or downstream clients should stay thin and call its verb methods.
• hakiapi/core/base_client.py
• hakiapi/core/exceptions.py
• hakiapi/core/retry.py
Flow: client endpoint → _request() → requests.Session with mounted urllib3 retry adapters → external API → JSON/text or typed exception.
Authentication and OAuth boundary
Authentication strategies plug into requests through AuthBase: bearer, header/query API keys, HMAC signing, and OAuth-backed bearer injection. Google OAuth is separate from API transport: it manages browser authorization, localhost redirect capture, CSRF state validation, token exchange, persistence, and optional explicit refresh.
• hakiapi/core/auth.py
• hakiapi/core/oauth/google.py
• hakiapi/core/oauth/token_store.py
The OAuth integration touches Google authorization/token endpoints, the local browser, and a one-shot localhost HTTP server. Token persistence is local filesystem JSON with atomic replacement and restrictive permissions.
Resilience and failure semantics
Retries occur before final error mapping for retryable HTTP statuses and network failures. Typed exceptions provide a uniform boundary for application code across client implementations. A standalone, thread-safe circuit breaker wraps synchronous callables and transitions CLOSED → OPEN → HALF_OPEN to prevent repeated downstream failures.
• hakiapi/core/retry.py
• hakiapi/core/exceptions.py
• hakiapi/core/circuit_breaker.py
Pagination abstraction
A lazy paginator repeatedly invokes a client with raw_response=True, interprets response-specific item and continuation conventions, and yields items without making pagination part of each endpoint implementation. It supports Link-header and token-based styles used by the bundled services.
• hakiapi/core/paginator.py
• hakiapi/core/base_client.py
Async transport path
AsyncBaseAPIClient is an independent asyncio/httpx implementation, not a wrapper around the sync core. It recreates retry/backoff, typed exceptions, endpoint SSRF protections, disabled redirect following, response-size limits, and async context-manager cleanup. httpx is an optional runtime dependency.
• hakiapi/core/async_base_client.py
• hakiapi/core/exceptions.py
Bundled service adapters
Bundled clients specialize the common core with service URLs, authentication conventions, resource-oriented methods, and service-specific pagination. GitHub adds REST and GraphQL execution, including GraphQL payload-error detection; Gmail and Calendar expose resource groupings for their Google APIs.
• hakiapi/clients/github.py
• hakiapi/clients/gmail.py
• hakiapi/clients/google_calendar.py
External service boundaries are GitHub REST/GraphQL APIs, Gmail API, Google Calendar API, and Google OAuth endpoints. Packaging/release infrastructure is defined in pyproject.toml and .github/workflows/release.yml.
Main entry points: base_client.py BaseAPIClient · __init__.py Public SDK imports · google.py Google OAuth flow · async_base_client.py AsyncBaseAPIClient
Components 17
Client Core
Shared synchronous and asynchronous transport foundations.
- BaseAPIClient · Centralizes session, routing, parsing, and status mapping.
hakiapi/core/base_client.py - AsyncBaseAPIClient · Independent asyncio/httpx transport with safety controls.
hakiapi/core/async_base_client.py - Lazy paginator · Consumes raw responses and yields items across continuation styles.
hakiapi/core/paginator.py
Auth & Resilience
Authentication, OAuth, and failure-control components.
- Retry policy · Configures retryable HTTP and network failure handling.
hakiapi/core/retry.py - Typed exceptions · Uniform application-facing HTTP failure mapping.
hakiapi/core/exceptions.py - Circuit breaker · Thread-safe CLOSED, OPEN, and HALF_OPEN callable guard.
hakiapi/core/circuit_breaker.py - Auth strategies · Bearer, API key, HMAC, and OAuth bearer attachment.
hakiapi/core/auth.py - Google OAuth flow · Runs browser authorization, redirect validation, exchange, and refresh.
hakiapi/core/oauth/google.py - Token store · Atomically persists restricted-permission token JSON.
hakiapi/core/oauth/token_store.py
Bundled Clients
Service-specific API adapters.
- GitHub client · Provides REST and GraphQL calls with GraphQL error detection.
hakiapi/clients/github.py - Gmail client · Exposes Gmail resource operations.
hakiapi/clients/gmail.py - Google Calendar client · Exposes Calendar resource operations.
hakiapi/clients/google_calendar.py
External Boundaries
Remote services and local OAuth interactions.
- GitHub REST & GraphQL
- Gmail & Calendar APIs
- Google OAuth endpoints
- Browser & localhost callback · Browser authorization and one-shot localhost redirect capture.
Other components
- Public SDK imports · Exports the supported consumer-facing SDK surface.
hakiapi/__init__.py
Connections 22
- Public SDK imports → BaseAPIClient: exports
- Public SDK imports → AsyncBaseAPIClient: exports
- Public SDK imports → GitHub client: exports
- Public SDK imports → Gmail client: exports
- Public SDK imports → Google Calendar client: exports
- GitHub client → BaseAPIClient: specializes
- Gmail client → BaseAPIClient: specializes
- Google Calendar client → BaseAPIClient: specializes
- BaseAPIClient → Auth strategies: attaches auth
- BaseAPIClient → Retry policy: mounts retry adapters
- BaseAPIClient → Typed exceptions: maps failures
- AsyncBaseAPIClient → Retry policy: recreates backoff
- AsyncBaseAPIClient → Typed exceptions: maps failures
- Lazy paginator → BaseAPIClient: calls with raw response
- Google OAuth flow → Auth strategies: supplies OAuth bearer
- Google OAuth flow → Token store: persists tokens
- Google OAuth flow → Google OAuth endpoints: authorizes and exchanges
- Google OAuth flow → Browser & localhost callback: uses redirect flow
- GitHub client → GitHub REST & GraphQL: REST and GraphQL requests
- Gmail client → Gmail & Calendar APIs: API requests
- Google Calendar client → Gmail & Calendar APIs: API requests
- Circuit breaker → BaseAPIClient: wraps client calls