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