Skip to main content

limit_lens/apis/
rate_test_api.rs

1/*
2 * Limit Lens API
3 *
4 * A simple API for testing and visualizing rate limiters in real-time. Monitor request throughput and see how your rate limiting algorithms perform under load.
5 *
6 * The version of the OpenAPI document: 0.6.1
7 * Contact: sharmaninenine@gmail.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`create_test_session`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateTestSessionError {
22    Status500(),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`get_all_sessions`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum GetAllSessionsError {
30    UnknownValue(serde_json::Value),
31}
32
33/// struct for typed errors of method [`get_test_metrics`]
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum GetTestMetricsError {
37    Status404(),
38    UnknownValue(serde_json::Value),
39}
40
41/// struct for typed errors of method [`metrics_dashboard`]
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(untagged)]
44pub enum MetricsDashboardError {
45    UnknownValue(serde_json::Value),
46}
47
48/// struct for typed errors of method [`receive_test_request`]
49#[derive(Debug, Clone, Serialize, Deserialize)]
50#[serde(untagged)]
51pub enum ReceiveTestRequestError {
52    Status404(),
53    UnknownValue(serde_json::Value),
54}
55
56
57/// Returns a unique session ID that can be used for rate limit testing
58pub async fn create_test_session(configuration: &configuration::Configuration, create_session_request: models::CreateSessionRequest) -> Result<models::TestSession, Error<CreateTestSessionError>> {
59    // add a prefix to parameters to efficiently prevent name collisions
60    let p_create_session_request = create_session_request;
61
62    let uri_str = format!("{}/api/test/session", configuration.base_path);
63    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
64
65    if let Some(ref user_agent) = configuration.user_agent {
66        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
67    }
68    req_builder = req_builder.json(&p_create_session_request);
69
70    let req = req_builder.build()?;
71    let resp = configuration.client.execute(req).await?;
72
73    let status = resp.status();
74    let content_type = resp
75        .headers()
76        .get("content-type")
77        .and_then(|v| v.to_str().ok())
78        .unwrap_or("application/octet-stream");
79    let content_type = super::ContentType::from(content_type);
80
81    if !status.is_client_error() && !status.is_server_error() {
82        let content = resp.text().await?;
83        match content_type {
84            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
85            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TestSession`"))),
86            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TestSession`")))),
87        }
88    } else {
89        let content = resp.text().await?;
90        let entity: Option<CreateTestSessionError> = serde_json::from_str(&content).ok();
91        Err(Error::ResponseError(ResponseContent { status, content, entity }))
92    }
93}
94
95/// Returns a list of all active test sessions
96pub async fn get_all_sessions(configuration: &configuration::Configuration, ) -> Result<Vec<models::TestSession>, Error<GetAllSessionsError>> {
97
98    let uri_str = format!("{}/api/test/sessions", configuration.base_path);
99    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
100
101    if let Some(ref user_agent) = configuration.user_agent {
102        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
103    }
104
105    let req = req_builder.build()?;
106    let resp = configuration.client.execute(req).await?;
107
108    let status = resp.status();
109    let content_type = resp
110        .headers()
111        .get("content-type")
112        .and_then(|v| v.to_str().ok())
113        .unwrap_or("application/octet-stream");
114    let content_type = super::ContentType::from(content_type);
115
116    if !status.is_client_error() && !status.is_server_error() {
117        let content = resp.text().await?;
118        match content_type {
119            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
120            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::TestSession&gt;`"))),
121            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec&lt;models::TestSession&gt;`")))),
122        }
123    } else {
124        let content = resp.text().await?;
125        let entity: Option<GetAllSessionsError> = serde_json::from_str(&content).ok();
126        Err(Error::ResponseError(ResponseContent { status, content, entity }))
127    }
128}
129
130/// Returns metrics and analysis of requests received for a session
131pub async fn get_test_metrics(configuration: &configuration::Configuration, session_id: &str) -> Result<models::TestMetrics, Error<GetTestMetricsError>> {
132    // add a prefix to parameters to efficiently prevent name collisions
133    let p_session_id = session_id;
134
135    let uri_str = format!("{}/api/test/metrics/{session_id}", configuration.base_path, session_id=crate::apis::urlencode(p_session_id));
136    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
137
138    if let Some(ref user_agent) = configuration.user_agent {
139        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
140    }
141
142    let req = req_builder.build()?;
143    let resp = configuration.client.execute(req).await?;
144
145    let status = resp.status();
146    let content_type = resp
147        .headers()
148        .get("content-type")
149        .and_then(|v| v.to_str().ok())
150        .unwrap_or("application/octet-stream");
151    let content_type = super::ContentType::from(content_type);
152
153    if !status.is_client_error() && !status.is_server_error() {
154        let content = resp.text().await?;
155        match content_type {
156            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
157            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TestMetrics`"))),
158            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TestMetrics`")))),
159        }
160    } else {
161        let content = resp.text().await?;
162        let entity: Option<GetTestMetricsError> = serde_json::from_str(&content).ok();
163        Err(Error::ResponseError(ResponseContent { status, content, entity }))
164    }
165}
166
167/// Serves an HTML page that displays live metrics for all active sessions
168pub async fn metrics_dashboard(configuration: &configuration::Configuration, ) -> Result<(), Error<MetricsDashboardError>> {
169
170    let uri_str = format!("{}/dashboard", configuration.base_path);
171    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
172
173    if let Some(ref user_agent) = configuration.user_agent {
174        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
175    }
176
177    let req = req_builder.build()?;
178    let resp = configuration.client.execute(req).await?;
179
180    let status = resp.status();
181
182    if !status.is_client_error() && !status.is_server_error() {
183        Ok(())
184    } else {
185        let content = resp.text().await?;
186        let entity: Option<MetricsDashboardError> = serde_json::from_str(&content).ok();
187        Err(Error::ResponseError(ResponseContent { status, content, entity }))
188    }
189}
190
191/// Records a request for the specified session ID
192pub async fn receive_test_request(configuration: &configuration::Configuration, session_id: &str) -> Result<(), Error<ReceiveTestRequestError>> {
193    // add a prefix to parameters to efficiently prevent name collisions
194    let p_session_id = session_id;
195
196    let uri_str = format!("{}/api/test/request/{session_id}", configuration.base_path, session_id=crate::apis::urlencode(p_session_id));
197    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
198
199    if let Some(ref user_agent) = configuration.user_agent {
200        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
201    }
202
203    let req = req_builder.build()?;
204    let resp = configuration.client.execute(req).await?;
205
206    let status = resp.status();
207
208    if !status.is_client_error() && !status.is_server_error() {
209        Ok(())
210    } else {
211        let content = resp.text().await?;
212        let entity: Option<ReceiveTestRequestError> = serde_json::from_str(&content).ok();
213        Err(Error::ResponseError(ResponseContent { status, content, entity }))
214    }
215}
216