fastly_api/apis/
metrics_platform_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_platform_metrics_service_historical`]
15#[derive(Clone, Debug, Default)]
16pub struct GetPlatformMetricsServiceHistoricalParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    /// Duration of sample windows.
20    pub granularity: String,
21    /// A valid RFC-8339-formatted date and time indicating the inclusive start of the query time range. If not provided, a default is chosen based on the provided `granularity` value.
22    pub from: Option<String>,
23    /// A valid RFC-8339-formatted date and time indicating the exclusive end of the query time range. If not provided, a default is chosen based on the provided `granularity` value.
24    pub to: Option<String>,
25    /// The metric(s) to retrieve. Multiple values should be comma-separated.
26    pub metric: Option<String>,
27    /// The metric set(s) to retrieve. Multiple values should be comma-separated.
28    pub metric_set: Option<String>,
29    /// Field to group_by in the query. For example, `group_by=region` will return entries for grouped by timestamp and region. 
30    pub group_by: Option<String>,
31    /// Limit query to one or more specific geographic regions. Values should be comma-separated. 
32    pub region: Option<String>,
33    /// Limit query to one or more specific POPs. Values should be comma-separated.
34    pub datacenter: Option<String>,
35    /// 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.
36    pub cursor: Option<String>,
37    /// Number of results per page. The maximum is 10000.
38    pub limit: Option<String>
39}
40
41
42/// struct for typed errors of method [`get_platform_metrics_service_historical`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum GetPlatformMetricsServiceHistoricalError {
46    UnknownValue(serde_json::Value),
47}
48
49
50/// Fetches historical metrics for a single service for a given granularity.
51pub async fn get_platform_metrics_service_historical(configuration: &mut configuration::Configuration, params: GetPlatformMetricsServiceHistoricalParams) -> Result<crate::models::PlatformMetricsResponse, Error<GetPlatformMetricsServiceHistoricalError>> {
52    let local_var_configuration = configuration;
53
54    // unbox the parameters
55    let service_id = params.service_id;
56    let granularity = params.granularity;
57    let from = params.from;
58    let to = params.to;
59    let metric = params.metric;
60    let metric_set = params.metric_set;
61    let group_by = params.group_by;
62    let region = params.region;
63    let datacenter = params.datacenter;
64    let cursor = params.cursor;
65    let limit = params.limit;
66
67
68    let local_var_client = &local_var_configuration.client;
69
70    let local_var_uri_str = format!("{}/metrics/platform/services/{service_id}/{granularity}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), granularity=crate::apis::urlencode(granularity));
71    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
72
73    if let Some(ref local_var_str) = from {
74        local_var_req_builder = local_var_req_builder.query(&[("from", &local_var_str.to_string())]);
75    }
76    if let Some(ref local_var_str) = to {
77        local_var_req_builder = local_var_req_builder.query(&[("to", &local_var_str.to_string())]);
78    }
79    if let Some(ref local_var_str) = metric {
80        local_var_req_builder = local_var_req_builder.query(&[("metric", &local_var_str.to_string())]);
81    }
82    if let Some(ref local_var_str) = metric_set {
83        local_var_req_builder = local_var_req_builder.query(&[("metric_set", &local_var_str.to_string())]);
84    }
85    if let Some(ref local_var_str) = group_by {
86        local_var_req_builder = local_var_req_builder.query(&[("group_by", &local_var_str.to_string())]);
87    }
88    if let Some(ref local_var_str) = region {
89        local_var_req_builder = local_var_req_builder.query(&[("region", &local_var_str.to_string())]);
90    }
91    if let Some(ref local_var_str) = datacenter {
92        local_var_req_builder = local_var_req_builder.query(&[("datacenter", &local_var_str.to_string())]);
93    }
94    if let Some(ref local_var_str) = cursor {
95        local_var_req_builder = local_var_req_builder.query(&[("cursor", &local_var_str.to_string())]);
96    }
97    if let Some(ref local_var_str) = limit {
98        local_var_req_builder = local_var_req_builder.query(&[("limit", &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<GetPlatformMetricsServiceHistoricalError> = 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