fastly_api/apis/
kv_store_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 [`kv_store_create`]
15#[derive(Clone, Debug, Default)]
16pub struct KvStoreCreateParams {
17    pub location: Option<String>,
18    pub kv_store_request_create_or_update: Option<crate::models::KvStoreRequestCreateOrUpdate>
19}
20
21/// struct for passing parameters to the method [`kv_store_delete`]
22#[derive(Clone, Debug, Default)]
23pub struct KvStoreDeleteParams {
24    pub store_id: String
25}
26
27/// struct for passing parameters to the method [`kv_store_get`]
28#[derive(Clone, Debug, Default)]
29pub struct KvStoreGetParams {
30    pub store_id: String
31}
32
33/// struct for passing parameters to the method [`kv_store_list`]
34#[derive(Clone, Debug, Default)]
35pub struct KvStoreListParams {
36    pub cursor: Option<String>,
37    pub limit: Option<i32>,
38    /// Returns a one-element array containing the details for the named KV store.
39    pub name: Option<String>
40}
41
42/// struct for passing parameters to the method [`kv_store_put`]
43#[derive(Clone, Debug, Default)]
44pub struct KvStorePutParams {
45    pub store_id: String,
46    pub kv_store_request_create_or_update: Option<crate::models::KvStoreRequestCreateOrUpdate>
47}
48
49
50/// struct for typed errors of method [`kv_store_create`]
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum KvStoreCreateError {
54    Status400(),
55    UnknownValue(serde_json::Value),
56}
57
58/// struct for typed errors of method [`kv_store_delete`]
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(untagged)]
61pub enum KvStoreDeleteError {
62    Status404(),
63    Status409(),
64    UnknownValue(serde_json::Value),
65}
66
67/// struct for typed errors of method [`kv_store_get`]
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(untagged)]
70pub enum KvStoreGetError {
71    Status404(),
72    UnknownValue(serde_json::Value),
73}
74
75/// struct for typed errors of method [`kv_store_list`]
76#[derive(Debug, Clone, Serialize, Deserialize)]
77#[serde(untagged)]
78pub enum KvStoreListError {
79    UnknownValue(serde_json::Value),
80}
81
82/// struct for typed errors of method [`kv_store_put`]
83#[derive(Debug, Clone, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum KvStorePutError {
86    Status400(),
87    Status404(),
88    UnknownValue(serde_json::Value),
89}
90
91
92/// Create a KV store.
93pub async fn kv_store_create(configuration: &mut configuration::Configuration, params: KvStoreCreateParams) -> Result<crate::models::KvStoreDetails, Error<KvStoreCreateError>> {
94    let local_var_configuration = configuration;
95
96    // unbox the parameters
97    let location = params.location;
98    let kv_store_request_create_or_update = params.kv_store_request_create_or_update;
99
100
101    let local_var_client = &local_var_configuration.client;
102
103    let local_var_uri_str = format!("{}/resources/stores/kv", local_var_configuration.base_path);
104    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
105
106    if let Some(ref local_var_str) = location {
107        local_var_req_builder = local_var_req_builder.query(&[("location", &local_var_str.to_string())]);
108    }
109    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
110        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
111    }
112    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
113        let local_var_key = local_var_apikey.key.clone();
114        let local_var_value = match local_var_apikey.prefix {
115            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
116            None => local_var_key,
117        };
118        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
119    };
120    local_var_req_builder = local_var_req_builder.json(&kv_store_request_create_or_update);
121
122    let local_var_req = local_var_req_builder.build()?;
123    let local_var_resp = local_var_client.execute(local_var_req).await?;
124
125    if "POST" != "GET" && "POST" != "HEAD" {
126      let headers = local_var_resp.headers();
127      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
128          Some(v) => v.to_str().unwrap().parse().unwrap(),
129          None => configuration::DEFAULT_RATELIMIT,
130      };
131      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
132          Some(v) => v.to_str().unwrap().parse().unwrap(),
133          None => 0,
134      };
135    }
136
137    let local_var_status = local_var_resp.status();
138    let local_var_content = local_var_resp.text().await?;
139
140    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
141        serde_json::from_str(&local_var_content).map_err(Error::from)
142    } else {
143        let local_var_entity: Option<KvStoreCreateError> = serde_json::from_str(&local_var_content).ok();
144        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
145        Err(Error::ResponseError(local_var_error))
146    }
147}
148
149/// A KV store must be empty before it can be deleted. Attempting to delete a KV store that contains items will result in a response with a `409` status code.
150pub async fn kv_store_delete(configuration: &mut configuration::Configuration, params: KvStoreDeleteParams) -> Result<(), Error<KvStoreDeleteError>> {
151    let local_var_configuration = configuration;
152
153    // unbox the parameters
154    let store_id = params.store_id;
155
156
157    let local_var_client = &local_var_configuration.client;
158
159    let local_var_uri_str = format!("{}/resources/stores/kv/{store_id}", local_var_configuration.base_path, store_id=crate::apis::urlencode(store_id));
160    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
161
162    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
163        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
164    }
165    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
166        let local_var_key = local_var_apikey.key.clone();
167        let local_var_value = match local_var_apikey.prefix {
168            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
169            None => local_var_key,
170        };
171        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
172    };
173
174    let local_var_req = local_var_req_builder.build()?;
175    let local_var_resp = local_var_client.execute(local_var_req).await?;
176
177    if "DELETE" != "GET" && "DELETE" != "HEAD" {
178      let headers = local_var_resp.headers();
179      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
180          Some(v) => v.to_str().unwrap().parse().unwrap(),
181          None => configuration::DEFAULT_RATELIMIT,
182      };
183      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
184          Some(v) => v.to_str().unwrap().parse().unwrap(),
185          None => 0,
186      };
187    }
188
189    let local_var_status = local_var_resp.status();
190    let local_var_content = local_var_resp.text().await?;
191
192    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
193        Ok(())
194    } else {
195        let local_var_entity: Option<KvStoreDeleteError> = serde_json::from_str(&local_var_content).ok();
196        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
197        Err(Error::ResponseError(local_var_error))
198    }
199}
200
201/// Get details of a KV store.
202pub async fn kv_store_get(configuration: &mut configuration::Configuration, params: KvStoreGetParams) -> Result<crate::models::KvStoreDetails, Error<KvStoreGetError>> {
203    let local_var_configuration = configuration;
204
205    // unbox the parameters
206    let store_id = params.store_id;
207
208
209    let local_var_client = &local_var_configuration.client;
210
211    let local_var_uri_str = format!("{}/resources/stores/kv/{store_id}", local_var_configuration.base_path, store_id=crate::apis::urlencode(store_id));
212    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
213
214    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
215        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
216    }
217    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
218        let local_var_key = local_var_apikey.key.clone();
219        let local_var_value = match local_var_apikey.prefix {
220            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
221            None => local_var_key,
222        };
223        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
224    };
225
226    let local_var_req = local_var_req_builder.build()?;
227    let local_var_resp = local_var_client.execute(local_var_req).await?;
228
229    if "GET" != "GET" && "GET" != "HEAD" {
230      let headers = local_var_resp.headers();
231      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
232          Some(v) => v.to_str().unwrap().parse().unwrap(),
233          None => configuration::DEFAULT_RATELIMIT,
234      };
235      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
236          Some(v) => v.to_str().unwrap().parse().unwrap(),
237          None => 0,
238      };
239    }
240
241    let local_var_status = local_var_resp.status();
242    let local_var_content = local_var_resp.text().await?;
243
244    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
245        serde_json::from_str(&local_var_content).map_err(Error::from)
246    } else {
247        let local_var_entity: Option<KvStoreGetError> = serde_json::from_str(&local_var_content).ok();
248        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
249        Err(Error::ResponseError(local_var_error))
250    }
251}
252
253/// List all KV stores.
254pub async fn kv_store_list(configuration: &mut configuration::Configuration, params: KvStoreListParams) -> Result<crate::models::InlineResponse2007, Error<KvStoreListError>> {
255    let local_var_configuration = configuration;
256
257    // unbox the parameters
258    let cursor = params.cursor;
259    let limit = params.limit;
260    let name = params.name;
261
262
263    let local_var_client = &local_var_configuration.client;
264
265    let local_var_uri_str = format!("{}/resources/stores/kv", local_var_configuration.base_path);
266    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
267
268    if let Some(ref local_var_str) = cursor {
269        local_var_req_builder = local_var_req_builder.query(&[("cursor", &local_var_str.to_string())]);
270    }
271    if let Some(ref local_var_str) = limit {
272        local_var_req_builder = local_var_req_builder.query(&[("limit", &local_var_str.to_string())]);
273    }
274    if let Some(ref local_var_str) = name {
275        local_var_req_builder = local_var_req_builder.query(&[("name", &local_var_str.to_string())]);
276    }
277    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
278        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
279    }
280    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
281        let local_var_key = local_var_apikey.key.clone();
282        let local_var_value = match local_var_apikey.prefix {
283            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
284            None => local_var_key,
285        };
286        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
287    };
288
289    let local_var_req = local_var_req_builder.build()?;
290    let local_var_resp = local_var_client.execute(local_var_req).await?;
291
292    if "GET" != "GET" && "GET" != "HEAD" {
293      let headers = local_var_resp.headers();
294      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
295          Some(v) => v.to_str().unwrap().parse().unwrap(),
296          None => configuration::DEFAULT_RATELIMIT,
297      };
298      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
299          Some(v) => v.to_str().unwrap().parse().unwrap(),
300          None => 0,
301      };
302    }
303
304    let local_var_status = local_var_resp.status();
305    let local_var_content = local_var_resp.text().await?;
306
307    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
308        serde_json::from_str(&local_var_content).map_err(Error::from)
309    } else {
310        let local_var_entity: Option<KvStoreListError> = serde_json::from_str(&local_var_content).ok();
311        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
312        Err(Error::ResponseError(local_var_error))
313    }
314}
315
316/// Update the name of a KV store.
317pub async fn kv_store_put(configuration: &mut configuration::Configuration, params: KvStorePutParams) -> Result<(), Error<KvStorePutError>> {
318    let local_var_configuration = configuration;
319
320    // unbox the parameters
321    let store_id = params.store_id;
322    let kv_store_request_create_or_update = params.kv_store_request_create_or_update;
323
324
325    let local_var_client = &local_var_configuration.client;
326
327    let local_var_uri_str = format!("{}/resources/stores/kv/{store_id}", local_var_configuration.base_path, store_id=crate::apis::urlencode(store_id));
328    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
329
330    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
331        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
332    }
333    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
334        let local_var_key = local_var_apikey.key.clone();
335        let local_var_value = match local_var_apikey.prefix {
336            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
337            None => local_var_key,
338        };
339        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
340    };
341    local_var_req_builder = local_var_req_builder.json(&kv_store_request_create_or_update);
342
343    let local_var_req = local_var_req_builder.build()?;
344    let local_var_resp = local_var_client.execute(local_var_req).await?;
345
346    if "PUT" != "GET" && "PUT" != "HEAD" {
347      let headers = local_var_resp.headers();
348      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
349          Some(v) => v.to_str().unwrap().parse().unwrap(),
350          None => configuration::DEFAULT_RATELIMIT,
351      };
352      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
353          Some(v) => v.to_str().unwrap().parse().unwrap(),
354          None => 0,
355      };
356    }
357
358    let local_var_status = local_var_resp.status();
359    let local_var_content = local_var_resp.text().await?;
360
361    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
362        Ok(())
363    } else {
364        let local_var_entity: Option<KvStorePutError> = serde_json::from_str(&local_var_content).ok();
365        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
366        Err(Error::ResponseError(local_var_error))
367    }
368}
369