fastly_api/apis/
realtime_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_stats_last120_seconds`]
15#[derive(Clone, Debug, Default)]
16pub struct GetStatsLast120SecondsParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String
19}
20
21/// struct for passing parameters to the method [`get_stats_last120_seconds_limit_entries`]
22#[derive(Clone, Debug, Default)]
23pub struct GetStatsLast120SecondsLimitEntriesParams {
24    /// Alphanumeric string identifying the service.
25    pub service_id: String,
26    /// Maximum number of results to show.
27    pub max_entries: i32
28}
29
30/// struct for passing parameters to the method [`get_stats_last_second`]
31#[derive(Clone, Debug, Default)]
32pub struct GetStatsLastSecondParams {
33    /// Alphanumeric string identifying the service.
34    pub service_id: String,
35    /// Timestamp in seconds (Unix epoch time).
36    pub timestamp_in_seconds: i32
37}
38
39
40/// struct for typed errors of method [`get_stats_last120_seconds`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum GetStatsLast120SecondsError {
44    UnknownValue(serde_json::Value),
45}
46
47/// struct for typed errors of method [`get_stats_last120_seconds_limit_entries`]
48#[derive(Debug, Clone, Serialize, Deserialize)]
49#[serde(untagged)]
50pub enum GetStatsLast120SecondsLimitEntriesError {
51    UnknownValue(serde_json::Value),
52}
53
54/// struct for typed errors of method [`get_stats_last_second`]
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(untagged)]
57pub enum GetStatsLastSecondError {
58    UnknownValue(serde_json::Value),
59}
60
61
62/// Get data for the 120 seconds preceding the latest timestamp available for a service.
63pub async fn get_stats_last120_seconds(configuration: &mut configuration::Configuration, params: GetStatsLast120SecondsParams) -> Result<crate::models::Realtime, Error<GetStatsLast120SecondsError>> {
64    let local_var_configuration = configuration;
65
66    // unbox the parameters
67    let service_id = params.service_id;
68
69
70    let local_var_client = &local_var_configuration.client;
71
72    let local_var_uri_str = format!("{}/v1/channel/{service_id}/ts/h", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id));
73    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
74
75    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
76        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
77    }
78    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
79        let local_var_key = local_var_apikey.key.clone();
80        let local_var_value = match local_var_apikey.prefix {
81            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
82            None => local_var_key,
83        };
84        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
85    };
86
87    let local_var_req = local_var_req_builder.build()?;
88    let local_var_resp = local_var_client.execute(local_var_req).await?;
89
90    if "GET" != "GET" && "GET" != "HEAD" {
91      let headers = local_var_resp.headers();
92      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
93          Some(v) => v.to_str().unwrap().parse().unwrap(),
94          None => configuration::DEFAULT_RATELIMIT,
95      };
96      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
97          Some(v) => v.to_str().unwrap().parse().unwrap(),
98          None => 0,
99      };
100    }
101
102    let local_var_status = local_var_resp.status();
103    let local_var_content = local_var_resp.text().await?;
104
105    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
106        serde_json::from_str(&local_var_content).map_err(Error::from)
107    } else {
108        let local_var_entity: Option<GetStatsLast120SecondsError> = serde_json::from_str(&local_var_content).ok();
109        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
110        Err(Error::ResponseError(local_var_error))
111    }
112}
113
114/// Get data for the 120 seconds preceding the latest timestamp available for a service, up to a maximum of `max_entries` entries.
115pub async fn get_stats_last120_seconds_limit_entries(configuration: &mut configuration::Configuration, params: GetStatsLast120SecondsLimitEntriesParams) -> Result<crate::models::Realtime, Error<GetStatsLast120SecondsLimitEntriesError>> {
116    let local_var_configuration = configuration;
117
118    // unbox the parameters
119    let service_id = params.service_id;
120    let max_entries = params.max_entries;
121
122
123    let local_var_client = &local_var_configuration.client;
124
125    let local_var_uri_str = format!("{}/v1/channel/{service_id}/ts/h/limit/{max_entries}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), max_entries=max_entries);
126    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
127
128    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
129        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
130    }
131    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
132        let local_var_key = local_var_apikey.key.clone();
133        let local_var_value = match local_var_apikey.prefix {
134            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
135            None => local_var_key,
136        };
137        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
138    };
139
140    let local_var_req = local_var_req_builder.build()?;
141    let local_var_resp = local_var_client.execute(local_var_req).await?;
142
143    if "GET" != "GET" && "GET" != "HEAD" {
144      let headers = local_var_resp.headers();
145      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
146          Some(v) => v.to_str().unwrap().parse().unwrap(),
147          None => configuration::DEFAULT_RATELIMIT,
148      };
149      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
150          Some(v) => v.to_str().unwrap().parse().unwrap(),
151          None => 0,
152      };
153    }
154
155    let local_var_status = local_var_resp.status();
156    let local_var_content = local_var_resp.text().await?;
157
158    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
159        serde_json::from_str(&local_var_content).map_err(Error::from)
160    } else {
161        let local_var_entity: Option<GetStatsLast120SecondsLimitEntriesError> = serde_json::from_str(&local_var_content).ok();
162        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
163        Err(Error::ResponseError(local_var_error))
164    }
165}
166
167/// Get real-time data for the specified reporting period. Specify `0` to get a single entry for the last complete second. The `Timestamp` field included in the response provides the time index of the latest entry in the dataset and can be provided as the `start_timestamp` of the next request for a seamless continuation of the dataset from one request to the next.
168pub async fn get_stats_last_second(configuration: &mut configuration::Configuration, params: GetStatsLastSecondParams) -> Result<crate::models::Realtime, Error<GetStatsLastSecondError>> {
169    let local_var_configuration = configuration;
170
171    // unbox the parameters
172    let service_id = params.service_id;
173    let timestamp_in_seconds = params.timestamp_in_seconds;
174
175
176    let local_var_client = &local_var_configuration.client;
177
178    let local_var_uri_str = format!("{}/v1/channel/{service_id}/ts/{timestamp_in_seconds}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), timestamp_in_seconds=timestamp_in_seconds);
179    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
180
181    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
182        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
183    }
184    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
185        let local_var_key = local_var_apikey.key.clone();
186        let local_var_value = match local_var_apikey.prefix {
187            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
188            None => local_var_key,
189        };
190        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
191    };
192
193    let local_var_req = local_var_req_builder.build()?;
194    let local_var_resp = local_var_client.execute(local_var_req).await?;
195
196    if "GET" != "GET" && "GET" != "HEAD" {
197      let headers = local_var_resp.headers();
198      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
199          Some(v) => v.to_str().unwrap().parse().unwrap(),
200          None => configuration::DEFAULT_RATELIMIT,
201      };
202      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
203          Some(v) => v.to_str().unwrap().parse().unwrap(),
204          None => 0,
205      };
206    }
207
208    let local_var_status = local_var_resp.status();
209    let local_var_content = local_var_resp.text().await?;
210
211    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
212        serde_json::from_str(&local_var_content).map_err(Error::from)
213    } else {
214        let local_var_entity: Option<GetStatsLastSecondError> = serde_json::from_str(&local_var_content).ok();
215        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
216        Err(Error::ResponseError(local_var_error))
217    }
218}
219