Skip to main content

agentis_pay_shared/
config.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3
4#[cfg(debug_assertions)]
5const DEFAULT_GRPC_ENDPOINT: &str = "http://localhost:9000";
6#[cfg(debug_assertions)]
7const DEFAULT_REST_ENDPOINT: &str = "http://localhost:8000";
8
9#[cfg(not(debug_assertions))]
10const DEFAULT_GRPC_ENDPOINT: &str = "https://agentispay-api.bipa.app";
11#[cfg(not(debug_assertions))]
12const DEFAULT_REST_ENDPOINT: &str = "https://api.bipa.app";
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct ClientConfig {
16    #[serde(default = "default_grpc_endpoint")]
17    pub endpoint: String,
18    #[serde(default = "default_rest_endpoint")]
19    pub oauth_endpoint: String,
20}
21
22impl Default for ClientConfig {
23    fn default() -> Self {
24        Self {
25            endpoint: default_grpc_endpoint(),
26            oauth_endpoint: default_rest_endpoint(),
27        }
28    }
29}
30
31impl ClientConfig {
32    /// Loads internal defaults only.
33    ///
34    /// User-editable config files were removed from the CLI bootstrap path so the
35    /// endpoints are controlled by the application, not user config.
36    pub fn load() -> Result<Self> {
37        let endpoint = read_env_var("AGENTIS_PAY_ENDPOINT").unwrap_or_else(default_grpc_endpoint);
38        let oauth_endpoint =
39            read_env_var("AGENTIS_PAY_OAUTH_ENDPOINT").unwrap_or_else(default_rest_endpoint);
40
41        Ok(Self {
42            endpoint,
43            oauth_endpoint,
44        })
45    }
46
47    pub fn endpoint(&self) -> &str {
48        self.endpoint.as_str()
49    }
50
51    pub fn oauth_endpoint(&self) -> &str {
52        self.oauth_endpoint.as_str()
53    }
54
55    /// Returns true when the gRPC endpoint uses TLS (https).
56    pub fn is_tls(&self) -> bool {
57        self.endpoint.starts_with("https://")
58    }
59}
60
61fn read_env_var(name: &str) -> Option<String> {
62    std::env::var(name)
63        .ok()
64        .map(|value| value.trim().trim_end_matches('/').to_string())
65        .filter(|value| !value.is_empty())
66}
67
68fn default_grpc_endpoint() -> String {
69    DEFAULT_GRPC_ENDPOINT.to_string()
70}
71
72fn default_rest_endpoint() -> String {
73    DEFAULT_REST_ENDPOINT.to_string()
74}