amazon_spapi/client/
endpoint.rs

1use std::fmt;
2
3/// Represents an SP-API endpoint with its configuration
4#[derive(Clone)]
5pub struct ApiEndpoint {
6    /// API version identifier for rate limiting grouping
7    pub version: &'static str,
8    /// The request path with placeholders for parameters
9    pub path: &'static str,
10    /// Parameters Map
11    pub path_params: Option<Vec<(&'static str, String)>>,
12    /// HTTP method
13    pub method: ApiMethod,
14    /// Rate limit for this endpoint in requests per second
15    pub rate: f64,
16    /// Burst capacity
17    pub burst: u32,
18}
19
20impl ApiEndpoint {
21    /// Generate the rate limiting key using api_version + path template
22    pub fn rate_limit_key(&self) -> String {
23        format!("{}:{}", self.version, self.path)
24    }
25
26    /// Get the actual URL path with parameters substituted
27    pub fn get_path(&self) -> String {
28        let mut path = self.path.to_string();
29        if let Some(path_params) = &self.path_params {
30            for (key, value) in path_params {
31                let placeholder = format!("{{{}}}", key);
32                path = path.replace(&placeholder, value);
33            }
34        }
35        path
36    }
37}
38
39/// HTTP methods supported by SP-API
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub enum ApiMethod {
42    Get,
43    Post,
44    Put,
45    Delete,
46    Patch,
47}
48
49impl fmt::Display for ApiMethod {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        match self {
52            ApiMethod::Get => write!(f, "GET"),
53            ApiMethod::Post => write!(f, "POST"),
54            ApiMethod::Put => write!(f, "PUT"),
55            ApiMethod::Delete => write!(f, "DELETE"),
56            ApiMethod::Patch => write!(f, "PATCH"),
57        }
58    }
59}