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 field is not required for CSV requests.
18    pub product_id: String,
19    /// The usage type name for the metrics returned (e.g., `North America Requests`). This field is not required for CSV requests.
20    pub usage_type_name: String,
21    pub start_month: Option<String>,
22    pub end_month: Option<String>,
23    /// Number of results per page. The maximum is 100.
24    pub limit: Option<String>,
25    /// 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.
26    pub cursor: Option<String>
27}
28
29/// struct for passing parameters to the method [`get_usage_metrics`]
30#[derive(Clone, Debug, Default)]
31pub struct GetUsageMetricsParams {
32    pub start_month: Option<String>,
33    pub end_month: Option<String>
34}
35
36
37/// struct for typed errors of method [`get_service_level_usage`]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum GetServiceLevelUsageError {
41    Status400(crate::models::Error),
42    Status401(crate::models::Error),
43    Status500(crate::models::Error),
44    UnknownValue(serde_json::Value),
45}
46
47/// struct for typed errors of method [`get_usage_metrics`]
48#[derive(Debug, Clone, Serialize, Deserialize)]
49#[serde(untagged)]
50pub enum GetUsageMetricsError {
51    Status400(crate::models::Error),
52    Status401(crate::models::Error),
53    Status500(crate::models::Error),
54    UnknownValue(serde_json::Value),
55}
56
57
58/// Returns product usage, broken down by service.
59pub async fn get_service_level_usage(configuration: &mut configuration::Configuration, params: GetServiceLevelUsageParams) -> Result<crate::models::Serviceusagemetrics, Error<GetServiceLevelUsageError>> {
60    let local_var_configuration = configuration;
61
62    // unbox the parameters
63    let product_id = params.product_id;
64    let usage_type_name = params.usage_type_name;
65    let start_month = params.start_month;
66    let end_month = params.end_month;
67    let limit = params.limit;
68    let cursor = params.cursor;
69
70
71    let local_var_client = &local_var_configuration.client;
72
73    let local_var_uri_str = format!("{}/billing/v3/service-usage-metrics", local_var_configuration.base_path);
74    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
75
76    local_var_req_builder = local_var_req_builder.query(&[("product_id", &product_id.to_string())]);
77    local_var_req_builder = local_var_req_builder.query(&[("usage_type_name", &usage_type_name.to_string())]);
78    if let Some(ref local_var_str) = start_month {
79        local_var_req_builder = local_var_req_builder.query(&[("start_month", &local_var_str.to_string())]);
80    }
81    if let Some(ref local_var_str) = end_month {
82        local_var_req_builder = local_var_req_builder.query(&[("end_month", &local_var_str.to_string())]);
83    }
84    if let Some(ref local_var_str) = limit {
85        local_var_req_builder = local_var_req_builder.query(&[("limit", &local_var_str.to_string())]);
86    }
87    if let Some(ref local_var_str) = cursor {
88        local_var_req_builder = local_var_req_builder.query(&[("cursor", &local_var_str.to_string())]);
89    }
90    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
91        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
92    }
93    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
94        let local_var_key = local_var_apikey.key.clone();
95        let local_var_value = match local_var_apikey.prefix {
96            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
97            None => local_var_key,
98        };
99        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
100    };
101
102    let local_var_req = local_var_req_builder.build()?;
103    let local_var_resp = local_var_client.execute(local_var_req).await?;
104
105    if "GET" != "GET" && "GET" != "HEAD" {
106      let headers = local_var_resp.headers();
107      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
108          Some(v) => v.to_str().unwrap().parse().unwrap(),
109          None => configuration::DEFAULT_RATELIMIT,
110      };
111      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
112          Some(v) => v.to_str().unwrap().parse().unwrap(),
113          None => 0,
114      };
115    }
116
117    let local_var_status = local_var_resp.status();
118    let local_var_content = local_var_resp.text().await?;
119
120    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
121        serde_json::from_str(&local_var_content).map_err(Error::from)
122    } else {
123        let local_var_entity: Option<GetServiceLevelUsageError> = serde_json::from_str(&local_var_content).ok();
124        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
125        Err(Error::ResponseError(local_var_error))
126    }
127}
128
129/// Returns monthly usage metrics for customer by product.
130pub async fn get_usage_metrics(configuration: &mut configuration::Configuration, params: GetUsageMetricsParams) -> Result<crate::models::Usagemetric, Error<GetUsageMetricsError>> {
131    let local_var_configuration = configuration;
132
133    // unbox the parameters
134    let start_month = params.start_month;
135    let end_month = params.end_month;
136
137
138    let local_var_client = &local_var_configuration.client;
139
140    let local_var_uri_str = format!("{}/billing/v3/usage-metrics", local_var_configuration.base_path);
141    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
142
143    if let Some(ref local_var_str) = start_month {
144        local_var_req_builder = local_var_req_builder.query(&[("start_month", &local_var_str.to_string())]);
145    }
146    if let Some(ref local_var_str) = end_month {
147        local_var_req_builder = local_var_req_builder.query(&[("end_month", &local_var_str.to_string())]);
148    }
149    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
150        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
151    }
152    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
153        let local_var_key = local_var_apikey.key.clone();
154        let local_var_value = match local_var_apikey.prefix {
155            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
156            None => local_var_key,
157        };
158        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
159    };
160
161    let local_var_req = local_var_req_builder.build()?;
162    let local_var_resp = local_var_client.execute(local_var_req).await?;
163
164    if "GET" != "GET" && "GET" != "HEAD" {
165      let headers = local_var_resp.headers();
166      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
167          Some(v) => v.to_str().unwrap().parse().unwrap(),
168          None => configuration::DEFAULT_RATELIMIT,
169      };
170      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
171          Some(v) => v.to_str().unwrap().parse().unwrap(),
172          None => 0,
173      };
174    }
175
176    let local_var_status = local_var_resp.status();
177    let local_var_content = local_var_resp.text().await?;
178
179    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
180        serde_json::from_str(&local_var_content).map_err(Error::from)
181    } else {
182        let local_var_entity: Option<GetUsageMetricsError> = serde_json::from_str(&local_var_content).ok();
183        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
184        Err(Error::ResponseError(local_var_error))
185    }
186}
187