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: Option<crate::models::KvStoreRequestCreate>
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}
39
40
41/// struct for typed errors of method [`kv_store_create`]
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(untagged)]
44pub enum KvStoreCreateError {
45    Status400(),
46    UnknownValue(serde_json::Value),
47}
48
49/// struct for typed errors of method [`kv_store_delete`]
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(untagged)]
52pub enum KvStoreDeleteError {
53    Status404(),
54    Status409(),
55    UnknownValue(serde_json::Value),
56}
57
58/// struct for typed errors of method [`kv_store_get`]
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(untagged)]
61pub enum KvStoreGetError {
62    Status404(),
63    UnknownValue(serde_json::Value),
64}
65
66/// struct for typed errors of method [`kv_store_list`]
67#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(untagged)]
69pub enum KvStoreListError {
70    UnknownValue(serde_json::Value),
71}
72
73
74/// Create a KV store.
75pub async fn kv_store_create(configuration: &mut configuration::Configuration, params: KvStoreCreateParams) -> Result<crate::models::KvStoreDetails, Error<KvStoreCreateError>> {
76    let local_var_configuration = configuration;
77
78    // unbox the parameters
79    let location = params.location;
80    let kv_store_request_create = params.kv_store_request_create;
81
82
83    let local_var_client = &local_var_configuration.client;
84
85    let local_var_uri_str = format!("{}/resources/stores/kv", local_var_configuration.base_path);
86    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
87
88    if let Some(ref local_var_str) = location {
89        local_var_req_builder = local_var_req_builder.query(&[("location", &local_var_str.to_string())]);
90    }
91    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
92        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
93    }
94    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
95        let local_var_key = local_var_apikey.key.clone();
96        let local_var_value = match local_var_apikey.prefix {
97            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
98            None => local_var_key,
99        };
100        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
101    };
102    local_var_req_builder = local_var_req_builder.json(&kv_store_request_create);
103
104    let local_var_req = local_var_req_builder.build()?;
105    let local_var_resp = local_var_client.execute(local_var_req).await?;
106
107    if "POST" != "GET" && "POST" != "HEAD" {
108      let headers = local_var_resp.headers();
109      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
110          Some(v) => v.to_str().unwrap().parse().unwrap(),
111          None => configuration::DEFAULT_RATELIMIT,
112      };
113      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
114          Some(v) => v.to_str().unwrap().parse().unwrap(),
115          None => 0,
116      };
117    }
118
119    let local_var_status = local_var_resp.status();
120    let local_var_content = local_var_resp.text().await?;
121
122    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
123        serde_json::from_str(&local_var_content).map_err(Error::from)
124    } else {
125        let local_var_entity: Option<KvStoreCreateError> = serde_json::from_str(&local_var_content).ok();
126        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
127        Err(Error::ResponseError(local_var_error))
128    }
129}
130
131/// 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.
132pub async fn kv_store_delete(configuration: &mut configuration::Configuration, params: KvStoreDeleteParams) -> Result<(), Error<KvStoreDeleteError>> {
133    let local_var_configuration = configuration;
134
135    // unbox the parameters
136    let store_id = params.store_id;
137
138
139    let local_var_client = &local_var_configuration.client;
140
141    let local_var_uri_str = format!("{}/resources/stores/kv/{store_id}", local_var_configuration.base_path, store_id=crate::apis::urlencode(store_id));
142    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
143
144    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
145        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
146    }
147    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
148        let local_var_key = local_var_apikey.key.clone();
149        let local_var_value = match local_var_apikey.prefix {
150            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
151            None => local_var_key,
152        };
153        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
154    };
155
156    let local_var_req = local_var_req_builder.build()?;
157    let local_var_resp = local_var_client.execute(local_var_req).await?;
158
159    if "DELETE" != "GET" && "DELETE" != "HEAD" {
160      let headers = local_var_resp.headers();
161      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
162          Some(v) => v.to_str().unwrap().parse().unwrap(),
163          None => configuration::DEFAULT_RATELIMIT,
164      };
165      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
166          Some(v) => v.to_str().unwrap().parse().unwrap(),
167          None => 0,
168      };
169    }
170
171    let local_var_status = local_var_resp.status();
172    let local_var_content = local_var_resp.text().await?;
173
174    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
175        Ok(())
176    } else {
177        let local_var_entity: Option<KvStoreDeleteError> = serde_json::from_str(&local_var_content).ok();
178        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
179        Err(Error::ResponseError(local_var_error))
180    }
181}
182
183/// Get details of a KV store.
184pub async fn kv_store_get(configuration: &mut configuration::Configuration, params: KvStoreGetParams) -> Result<crate::models::KvStoreDetails, Error<KvStoreGetError>> {
185    let local_var_configuration = configuration;
186
187    // unbox the parameters
188    let store_id = params.store_id;
189
190
191    let local_var_client = &local_var_configuration.client;
192
193    let local_var_uri_str = format!("{}/resources/stores/kv/{store_id}", local_var_configuration.base_path, store_id=crate::apis::urlencode(store_id));
194    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
195
196    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
197        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
198    }
199    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
200        let local_var_key = local_var_apikey.key.clone();
201        let local_var_value = match local_var_apikey.prefix {
202            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
203            None => local_var_key,
204        };
205        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
206    };
207
208    let local_var_req = local_var_req_builder.build()?;
209    let local_var_resp = local_var_client.execute(local_var_req).await?;
210
211    if "GET" != "GET" && "GET" != "HEAD" {
212      let headers = local_var_resp.headers();
213      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
214          Some(v) => v.to_str().unwrap().parse().unwrap(),
215          None => configuration::DEFAULT_RATELIMIT,
216      };
217      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
218          Some(v) => v.to_str().unwrap().parse().unwrap(),
219          None => 0,
220      };
221    }
222
223    let local_var_status = local_var_resp.status();
224    let local_var_content = local_var_resp.text().await?;
225
226    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
227        serde_json::from_str(&local_var_content).map_err(Error::from)
228    } else {
229        let local_var_entity: Option<KvStoreGetError> = serde_json::from_str(&local_var_content).ok();
230        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
231        Err(Error::ResponseError(local_var_error))
232    }
233}
234
235/// List all KV stores.
236pub async fn kv_store_list(configuration: &mut configuration::Configuration, params: KvStoreListParams) -> Result<crate::models::InlineResponse2003, Error<KvStoreListError>> {
237    let local_var_configuration = configuration;
238
239    // unbox the parameters
240    let cursor = params.cursor;
241    let limit = params.limit;
242
243
244    let local_var_client = &local_var_configuration.client;
245
246    let local_var_uri_str = format!("{}/resources/stores/kv", local_var_configuration.base_path);
247    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
248
249    if let Some(ref local_var_str) = cursor {
250        local_var_req_builder = local_var_req_builder.query(&[("cursor", &local_var_str.to_string())]);
251    }
252    if let Some(ref local_var_str) = limit {
253        local_var_req_builder = local_var_req_builder.query(&[("limit", &local_var_str.to_string())]);
254    }
255    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
256        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
257    }
258    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
259        let local_var_key = local_var_apikey.key.clone();
260        let local_var_value = match local_var_apikey.prefix {
261            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
262            None => local_var_key,
263        };
264        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
265    };
266
267    let local_var_req = local_var_req_builder.build()?;
268    let local_var_resp = local_var_client.execute(local_var_req).await?;
269
270    if "GET" != "GET" && "GET" != "HEAD" {
271      let headers = local_var_resp.headers();
272      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
273          Some(v) => v.to_str().unwrap().parse().unwrap(),
274          None => configuration::DEFAULT_RATELIMIT,
275      };
276      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
277          Some(v) => v.to_str().unwrap().parse().unwrap(),
278          None => 0,
279      };
280    }
281
282    let local_var_status = local_var_resp.status();
283    let local_var_content = local_var_resp.text().await?;
284
285    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
286        serde_json::from_str(&local_var_content).map_err(Error::from)
287    } else {
288        let local_var_entity: Option<KvStoreListError> = serde_json::from_str(&local_var_content).ok();
289        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
290        Err(Error::ResponseError(local_var_error))
291    }
292}
293