typesense_rs/apis/
operations_api.rs

1// Typesense API
2//
3// An open source search engine for building delightful search experiences.
4//
5// The version of the OpenAPI document: 27.0
6//
7// Generated by: https://openapi-generator.tech
8
9use std::sync::Arc;
10
11use async_trait::async_trait;
12use reqwest;
13use serde::{Deserialize, Serialize};
14
15use super::{configuration, Error};
16use crate::apis::ResponseContent;
17use crate::models;
18
19#[async_trait]
20pub trait OperationsApi: Send + Sync {
21	async fn retrieve_api_stats(&self) -> Result<models::ApiStatsResponse, Error<RetrieveApiStatsError>>;
22	async fn retrieve_metrics(&self) -> Result<serde_json::Value, Error<RetrieveMetricsError>>;
23	async fn take_snapshot(&self, params: TakeSnapshotParams) -> Result<models::SuccessStatus, Error<TakeSnapshotError>>;
24	async fn vote(&self) -> Result<models::SuccessStatus, Error<VoteError>>;
25}
26
27pub struct OperationsApiClient {
28	configuration: Arc<configuration::Configuration>,
29}
30
31impl OperationsApiClient {
32	pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
33		Self { configuration }
34	}
35}
36
37/// struct for passing parameters to the method [`take_snapshot`]
38#[derive(Clone, Debug)]
39#[cfg_attr(feature = "bon", derive(::bon::Builder))]
40pub struct TakeSnapshotParams {
41	/// The directory on the server where the snapshot should be saved.
42	pub snapshot_path: String,
43}
44
45#[async_trait]
46impl OperationsApi for OperationsApiClient {
47	/// Retrieve the stats about API endpoints.
48	async fn retrieve_api_stats(&self) -> Result<models::ApiStatsResponse, Error<RetrieveApiStatsError>> {
49		let local_var_configuration = &self.configuration;
50
51		let local_var_client = &local_var_configuration.client;
52
53		let local_var_uri_str = format!("{}/stats.json", local_var_configuration.base_path);
54		let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
55
56		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
57			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
58		}
59		if let Some(ref local_var_apikey) = local_var_configuration.api_key {
60			let local_var_key = local_var_apikey.key.clone();
61			let local_var_value = match local_var_apikey.prefix {
62				Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
63				None => local_var_key,
64			};
65			local_var_req_builder = local_var_req_builder.header("X-TYPESENSE-API-KEY", local_var_value);
66		};
67
68		let local_var_req = local_var_req_builder.build()?;
69		let local_var_resp = local_var_client.execute(local_var_req).await?;
70
71		let local_var_status = local_var_resp.status();
72		let local_var_content = local_var_resp.text().await?;
73
74		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
75			serde_json::from_str(&local_var_content).map_err(Error::from)
76		} else {
77			let local_var_entity: Option<RetrieveApiStatsError> = serde_json::from_str(&local_var_content).ok();
78			let local_var_error = ResponseContent {
79				status: local_var_status,
80				content: local_var_content,
81				entity: local_var_entity,
82			};
83			Err(Error::ResponseError(local_var_error))
84		}
85	}
86
87	/// Retrieve the metrics.
88	async fn retrieve_metrics(&self) -> Result<serde_json::Value, Error<RetrieveMetricsError>> {
89		let local_var_configuration = &self.configuration;
90
91		let local_var_client = &local_var_configuration.client;
92
93		let local_var_uri_str = format!("{}/metrics.json", local_var_configuration.base_path);
94		let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
95
96		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
97			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
98		}
99		if let Some(ref local_var_apikey) = local_var_configuration.api_key {
100			let local_var_key = local_var_apikey.key.clone();
101			let local_var_value = match local_var_apikey.prefix {
102				Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
103				None => local_var_key,
104			};
105			local_var_req_builder = local_var_req_builder.header("X-TYPESENSE-API-KEY", local_var_value);
106		};
107
108		let local_var_req = local_var_req_builder.build()?;
109		let local_var_resp = local_var_client.execute(local_var_req).await?;
110
111		let local_var_status = local_var_resp.status();
112		let local_var_content = local_var_resp.text().await?;
113
114		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
115			serde_json::from_str(&local_var_content).map_err(Error::from)
116		} else {
117			let local_var_entity: Option<RetrieveMetricsError> = serde_json::from_str(&local_var_content).ok();
118			let local_var_error = ResponseContent {
119				status: local_var_status,
120				content: local_var_content,
121				entity: local_var_entity,
122			};
123			Err(Error::ResponseError(local_var_error))
124		}
125	}
126
127	/// Creates a point-in-time snapshot of a Typesense node's state and data in
128	/// the specified directory. You can then backup the snapshot directory that
129	/// gets created and later restore it as a data directory, as needed.
130	async fn take_snapshot(&self, params: TakeSnapshotParams) -> Result<models::SuccessStatus, Error<TakeSnapshotError>> {
131		let TakeSnapshotParams { snapshot_path } = params;
132
133		let local_var_configuration = &self.configuration;
134
135		let local_var_client = &local_var_configuration.client;
136
137		let local_var_uri_str = format!("{}/operations/snapshot", local_var_configuration.base_path);
138		let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
139
140		local_var_req_builder = local_var_req_builder.query(&[("snapshot_path", &snapshot_path.to_string())]);
141		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
142			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
143		}
144		if let Some(ref local_var_apikey) = local_var_configuration.api_key {
145			let local_var_key = local_var_apikey.key.clone();
146			let local_var_value = match local_var_apikey.prefix {
147				Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
148				None => local_var_key,
149			};
150			local_var_req_builder = local_var_req_builder.header("X-TYPESENSE-API-KEY", local_var_value);
151		};
152
153		let local_var_req = local_var_req_builder.build()?;
154		let local_var_resp = local_var_client.execute(local_var_req).await?;
155
156		let local_var_status = local_var_resp.status();
157		let local_var_content = local_var_resp.text().await?;
158
159		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
160			serde_json::from_str(&local_var_content).map_err(Error::from)
161		} else {
162			let local_var_entity: Option<TakeSnapshotError> = serde_json::from_str(&local_var_content).ok();
163			let local_var_error = ResponseContent {
164				status: local_var_status,
165				content: local_var_content,
166				entity: local_var_entity,
167			};
168			Err(Error::ResponseError(local_var_error))
169		}
170	}
171
172	/// Triggers a follower node to initiate the raft voting process, which
173	/// triggers leader re-election. The follower node that you run this
174	/// operation against will become the new leader, once this command
175	/// succeeds.
176	async fn vote(&self) -> Result<models::SuccessStatus, Error<VoteError>> {
177		let local_var_configuration = &self.configuration;
178
179		let local_var_client = &local_var_configuration.client;
180
181		let local_var_uri_str = format!("{}/operations/vote", local_var_configuration.base_path);
182		let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
183
184		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
185			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
186		}
187		if let Some(ref local_var_apikey) = local_var_configuration.api_key {
188			let local_var_key = local_var_apikey.key.clone();
189			let local_var_value = match local_var_apikey.prefix {
190				Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
191				None => local_var_key,
192			};
193			local_var_req_builder = local_var_req_builder.header("X-TYPESENSE-API-KEY", local_var_value);
194		};
195
196		let local_var_req = local_var_req_builder.build()?;
197		let local_var_resp = local_var_client.execute(local_var_req).await?;
198
199		let local_var_status = local_var_resp.status();
200		let local_var_content = local_var_resp.text().await?;
201
202		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
203			serde_json::from_str(&local_var_content).map_err(Error::from)
204		} else {
205			let local_var_entity: Option<VoteError> = serde_json::from_str(&local_var_content).ok();
206			let local_var_error = ResponseContent {
207				status: local_var_status,
208				content: local_var_content,
209				entity: local_var_entity,
210			};
211			Err(Error::ResponseError(local_var_error))
212		}
213	}
214}
215
216/// struct for typed errors of method [`retrieve_api_stats`]
217#[derive(Debug, Clone, Serialize, Deserialize)]
218#[serde(untagged)]
219pub enum RetrieveApiStatsError {
220	UnknownValue(serde_json::Value),
221}
222
223/// struct for typed errors of method [`retrieve_metrics`]
224#[derive(Debug, Clone, Serialize, Deserialize)]
225#[serde(untagged)]
226pub enum RetrieveMetricsError {
227	UnknownValue(serde_json::Value),
228}
229
230/// struct for typed errors of method [`take_snapshot`]
231#[derive(Debug, Clone, Serialize, Deserialize)]
232#[serde(untagged)]
233pub enum TakeSnapshotError {
234	UnknownValue(serde_json::Value),
235}
236
237/// struct for typed errors of method [`vote`]
238#[derive(Debug, Clone, Serialize, Deserialize)]
239#[serde(untagged)]
240pub enum VoteError {
241	UnknownValue(serde_json::Value),
242}