# gleanwork/api-client-python architecture

> Architecture diagram of the GitHub repository gleanwork/api-client-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gleanwork/api-client-python
- Repository: https://github.com/gleanwork/api-client-python

## Overview

##### 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”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Code-Generation Pipeline
    subgraph "Code-Generation Pipeline"
        direction TB
        Spec[".speakeasy/gen.yaml"]:::gen
        Gen["Speakeasy Generator"]:::gen
        Spec --> Gen
        Gen -->|"generates"| SDKSrc["SDK Source & Docs"]:::core
        Gen --> Docs["docs/"]:::ext
        Gen --> Examples["examples/"]:::ext
    end

    %% SDK Core Layer
    subgraph "SDK Core" 
        direction TB
        Glean["Glean Class"]:::core
        BaseSDK["BaseSDK Logic"]:::core
        SDKConfig["SDK Configuration"]:::core
        HTTPClient["HTTP Client Abstraction"]:::core
        Models["Models Layer"]:::core
        Utils["Utils & Hooks"]:::core
        subgraph "Namespace Clients"
            direction TB
            ClientAPI["Client API Namespace"]:::core
            IndexingAPI["Indexing API Namespace"]:::core
        end
        Glean --> BaseSDK --> SDKConfig --> HTTPClient --> Models --> Utils
        Glean --> ClientAPI
        Glean --> IndexingAPI
        ClientAPI --> HTTPClient
        IndexingAPI --> HTTPClient
    end

    %% Testing Infrastructure
    subgraph "Testing Infrastructure"
        direction TB
        Pytest["pytest Suite"]:::test
        MockServer["Mock Server"]:::test
        Pytest --> MockServer
        MockServer --> GleanAPI
    end

    %% External / Integration Points
    UserCode["User Code / Examples"]:::ext
    PyPI["PyPI Registry"]:::ext
    GleanAPI["Glean API (Real)"]:::ext
    CIWorkflows["CI Workflows"]:::gen

    UserCode --> Glean
    Glean -->|"HTTP calls"| GleanAPI
    Glean --> PyPI
    CIWorkflows --> Spec
    CIWorkflows --> Pytest
    CIWorkflows --> PyPI

    %% Click Events
    click Glean "https://github.com/gleanwork/api-client-python/blob/main/src/glean/api_client/client.py"
    click BaseSDK "https://github.com/gleanwork/api-client-python/blob/main/src/glean/api_client/basesdk.py"
    click SDKConfig "https://github.com/gleanwork/api-client-python/blob/main/src/glean/api_client/sdkconfiguration.py"
    click HTTPClient "https://github.com/gleanwork/api-client-python/blob/main/src/glean/api_client/httpclient.py"
    click Models "https://github.com/gleanwork/api-client-python/tree/main/src/glean/api_client/models/"
    click Utils "https://github.com/gleanwork/api-client-python/tree/main/src/glean/api_client/utils/"
    click Spec "https://github.com/gleanwork/api-client-python/blob/main/.speakeasy/gen.yaml"
    click CIWorkflows "https://github.com/gleanwork/api-client-python/blob/main/.github/workflows/sdk_generation.yaml"
    click Pytest "https://github.com/gleanwork/api-client-python/tree/main/tests/"
    click MockServer "https://github.com/gleanwork/api-client-python/tree/main/tests/mockserver/"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,color:#004085
    classDef gen fill:#d4edda,stroke:#155724,color:#155724
    classDef test fill:#fff3cd,stroke:#856404,color:#856404
    classDef ext fill:#e2e3e5,stroke:#6c757d,color:#6c757d
```
