How gleanwork/api-client-python works
Project Type and Purpose
• This is an open-source SDK library (the Glean Python API Client) that provides a unified interface to two REST API namespaces:
• Client API: end-user operations (search, chat, documents, etc.)
• Indexing API: administrative operations (indexing content, permissions).
• It’s code-generated via the Speakeasy toolchain and uses httpx under the hood for both sync and async HTTP calls.
Key Top-Level Directories and Roles
• src/glean/api_client – Core SDK code:
• client.py, basesdk.py, namespace clients (client_chat.py, indexing_documents.py, etc.)
• models/ – Pydantic-style request/response models
• utils/ – HTTP glue, retry logic, serializers
• .speakeasy, scripts/, .github/ – Code generation pipeline:
• .speakeasy/gen.yaml – spec & generator config
• scripts/prepare_readme.py, CI workflows for SDK build/publish
• docs/ & README.md – Generated documentation and usage examples
• examples/ – Sample usage scripts
• tests/ – SDK unit tests and a Docker-based mock server for integration testing
Main Components
a. SDK Core
– Glean Class (entry point)
– Namespace Clients (client, indexing)
– HTTP Client Abstraction (httpx.Client / httpx.AsyncClient injected)
– Models Layer (request/response dataclasses)
– Hooks & Utils (retry, logging, auth)
b. Code-Generation Pipeline
– Speakeasy Spec → Generator → src/ + docs/ + examples/
– CI Workflows (.github/**) that trigger generation, tests, and publish
c. Testing Infrastructure
– Mock Server (Docker container serving the Glean API contract)
– pytest suite calling the SDK against the mock server
Relationships and Data Flows
• User Code calls → Glean → selects namespace → invokes operation method (e.g., client.search.query)
• Namespace client builds request using Models and HTTP Utils, passes to HTTP Client
• HTTP Client sends request → Glean API (real or mock server) → HTTP response
• Response deserialized into Model and returned to user
• RetryConfig, Auth, Logging Hooks intercept in the HTTP Utils layer
Architectural Patterns & Principles
• Factory/Factory Method: Glean class creates per-namespace clients
• Dependency Injection: Custom HTTP client, retry config, logger passed into SDK
• Code Generation: Spec-driven generation ensures consistency across languages
• Separation of Concerns: clear layers for client logic, HTTP transport, models, docs, tests
Essential Elements
• Clear labels on each component
• Optional notes on patterns (e.g., “DI via httpx client”, “Context manager closes resources”)