fastly_api/apis/
billing_usage_metrics_api.rs

1/*
2 * Fastly API
3 *
4 * Via the Fastly API you can perform any of the operations that are possible within the management console,  including creating services, domains, and backends, configuring rules or uploading your own application code, as well as account operations such as user administration and billing reports. The API is organized into collections of endpoints that allow manipulation of objects related to Fastly services and accounts. For the most accurate and up-to-date API reference content, visit our [Developer Hub](https://www.fastly.com/documentation/reference/api/) 
5 *
6 */
7
8
9use reqwest;
10
11use crate::apis::ResponseContent;
12use super::{Error, configuration};
13
14/// struct for passing parameters to the method [`get_service_level_usage`]
15#[derive(Clone, Debug, Default)]
16pub struct GetServiceLevelUsageParams {
17    /// The product identifier for the metrics returned (e.g., `cdn_usage`). This should be used along with `usage_type_name`.
18    pub product_id: Option<String>,
19    /// The service identifier for the metrics being requested.
20    pub service: Option<String>,
21    /// The usage type name for the metrics returned (e.g., `North America Requests`). This should be used along with `product_id`.
22    pub usage_type_name: Option<String>,
23    pub start_month: Option<String>,
24    pub end_month: Option<String>,
25    /// Number of results per page. The maximum is 10000.
26    pub limit: Option<String>,
27    /// Cursor value from the `next_cursor` field of a previous response, used to retrieve the next page. To request the first page, this should be empty.
28    pub cursor: Option<String>
29}
30
31/// struct for passing parameters to the method [`get_usage_metrics`]
32#[derive(Clone, Debug, Default)]
33pub struct GetUsageMetricsParams {
34    pub start_month: String,
35    pub end_month: String
36}
37
38
39/// struct for typed errors of method [`get_service_level_usage`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum GetServiceLevelUsageError {
43    Status400(crate::models::Error),
44    Status401(crate::models::Error),
45    Status500(crate::models::Error),
46    UnknownValue(serde_json::Value),
47}
48
49/// struct for typed errors of method [`get_usage_metrics`]
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(untagged)]
52pub enum GetUsageMetricsError {
53    Status400(crate::models::Error),
54    Status401(crate::models::Error),
55    Status500(crate::models::Error),
56    UnknownValue(serde_json::Value),
57}
58
59
60/// Returns product usage, broken down by service.
61pub async fn get_service_level_usage(configuration: &mut configuration::Configuration, params: GetServiceLevelUsageParams) -> Result<crate::models::Serviceusagemetrics, Error<GetServiceLevelUsageError>> {
62    let local_var_configuration = configuration;
63
64    // unbox the parameters
65    let product_id = params.product_id;
66    let service = params.service;
67    let usage_type_name = params.usage_type_name;
68    let start_month = params.start_month;
69    let end_month = params.end_month;
70    let limit = params.limit;
71    let cursor = params.cursor;
72
73
74    let local_var_client = &local_var_configuration.client;
75
76    let local_var_uri_str = format!("{}/billing/v3/service-usage-metrics", local_var_configuration.base_path);
77    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
78
79    if let Some(ref local_var_str) = product_id {
80        local_var_req_builder = local_var_req_builder.query(&[("product_id", &local_var_str.to_string())]);
81    }
82    if let Some(ref local_var_str) = service {
83        local_var_req_builder = local_var_req_builder.query(&[("service", &local_var_str.to_string())]);
84    }
85    if let Some(ref local_var_str) = usage_type_name {
86        local_var_req_builder = local_var_req_builder.query(&[("usage_type_name", &local_var_str.to_string())]);
87    }
88    if let Some(ref local_var_str) = start_month {
89        local_var_req_builder = local_var_req_builder.query(&[("start_month", &local_var_str.to_string())]);
90    }
91    if let Some(ref local_var_str) = end_month {
92        local_var_req_builder = local_var_req_builder.query(&[("end_month", &local_var_str.to_string())]);
93    }
94    if let Some(ref local_var_str) = limit {
95        local_var_req_builder = local_var_req_builder.query(&[("limit", &local_var_str.to_string())]);
96    }
97    if let Some(ref local_var_str) = cursor {
98        local_var_req_builder = local_var_req_builder.query(&[("cursor", &local_var_str.to_string())]);
99    }
100    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
101        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
102    }
103    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
104        let local_var_key = local_var_apikey.key.clone();
105        let local_var_value = match local_var_apikey.prefix {
106            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
107            None => local_var_key,
108        };
109        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
110    };
111
112    let local_var_req = local_var_req_builder.build()?;
113    let local_var_resp = local_var_client.execute(local_var_req).await?;
114
115    if "GET" != "GET" && "GET" != "HEAD" {
116      let headers = local_var_resp.headers();
117      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
118          Some(v) => v.to_str().unwrap().parse().unwrap(),
119          None => configuration::DEFAULT_RATELIMIT,
120      };
121      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
122          Some(v) => v.to_str().unwrap().parse().unwrap(),
123          None => 0,
124      };
125    }
126
127    let local_var_status = local_var_resp.status();
128    let local_var_content = local_var_resp.text().await?;
129
130    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
131        serde_json::from_str(&local_var_content).map_err(Error::from)
132    } else {
133        let local_var_entity: Option<GetServiceLevelUsageError> = serde_json::from_str(&local_var_content).ok();
134        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
135        Err(Error::ResponseError(local_var_error))
136    }
137}
138
139/// Returns monthly usage metrics for customer by product.
140pub async fn get_usage_metrics(configuration: &mut configuration::Configuration, params: GetUsageMetricsParams) -> Result<crate::models::Usagemetric, Error<GetUsageMetricsError>> {
141    let local_var_configuration = configuration;
142
143    // unbox the parameters
144    let start_month = params.start_month;
145    let end_month = params.end_month;
146
147
148    let local_var_client = &local_var_configuration.client;
149
150    let local_var_uri_str = format!("{}/billing/v3/usage-metrics", local_var_configuration.base_path);
151    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
152
153    local_var_req_builder = local_var_req_builder.query(&[("start_month", &start_month.to_string())]);
154    local_var_req_builder = local_var_req_builder.query(&[("end_month", &end_month.to_string())]);
155    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
156        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
157    }
158    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
159        let local_var_key = local_var_apikey.key.clone();
160        let local_var_value = match local_var_apikey.prefix {
161            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
162            None => local_var_key,
163        };
164        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
165    };
166
167    let local_var_req = local_var_req_builder.build()?;
168    let local_var_resp = local_var_client.execute(local_var_req).await?;
169
170    if "GET" != "GET" && "GET" != "HEAD" {
171      let headers = local_var_resp.headers();
172      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
173          Some(v) => v.to_str().unwrap().parse().unwrap(),
174          None => configuration::DEFAULT_RATELIMIT,
175      };
176      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
177          Some(v) => v.to_str().unwrap().parse().unwrap(),
178          None => 0,
179      };
180    }
181
182    let local_var_status = local_var_resp.status();
183    let local_var_content = local_var_resp.text().await?;
184
185    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
186        serde_json::from_str(&local_var_content).map_err(Error::from)
187    } else {
188        let local_var_entity: Option<GetUsageMetricsError> = serde_json::from_str(&local_var_content).ok();
189        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
190        Err(Error::ResponseError(local_var_error))
191    }
192}
193