fastly_api/apis/
condition_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_condition`]
15#[derive(Clone, Debug, Default)]
16pub struct CreateConditionParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    /// Integer identifying a service version.
20    pub version_id: i32,
21    /// A freeform descriptive note.
22    pub comment: Option<String>,
23    /// Name of the condition. Required.
24    pub name: Option<String>,
25    /// A numeric string. Priority determines execution order. Lower numbers execute first.
26    pub priority: Option<String>,
27    /// A conditional expression in VCL used to determine if the condition is met.
28    pub statement: Option<String>,
29    pub service_id2: Option<String>,
30    /// A numeric string that represents the service version.
31    pub version: Option<String>,
32    /// Type of the condition. Required.
33    pub _type: Option<String>
34}
35
36/// struct for passing parameters to the method [`delete_condition`]
37#[derive(Clone, Debug, Default)]
38pub struct DeleteConditionParams {
39    /// Alphanumeric string identifying the service.
40    pub service_id: String,
41    /// Integer identifying a service version.
42    pub version_id: i32,
43    /// Name of the condition. Required.
44    pub condition_name: String
45}
46
47/// struct for passing parameters to the method [`get_condition`]
48#[derive(Clone, Debug, Default)]
49pub struct GetConditionParams {
50    /// Alphanumeric string identifying the service.
51    pub service_id: String,
52    /// Integer identifying a service version.
53    pub version_id: i32,
54    /// Name of the condition. Required.
55    pub condition_name: String
56}
57
58/// struct for passing parameters to the method [`list_conditions`]
59#[derive(Clone, Debug, Default)]
60pub struct ListConditionsParams {
61    /// Alphanumeric string identifying the service.
62    pub service_id: String,
63    /// Integer identifying a service version.
64    pub version_id: i32
65}
66
67/// struct for passing parameters to the method [`update_condition`]
68#[derive(Clone, Debug, Default)]
69pub struct UpdateConditionParams {
70    /// Alphanumeric string identifying the service.
71    pub service_id: String,
72    /// Integer identifying a service version.
73    pub version_id: i32,
74    /// Name of the condition. Required.
75    pub condition_name: String,
76    /// A freeform descriptive note.
77    pub comment: Option<String>,
78    /// Name of the condition. Required.
79    pub name: Option<String>,
80    /// A numeric string. Priority determines execution order. Lower numbers execute first.
81    pub priority: Option<String>,
82    /// A conditional expression in VCL used to determine if the condition is met.
83    pub statement: Option<String>,
84    pub service_id2: Option<String>,
85    /// A numeric string that represents the service version.
86    pub version: Option<String>,
87    /// Type of the condition. Required.
88    pub _type: Option<String>
89}
90
91
92/// struct for typed errors of method [`create_condition`]
93#[derive(Debug, Clone, Serialize, Deserialize)]
94#[serde(untagged)]
95pub enum CreateConditionError {
96    UnknownValue(serde_json::Value),
97}
98
99/// struct for typed errors of method [`delete_condition`]
100#[derive(Debug, Clone, Serialize, Deserialize)]
101#[serde(untagged)]
102pub enum DeleteConditionError {
103    UnknownValue(serde_json::Value),
104}
105
106/// struct for typed errors of method [`get_condition`]
107#[derive(Debug, Clone, Serialize, Deserialize)]
108#[serde(untagged)]
109pub enum GetConditionError {
110    UnknownValue(serde_json::Value),
111}
112
113/// struct for typed errors of method [`list_conditions`]
114#[derive(Debug, Clone, Serialize, Deserialize)]
115#[serde(untagged)]
116pub enum ListConditionsError {
117    UnknownValue(serde_json::Value),
118}
119
120/// struct for typed errors of method [`update_condition`]
121#[derive(Debug, Clone, Serialize, Deserialize)]
122#[serde(untagged)]
123pub enum UpdateConditionError {
124    UnknownValue(serde_json::Value),
125}
126
127
128/// Creates a new condition.
129pub async fn create_condition(configuration: &mut configuration::Configuration, params: CreateConditionParams) -> Result<crate::models::ConditionResponse, Error<CreateConditionError>> {
130    let local_var_configuration = configuration;
131
132    // unbox the parameters
133    let service_id = params.service_id;
134    let version_id = params.version_id;
135    let comment = params.comment;
136    let name = params.name;
137    let priority = params.priority;
138    let statement = params.statement;
139    let service_id2 = params.service_id2;
140    let version = params.version;
141    let _type = params._type;
142
143
144    let local_var_client = &local_var_configuration.client;
145
146    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/condition", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
147    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
148
149    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
150        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
151    }
152    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
153        let local_var_key = local_var_apikey.key.clone();
154        let local_var_value = match local_var_apikey.prefix {
155            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
156            None => local_var_key,
157        };
158        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
159    };
160    let mut local_var_form_params = std::collections::HashMap::new();
161    if let Some(local_var_param_value) = comment {
162        local_var_form_params.insert("comment", local_var_param_value.to_string());
163    }
164    if let Some(local_var_param_value) = name {
165        local_var_form_params.insert("name", local_var_param_value.to_string());
166    }
167    if let Some(local_var_param_value) = priority {
168        local_var_form_params.insert("priority", local_var_param_value.to_string());
169    }
170    if let Some(local_var_param_value) = statement {
171        local_var_form_params.insert("statement", local_var_param_value.to_string());
172    }
173    if let Some(local_var_param_value) = service_id2 {
174        local_var_form_params.insert("service_id", local_var_param_value.to_string());
175    }
176    if let Some(local_var_param_value) = version {
177        local_var_form_params.insert("version", local_var_param_value.to_string());
178    }
179    if let Some(local_var_param_value) = _type {
180        local_var_form_params.insert("type", local_var_param_value.to_string());
181    }
182    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
183
184    let local_var_req = local_var_req_builder.build()?;
185    let local_var_resp = local_var_client.execute(local_var_req).await?;
186
187    if "POST" != "GET" && "POST" != "HEAD" {
188      let headers = local_var_resp.headers();
189      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
190          Some(v) => v.to_str().unwrap().parse().unwrap(),
191          None => configuration::DEFAULT_RATELIMIT,
192      };
193      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
194          Some(v) => v.to_str().unwrap().parse().unwrap(),
195          None => 0,
196      };
197    }
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<CreateConditionError> = serde_json::from_str(&local_var_content).ok();
206        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
207        Err(Error::ResponseError(local_var_error))
208    }
209}
210
211/// Deletes the specified condition.
212pub async fn delete_condition(configuration: &mut configuration::Configuration, params: DeleteConditionParams) -> Result<crate::models::InlineResponse200, Error<DeleteConditionError>> {
213    let local_var_configuration = configuration;
214
215    // unbox the parameters
216    let service_id = params.service_id;
217    let version_id = params.version_id;
218    let condition_name = params.condition_name;
219
220
221    let local_var_client = &local_var_configuration.client;
222
223    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/condition/{condition_name}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, condition_name=crate::apis::urlencode(condition_name));
224    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
225
226    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
227        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
228    }
229    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
230        let local_var_key = local_var_apikey.key.clone();
231        let local_var_value = match local_var_apikey.prefix {
232            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
233            None => local_var_key,
234        };
235        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
236    };
237
238    let local_var_req = local_var_req_builder.build()?;
239    let local_var_resp = local_var_client.execute(local_var_req).await?;
240
241    if "DELETE" != "GET" && "DELETE" != "HEAD" {
242      let headers = local_var_resp.headers();
243      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
244          Some(v) => v.to_str().unwrap().parse().unwrap(),
245          None => configuration::DEFAULT_RATELIMIT,
246      };
247      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
248          Some(v) => v.to_str().unwrap().parse().unwrap(),
249          None => 0,
250      };
251    }
252
253    let local_var_status = local_var_resp.status();
254    let local_var_content = local_var_resp.text().await?;
255
256    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
257        serde_json::from_str(&local_var_content).map_err(Error::from)
258    } else {
259        let local_var_entity: Option<DeleteConditionError> = serde_json::from_str(&local_var_content).ok();
260        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
261        Err(Error::ResponseError(local_var_error))
262    }
263}
264
265/// Gets the specified condition.
266pub async fn get_condition(configuration: &mut configuration::Configuration, params: GetConditionParams) -> Result<crate::models::ConditionResponse, Error<GetConditionError>> {
267    let local_var_configuration = configuration;
268
269    // unbox the parameters
270    let service_id = params.service_id;
271    let version_id = params.version_id;
272    let condition_name = params.condition_name;
273
274
275    let local_var_client = &local_var_configuration.client;
276
277    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/condition/{condition_name}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, condition_name=crate::apis::urlencode(condition_name));
278    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
279
280    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
281        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
282    }
283    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
284        let local_var_key = local_var_apikey.key.clone();
285        let local_var_value = match local_var_apikey.prefix {
286            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
287            None => local_var_key,
288        };
289        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
290    };
291
292    let local_var_req = local_var_req_builder.build()?;
293    let local_var_resp = local_var_client.execute(local_var_req).await?;
294
295    if "GET" != "GET" && "GET" != "HEAD" {
296      let headers = local_var_resp.headers();
297      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
298          Some(v) => v.to_str().unwrap().parse().unwrap(),
299          None => configuration::DEFAULT_RATELIMIT,
300      };
301      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
302          Some(v) => v.to_str().unwrap().parse().unwrap(),
303          None => 0,
304      };
305    }
306
307    let local_var_status = local_var_resp.status();
308    let local_var_content = local_var_resp.text().await?;
309
310    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
311        serde_json::from_str(&local_var_content).map_err(Error::from)
312    } else {
313        let local_var_entity: Option<GetConditionError> = serde_json::from_str(&local_var_content).ok();
314        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
315        Err(Error::ResponseError(local_var_error))
316    }
317}
318
319/// Gets all conditions for a particular service and version.
320pub async fn list_conditions(configuration: &mut configuration::Configuration, params: ListConditionsParams) -> Result<Vec<crate::models::ConditionResponse>, Error<ListConditionsError>> {
321    let local_var_configuration = configuration;
322
323    // unbox the parameters
324    let service_id = params.service_id;
325    let version_id = params.version_id;
326
327
328    let local_var_client = &local_var_configuration.client;
329
330    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/condition", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
331    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
332
333    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
334        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
335    }
336    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
337        let local_var_key = local_var_apikey.key.clone();
338        let local_var_value = match local_var_apikey.prefix {
339            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
340            None => local_var_key,
341        };
342        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
343    };
344
345    let local_var_req = local_var_req_builder.build()?;
346    let local_var_resp = local_var_client.execute(local_var_req).await?;
347
348    if "GET" != "GET" && "GET" != "HEAD" {
349      let headers = local_var_resp.headers();
350      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
351          Some(v) => v.to_str().unwrap().parse().unwrap(),
352          None => configuration::DEFAULT_RATELIMIT,
353      };
354      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
355          Some(v) => v.to_str().unwrap().parse().unwrap(),
356          None => 0,
357      };
358    }
359
360    let local_var_status = local_var_resp.status();
361    let local_var_content = local_var_resp.text().await?;
362
363    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
364        serde_json::from_str(&local_var_content).map_err(Error::from)
365    } else {
366        let local_var_entity: Option<ListConditionsError> = serde_json::from_str(&local_var_content).ok();
367        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
368        Err(Error::ResponseError(local_var_error))
369    }
370}
371
372/// Updates the specified condition.
373pub async fn update_condition(configuration: &mut configuration::Configuration, params: UpdateConditionParams) -> Result<crate::models::ConditionResponse, Error<UpdateConditionError>> {
374    let local_var_configuration = configuration;
375
376    // unbox the parameters
377    let service_id = params.service_id;
378    let version_id = params.version_id;
379    let condition_name = params.condition_name;
380    let comment = params.comment;
381    let name = params.name;
382    let priority = params.priority;
383    let statement = params.statement;
384    let service_id2 = params.service_id2;
385    let version = params.version;
386    let _type = params._type;
387
388
389    let local_var_client = &local_var_configuration.client;
390
391    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/condition/{condition_name}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, condition_name=crate::apis::urlencode(condition_name));
392    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
393
394    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
395        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
396    }
397    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
398        let local_var_key = local_var_apikey.key.clone();
399        let local_var_value = match local_var_apikey.prefix {
400            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
401            None => local_var_key,
402        };
403        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
404    };
405    let mut local_var_form_params = std::collections::HashMap::new();
406    if let Some(local_var_param_value) = comment {
407        local_var_form_params.insert("comment", local_var_param_value.to_string());
408    }
409    if let Some(local_var_param_value) = name {
410        local_var_form_params.insert("name", local_var_param_value.to_string());
411    }
412    if let Some(local_var_param_value) = priority {
413        local_var_form_params.insert("priority", local_var_param_value.to_string());
414    }
415    if let Some(local_var_param_value) = statement {
416        local_var_form_params.insert("statement", local_var_param_value.to_string());
417    }
418    if let Some(local_var_param_value) = service_id2 {
419        local_var_form_params.insert("service_id", local_var_param_value.to_string());
420    }
421    if let Some(local_var_param_value) = version {
422        local_var_form_params.insert("version", local_var_param_value.to_string());
423    }
424    if let Some(local_var_param_value) = _type {
425        local_var_form_params.insert("type", local_var_param_value.to_string());
426    }
427    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
428
429    let local_var_req = local_var_req_builder.build()?;
430    let local_var_resp = local_var_client.execute(local_var_req).await?;
431
432    if "PUT" != "GET" && "PUT" != "HEAD" {
433      let headers = local_var_resp.headers();
434      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
435          Some(v) => v.to_str().unwrap().parse().unwrap(),
436          None => configuration::DEFAULT_RATELIMIT,
437      };
438      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
439          Some(v) => v.to_str().unwrap().parse().unwrap(),
440          None => 0,
441      };
442    }
443
444    let local_var_status = local_var_resp.status();
445    let local_var_content = local_var_resp.text().await?;
446
447    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
448        serde_json::from_str(&local_var_content).map_err(Error::from)
449    } else {
450        let local_var_entity: Option<UpdateConditionError> = serde_json::from_str(&local_var_content).ok();
451        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
452        Err(Error::ResponseError(local_var_error))
453    }
454}
455