# batpool/upstox-totp architecture

> Architecture diagram of the GitHub repository batpool/upstox-totp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/batpool/upstox-totp
- Repository: https://github.com/batpool/upstox-totp

## Overview

Upstox TOTP is a Python SDK and CLI for turning account credentials and a TOTP secret into an Upstox OAuth access token. The main workflow configures a client, performs authorization-dialog lookup, OTP generation and verification, PIN-based 2FA, OAuth approval, then exchanges the authorization code for a token. The graph also captures the CLI and the shared request, response-model, error, and logging layers. Relationships are grounded in the sampled client, API, CLI, and model code; README claims about refresh/storage and trading integrations are not represented as implemented components because their source is not shown.

Main entry points: [`src/upstox_totp/_api/app_token.py`](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/_api/app_token.py) (Token Workflow), [`src/upstox_totp/client.py`](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/client.py) (Client), [`src/upstox_totp/_api/base.py`](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/_api/base.py) (HTTP Request Layer), [`src/upstox_totp/cli.py`](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/cli.py) (CLI Commands)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["SDK Interfaces"]
  node_sdk["Python SDK<br/>[__init__.py]"]
  node_cli["CLI Commands<br/>[cli.py]"]
end

subgraph group_authentication["Authentication Flow"]
  node_app_token["Token Workflow<br/>[app_token.py]"]
  node_token["Access Token"]
end

subgraph group_runtime["Client Runtime"]
  node_client["Client<br/>[client.py]"]
  node_totp["TOTP Generator<br/>[client.py]"]
  node_base_api["HTTP Request Layer<br/>[base.py]"]
end

subgraph group_support["Shared Support"]
  node_config["Configuration Models<br/>[models.py]"]
  node_response_models["Auth Response Models<br/>[models.py]"]
  node_errors["Error Handling<br/>[errors.py]"]
  node_logging["Logging<br/>[logging.py]"]
end

node_developer(("Developer"))
node_cli_user(("CLI User"))
node_upstox{{"Upstox Services"}}

node_developer -->|"uses"| node_sdk
node_cli_user -->|"runs"| node_cli
node_sdk -->|"exports"| node_client
node_cli -->|"creates"| node_client
node_cli -->|"requests token"| node_app_token
node_client -->|"loads"| node_config
node_client -->|"generates"| node_totp
node_client -->|"creates"| node_app_token
node_app_token -->|"sends requests"| node_base_api
node_base_api -->|"uses session"| node_client
node_app_token -->|"gets code"| node_totp
node_app_token -->|"validates responses"| node_response_models
node_base_api -->|"parses JSON"| node_response_models
node_app_token -->|"authenticates"| node_upstox
node_base_api -->|"HTTP exchange"| node_upstox
node_app_token -->|"returns token"| node_token
node_base_api -->|"raises on failure"| node_errors
node_app_token -->|"raises validation"| node_errors
node_client -->|"raises configuration"| node_errors
node_client -->|"sets level"| node_logging
node_base_api -->|"logs requests"| node_logging

click node_sdk "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/__init__.py"
click node_cli "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/cli.py"
click node_client "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/client.py"
click node_config "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/models.py"
click node_totp "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/client.py"
click node_app_token "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/_api/app_token.py"
click node_base_api "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/_api/base.py"
click node_response_models "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/models.py"
click node_errors "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/errors.py"
click node_logging "https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/logging.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sdk,node_cli,node_cli_user toneBlue
class node_app_token,node_token toneAmber
class node_client,node_totp,node_base_api toneMint
class node_config,node_response_models,node_errors,node_logging toneRose
class node_developer,node_upstox toneIndigo
```

## Components

### SDK Interfaces

- [Python SDK](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/__init__.py): `src/upstox_totp/__init__.py`
- [CLI Commands](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/cli.py): `src/upstox_totp/cli.py`

### Authentication Flow

- [Token Workflow](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/_api/app_token.py): `src/upstox_totp/_api/app_token.py`
- **Access Token**

### Client Runtime

- [Client](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/client.py): `src/upstox_totp/client.py`
- [TOTP Generator](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/client.py): `src/upstox_totp/client.py`
- [HTTP Request Layer](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/_api/base.py): `src/upstox_totp/_api/base.py`

### Shared Support

- [Configuration Models](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/models.py): `src/upstox_totp/models.py`
- [Auth Response Models](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/models.py): `src/upstox_totp/models.py`
- [Error Handling](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/errors.py): `src/upstox_totp/errors.py`
- [Logging](https://github.com/batpool/upstox-totp/blob/master/src/upstox_totp/logging.py): `src/upstox_totp/logging.py`

### Other components

- **Developer**
- **CLI User**
- **Upstox Services**

## Connections

- Developer → Python SDK: uses (evidence: `src/upstox_totp/__init__.py`)
- CLI User → CLI Commands: runs (evidence: `src/upstox_totp/cli.py`)
- Python SDK → Client: exports (evidence: `src/upstox_totp/__init__.py`)
- CLI Commands → Client: creates (evidence: `src/upstox_totp/cli.py`)
- CLI Commands → Token Workflow: requests token (evidence: `src/upstox_totp/cli.py`)
- Client → Configuration Models: loads (evidence: `src/upstox_totp/client.py`)
- Client → TOTP Generator: generates (evidence: `src/upstox_totp/client.py`)
- Client → Token Workflow: creates (evidence: `src/upstox_totp/client.py`)
- Token Workflow → HTTP Request Layer: sends requests (evidence: `src/upstox_totp/_api/app_token.py`)
- HTTP Request Layer → Client: uses session (evidence: `src/upstox_totp/_api/base.py`)
- Token Workflow → TOTP Generator: gets code (evidence: `src/upstox_totp/_api/app_token.py`)
- Token Workflow → Auth Response Models: validates responses (evidence: `src/upstox_totp/_api/app_token.py`)
- HTTP Request Layer → Auth Response Models: parses JSON (evidence: `src/upstox_totp/_api/base.py`)
- Token Workflow → Upstox Services: authenticates (evidence: `src/upstox_totp/_api/app_token.py`)
- HTTP Request Layer → Upstox Services: HTTP exchange (evidence: `src/upstox_totp/_api/base.py`)
- Token Workflow → Access Token: returns token (evidence: `src/upstox_totp/_api/app_token.py`)
- HTTP Request Layer → Error Handling: raises on failure (evidence: `src/upstox_totp/_api/base.py`)
- Token Workflow → Error Handling: raises validation (evidence: `src/upstox_totp/_api/app_token.py`)
- Client → Error Handling: raises configuration (evidence: `src/upstox_totp/client.py`)
- Client → Logging: sets level (evidence: `src/upstox_totp/client.py`)
- HTTP Request Layer → Logging: logs requests (evidence: `src/upstox_totp/_api/base.py`)
