fastly_api/apis/
cache_settings_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 [`create_cache_settings`]
15#[derive(Clone, Debug, Default)]
16pub struct CreateCacheSettingsParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    /// Integer identifying a service version.
20    pub version_id: i32,
21    /// If set, will cause vcl_fetch to terminate after processing this rule with the return state specified. If not set, other configuration logic in vcl_fetch with a lower priority will run after this rule. 
22    pub action: Option<String>,
23    /// Name of the cache condition controlling when this configuration applies.
24    pub cache_condition: Option<String>,
25    /// Name for the cache settings object.
26    pub name: Option<String>,
27    /// Maximum time in seconds to continue to use a stale version of the object if future requests to your backend server fail (also known as 'stale if error').
28    pub stale_ttl: Option<String>,
29    /// Maximum time to consider the object fresh in the cache (the cache 'time to live').
30    pub ttl: Option<String>
31}
32
33/// struct for passing parameters to the method [`delete_cache_settings`]
34#[derive(Clone, Debug, Default)]
35pub struct DeleteCacheSettingsParams {
36    /// Alphanumeric string identifying the service.
37    pub service_id: String,
38    /// Integer identifying a service version.
39    pub version_id: i32,
40    /// Name for the cache settings object.
41    pub cache_settings_name: String
42}
43
44/// struct for passing parameters to the method [`get_cache_settings`]
45#[derive(Clone, Debug, Default)]
46pub struct GetCacheSettingsParams {
47    /// Alphanumeric string identifying the service.
48    pub service_id: String,
49    /// Integer identifying a service version.
50    pub version_id: i32,
51    /// Name for the cache settings object.
52    pub cache_settings_name: String
53}
54
55/// struct for passing parameters to the method [`list_cache_settings`]
56#[derive(Clone, Debug, Default)]
57pub struct ListCacheSettingsParams {
58    /// Alphanumeric string identifying the service.
59    pub service_id: String,
60    /// Integer identifying a service version.
61    pub version_id: i32
62}
63
64/// struct for passing parameters to the method [`update_cache_settings`]
65#[derive(Clone, Debug, Default)]
66pub struct UpdateCacheSettingsParams {
67    /// Alphanumeric string identifying the service.
68    pub service_id: String,
69    /// Integer identifying a service version.
70    pub version_id: i32,
71    /// Name for the cache settings object.
72    pub cache_settings_name: String,
73    /// If set, will cause vcl_fetch to terminate after processing this rule with the return state specified. If not set, other configuration logic in vcl_fetch with a lower priority will run after this rule. 
74    pub action: Option<String>,
75    /// Name of the cache condition controlling when this configuration applies.
76    pub cache_condition: Option<String>,
77    /// Name for the cache settings object.
78    pub name: Option<String>,
79    /// Maximum time in seconds to continue to use a stale version of the object if future requests to your backend server fail (also known as 'stale if error').
80    pub stale_ttl: Option<String>,
81    /// Maximum time to consider the object fresh in the cache (the cache 'time to live').
82    pub ttl: Option<String>
83}
84
85
86/// struct for typed errors of method [`create_cache_settings`]
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[serde(untagged)]
89pub enum CreateCacheSettingsError {
90    UnknownValue(serde_json::Value),
91}
92
93/// struct for typed errors of method [`delete_cache_settings`]
94#[derive(Debug, Clone, Serialize, Deserialize)]
95#[serde(untagged)]
96pub enum DeleteCacheSettingsError {
97    UnknownValue(serde_json::Value),
98}
99
100/// struct for typed errors of method [`get_cache_settings`]
101#[derive(Debug, Clone, Serialize, Deserialize)]
102#[serde(untagged)]
103pub enum GetCacheSettingsError {
104    UnknownValue(serde_json::Value),
105}
106
107/// struct for typed errors of method [`list_cache_settings`]
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(untagged)]
110pub enum ListCacheSettingsError {
111    UnknownValue(serde_json::Value),
112}
113
114/// struct for typed errors of method [`update_cache_settings`]
115#[derive(Debug, Clone, Serialize, Deserialize)]
116#[serde(untagged)]
117pub enum UpdateCacheSettingsError {
118    UnknownValue(serde_json::Value),
119}
120
121
122/// Create a cache settings object.
123pub async fn create_cache_settings(configuration: &mut configuration::Configuration, params: CreateCacheSettingsParams) -> Result<crate::models::CacheSettingResponse, Error<CreateCacheSettingsError>> {
124    let local_var_configuration = configuration;
125
126    // unbox the parameters
127    let service_id = params.service_id;
128    let version_id = params.version_id;
129    let action = params.action;
130    let cache_condition = params.cache_condition;
131    let name = params.name;
132    let stale_ttl = params.stale_ttl;
133    let ttl = params.ttl;
134
135
136    let local_var_client = &local_var_configuration.client;
137
138    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/cache_settings", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
139    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
140
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("Fastly-Key", local_var_value);
151    };
152    let mut local_var_form_params = std::collections::HashMap::new();
153    if let Some(local_var_param_value) = action {
154        local_var_form_params.insert("action", local_var_param_value.to_string());
155    }
156    if let Some(local_var_param_value) = cache_condition {
157        local_var_form_params.insert("cache_condition", local_var_param_value.to_string());
158    }
159    if let Some(local_var_param_value) = name {
160        local_var_form_params.insert("name", local_var_param_value.to_string());
161    }
162    if let Some(local_var_param_value) = stale_ttl {
163        local_var_form_params.insert("stale_ttl", local_var_param_value.to_string());
164    }
165    if let Some(local_var_param_value) = ttl {
166        local_var_form_params.insert("ttl", local_var_param_value.to_string());
167    }
168    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
169
170    let local_var_req = local_var_req_builder.build()?;
171    let local_var_resp = local_var_client.execute(local_var_req).await?;
172
173    if "POST" != "GET" && "POST" != "HEAD" {
174      let headers = local_var_resp.headers();
175      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
176          Some(v) => v.to_str().unwrap().parse().unwrap(),
177          None => configuration::DEFAULT_RATELIMIT,
178      };
179      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
180          Some(v) => v.to_str().unwrap().parse().unwrap(),
181          None => 0,
182      };
183    }
184
185    let local_var_status = local_var_resp.status();
186    let local_var_content = local_var_resp.text().await?;
187
188    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
189        serde_json::from_str(&local_var_content).map_err(Error::from)
190    } else {
191        let local_var_entity: Option<CreateCacheSettingsError> = serde_json::from_str(&local_var_content).ok();
192        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
193        Err(Error::ResponseError(local_var_error))
194    }
195}
196
197/// Delete a specific cache settings object.
198pub async fn delete_cache_settings(configuration: &mut configuration::Configuration, params: DeleteCacheSettingsParams) -> Result<crate::models::InlineResponse200, Error<DeleteCacheSettingsError>> {
199    let local_var_configuration = configuration;
200
201    // unbox the parameters
202    let service_id = params.service_id;
203    let version_id = params.version_id;
204    let cache_settings_name = params.cache_settings_name;
205
206
207    let local_var_client = &local_var_configuration.client;
208
209    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/cache_settings/{cache_settings_name}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, cache_settings_name=crate::apis::urlencode(cache_settings_name));
210    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
211
212    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
213        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
214    }
215    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
216        let local_var_key = local_var_apikey.key.clone();
217        let local_var_value = match local_var_apikey.prefix {
218            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
219            None => local_var_key,
220        };
221        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
222    };
223
224    let local_var_req = local_var_req_builder.build()?;
225    let local_var_resp = local_var_client.execute(local_var_req).await?;
226
227    if "DELETE" != "GET" && "DELETE" != "HEAD" {
228      let headers = local_var_resp.headers();
229      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
230          Some(v) => v.to_str().unwrap().parse().unwrap(),
231          None => configuration::DEFAULT_RATELIMIT,
232      };
233      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
234          Some(v) => v.to_str().unwrap().parse().unwrap(),
235          None => 0,
236      };
237    }
238
239    let local_var_status = local_var_resp.status();
240    let local_var_content = local_var_resp.text().await?;
241
242    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
243        serde_json::from_str(&local_var_content).map_err(Error::from)
244    } else {
245        let local_var_entity: Option<DeleteCacheSettingsError> = serde_json::from_str(&local_var_content).ok();
246        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
247        Err(Error::ResponseError(local_var_error))
248    }
249}
250
251/// Get a specific cache settings object.
252pub async fn get_cache_settings(configuration: &mut configuration::Configuration, params: GetCacheSettingsParams) -> Result<crate::models::CacheSettingResponse, Error<GetCacheSettingsError>> {
253    let local_var_configuration = configuration;
254
255    // unbox the parameters
256    let service_id = params.service_id;
257    let version_id = params.version_id;
258    let cache_settings_name = params.cache_settings_name;
259
260
261    let local_var_client = &local_var_configuration.client;
262
263    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/cache_settings/{cache_settings_name}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, cache_settings_name=crate::apis::urlencode(cache_settings_name));
264    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
265
266    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
267        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
268    }
269    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
270        let local_var_key = local_var_apikey.key.clone();
271        let local_var_value = match local_var_apikey.prefix {
272            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
273            None => local_var_key,
274        };
275        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
276    };
277
278    let local_var_req = local_var_req_builder.build()?;
279    let local_var_resp = local_var_client.execute(local_var_req).await?;
280
281    if "GET" != "GET" && "GET" != "HEAD" {
282      let headers = local_var_resp.headers();
283      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
284          Some(v) => v.to_str().unwrap().parse().unwrap(),
285          None => configuration::DEFAULT_RATELIMIT,
286      };
287      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
288          Some(v) => v.to_str().unwrap().parse().unwrap(),
289          None => 0,
290      };
291    }
292
293    let local_var_status = local_var_resp.status();
294    let local_var_content = local_var_resp.text().await?;
295
296    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
297        serde_json::from_str(&local_var_content).map_err(Error::from)
298    } else {
299        let local_var_entity: Option<GetCacheSettingsError> = serde_json::from_str(&local_var_content).ok();
300        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
301        Err(Error::ResponseError(local_var_error))
302    }
303}
304
305/// Get a list of all cache settings for a particular service and version.
306pub async fn list_cache_settings(configuration: &mut configuration::Configuration, params: ListCacheSettingsParams) -> Result<Vec<crate::models::CacheSettingResponse>, Error<ListCacheSettingsError>> {
307    let local_var_configuration = configuration;
308
309    // unbox the parameters
310    let service_id = params.service_id;
311    let version_id = params.version_id;
312
313
314    let local_var_client = &local_var_configuration.client;
315
316    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/cache_settings", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
317    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
318
319    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
320        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
321    }
322    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
323        let local_var_key = local_var_apikey.key.clone();
324        let local_var_value = match local_var_apikey.prefix {
325            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
326            None => local_var_key,
327        };
328        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
329    };
330
331    let local_var_req = local_var_req_builder.build()?;
332    let local_var_resp = local_var_client.execute(local_var_req).await?;
333
334    if "GET" != "GET" && "GET" != "HEAD" {
335      let headers = local_var_resp.headers();
336      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
337          Some(v) => v.to_str().unwrap().parse().unwrap(),
338          None => configuration::DEFAULT_RATELIMIT,
339      };
340      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
341          Some(v) => v.to_str().unwrap().parse().unwrap(),
342          None => 0,
343      };
344    }
345
346    let local_var_status = local_var_resp.status();
347    let local_var_content = local_var_resp.text().await?;
348
349    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
350        serde_json::from_str(&local_var_content).map_err(Error::from)
351    } else {
352        let local_var_entity: Option<ListCacheSettingsError> = serde_json::from_str(&local_var_content).ok();
353        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
354        Err(Error::ResponseError(local_var_error))
355    }
356}
357
358/// Update a specific cache settings object.
359pub async fn update_cache_settings(configuration: &mut configuration::Configuration, params: UpdateCacheSettingsParams) -> Result<crate::models::CacheSettingResponse, Error<UpdateCacheSettingsError>> {
360    let local_var_configuration = configuration;
361
362    // unbox the parameters
363    let service_id = params.service_id;
364    let version_id = params.version_id;
365    let cache_settings_name = params.cache_settings_name;
366    let action = params.action;
367    let cache_condition = params.cache_condition;
368    let name = params.name;
369    let stale_ttl = params.stale_ttl;
370    let ttl = params.ttl;
371
372
373    let local_var_client = &local_var_configuration.client;
374
375    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/cache_settings/{cache_settings_name}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, cache_settings_name=crate::apis::urlencode(cache_settings_name));
376    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
377
378    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
379        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
380    }
381    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
382        let local_var_key = local_var_apikey.key.clone();
383        let local_var_value = match local_var_apikey.prefix {
384            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
385            None => local_var_key,
386        };
387        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
388    };
389    let mut local_var_form_params = std::collections::HashMap::new();
390    if let Some(local_var_param_value) = action {
391        local_var_form_params.insert("action", local_var_param_value.to_string());
392    }
393    if let Some(local_var_param_value) = cache_condition {
394        local_var_form_params.insert("cache_condition", local_var_param_value.to_string());
395    }
396    if let Some(local_var_param_value) = name {
397        local_var_form_params.insert("name", local_var_param_value.to_string());
398    }
399    if let Some(local_var_param_value) = stale_ttl {
400        local_var_form_params.insert("stale_ttl", local_var_param_value.to_string());
401    }
402    if let Some(local_var_param_value) = ttl {
403        local_var_form_params.insert("ttl", local_var_param_value.to_string());
404    }
405    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
406
407    let local_var_req = local_var_req_builder.build()?;
408    let local_var_resp = local_var_client.execute(local_var_req).await?;
409
410    if "PUT" != "GET" && "PUT" != "HEAD" {
411      let headers = local_var_resp.headers();
412      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
413          Some(v) => v.to_str().unwrap().parse().unwrap(),
414          None => configuration::DEFAULT_RATELIMIT,
415      };
416      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
417          Some(v) => v.to_str().unwrap().parse().unwrap(),
418          None => 0,
419      };
420    }
421
422    let local_var_status = local_var_resp.status();
423    let local_var_content = local_var_resp.text().await?;
424
425    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
426        serde_json::from_str(&local_var_content).map_err(Error::from)
427    } else {
428        let local_var_entity: Option<UpdateCacheSettingsError> = serde_json::from_str(&local_var_content).ok();
429        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
430        Err(Error::ResponseError(local_var_error))
431    }
432}
433