amazon_spapi/client/
config.rs1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::fs;
4
5#[derive(Debug, Serialize, Deserialize, Clone)]
6pub struct SpapiConfig {
7 pub client_id: String,
8 pub client_secret: String,
9 pub refresh_token: String,
10 pub marketplace_id: String,
11 pub region: String,
12 pub sandbox: bool,
13 pub user_agent: Option<String>,
14 pub timeout_sec: Option<u64>,
15}
16
17impl SpapiConfig {
18 pub fn from_env() -> Result<Self> {
19 let client_id = std::env::var("SPAPI_CLIENT_ID").map_err(|_| {
20 anyhow::anyhow!("SPAPI_CLIENT_ID environment variable is not set")
21 })?;
22 let client_secret = std::env::var("SPAPI_CLIENT_SECRET").map_err(|_| {
23 anyhow::anyhow!("SPAPI_CLIENT_SECRET environment variable is not set")
24 })?;
25 let refresh_token = std::env::var("SPAPI_REFRESH_TOKEN").map_err(|_| {
26 anyhow::anyhow!("SPAPI_REFRESH_TOKEN environment variable is not set")
27 })?;
28 let marketplace_id = std::env::var("SPAPI_MARKETPLACE_ID").map_err(|_| {
29 anyhow::anyhow!("SPAPI_MARKETPLACE_ID environment variable is not set")
30 })?;
31 let region = std::env::var("SPAPI_REGION").map_err(|_| {
32 anyhow::anyhow!("SPAPI_REGION environment variable is not set")
33 })?;
34 let sandbox = std::env::var("SPAPI_SANDBOX").map_err(|_| {
35 anyhow::anyhow!("SPAPI_SANDBOX environment variable is not set or invalid")
36 })?;
37 let sandbox = sandbox == "true" || sandbox == "1";
38 Ok(Self {
39 client_id,
40 client_secret,
41 refresh_token,
42 marketplace_id,
43 region,
44 sandbox,
45 user_agent: None,
46 timeout_sec: Some(30),
47 })
48 }
49}
50
51#[derive(Debug, Serialize, Deserialize)]
52pub struct Config {
53 pub spapi: SpapiConfig,
54}
55
56impl Config {
57 pub fn from_file(path: &str) -> Result<Self> {
58 let content = fs::read_to_string(path)?;
59 let config: Config = toml::from_str(&content)?;
60 Ok(config)
61 }
62
63 pub fn from_default_file() -> Result<Self> {
64 Self::from_file("config.toml")
65 }
66}