Skip to main content

haima_api/
lib.rs

1//! HTTP API for Haima — wallet, balance, transactions, payment endpoints.
2//!
3//! Endpoints:
4//! - `GET /health` — health check (public, no auth)
5//! - `GET /state` — get full financial state projection (protected)
6//! - `POST /v1/facilitate` — x402 payment facilitation (public)
7//! - `GET /v1/facilitator/stats` — facilitator dashboard stats (public)
8//! - `GET /v1/bureau/:agent_id` — agent credit bureau report (public)
9//!
10//! Auth is controlled via `HAIMA_JWT_SECRET` or `AUTH_SECRET` env var.
11//! If neither is set, auth is disabled (local dev mode).
12
13pub mod auth;
14pub mod routes;
15
16use std::collections::HashMap;
17use std::sync::Arc;
18
19use axum::Router;
20use haima_core::bureau::{PaymentHistory, TrustContext};
21use haima_core::credit::CreditScore;
22use haima_core::lending::CreditLine;
23use haima_lago::FinancialState;
24use haima_x402::FacilitatorStatsCounter;
25use tokio::sync::RwLock;
26
27use crate::auth::AuthConfig;
28
29/// Shared application state for the Haima API.
30#[derive(Clone)]
31pub struct AppState {
32    pub financial_state: Arc<RwLock<FinancialState>>,
33    pub auth_config: Arc<AuthConfig>,
34    /// In-memory facilitator statistics counter.
35    pub facilitator_stats: Arc<FacilitatorStatsCounter>,
36    /// Facilitator fee in basis points.
37    pub facilitator_fee_bps: u32,
38    /// In-memory credit score cache, keyed by `agent_id`.
39    pub credit_scores: Arc<RwLock<HashMap<String, CreditScore>>>,
40    /// In-memory credit lines, keyed by `agent_id`.
41    pub credit_lines: Arc<RwLock<HashMap<String, CreditLine>>>,
42    /// In-memory trust context cache, keyed by `agent_id` (from Autonomic).
43    pub trust_contexts: Arc<RwLock<HashMap<String, TrustContext>>>,
44    /// In-memory payment history cache, keyed by `agent_id`.
45    pub payment_histories: Arc<RwLock<HashMap<String, PaymentHistory>>>,
46}
47
48impl AppState {
49    /// Create a new `AppState` with the given auth config and default financial state.
50    pub fn new(auth_config: AuthConfig) -> Self {
51        Self {
52            financial_state: Arc::new(RwLock::new(FinancialState::default())),
53            auth_config: Arc::new(auth_config),
54            facilitator_stats: Arc::new(FacilitatorStatsCounter::new()),
55            facilitator_fee_bps: haima_x402::DEFAULT_FEE_BPS,
56            credit_scores: Arc::new(RwLock::new(HashMap::new())),
57            credit_lines: Arc::new(RwLock::new(HashMap::new())),
58            trust_contexts: Arc::new(RwLock::new(HashMap::new())),
59            payment_histories: Arc::new(RwLock::new(HashMap::new())),
60        }
61    }
62}
63
64impl Default for AppState {
65    fn default() -> Self {
66        Self {
67            financial_state: Arc::new(RwLock::new(FinancialState::default())),
68            auth_config: Arc::new(AuthConfig { jwt_secret: None }),
69            facilitator_stats: Arc::new(FacilitatorStatsCounter::new()),
70            facilitator_fee_bps: haima_x402::DEFAULT_FEE_BPS,
71            credit_scores: Arc::new(RwLock::new(HashMap::new())),
72            credit_lines: Arc::new(RwLock::new(HashMap::new())),
73            trust_contexts: Arc::new(RwLock::new(HashMap::new())),
74            payment_histories: Arc::new(RwLock::new(HashMap::new())),
75        }
76    }
77}
78
79/// Build the Haima API router.
80pub fn router(state: AppState) -> Router {
81    routes::routes(state)
82}