fastly_api/apis/
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 [`get_service_settings`]
15#[derive(Clone, Debug, Default)]
16pub struct GetServiceSettingsParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    /// Integer identifying a service version.
20    pub version_id: i32
21}
22
23/// struct for passing parameters to the method [`update_service_settings`]
24#[derive(Clone, Debug, Default)]
25pub struct UpdateServiceSettingsParams {
26    /// Alphanumeric string identifying the service.
27    pub service_id: String,
28    /// Integer identifying a service version.
29    pub version_id: i32,
30    /// The default host name for the version.
31    pub general_default_host: Option<String>,
32    /// The default time-to-live (TTL) for the version.
33    pub general_default_ttl: Option<i32>,
34    /// Enables serving a stale object if there is an error.
35    pub general_stale_if_error: Option<bool>,
36    /// The default time-to-live (TTL) for serving the stale object for the version.
37    pub general_stale_if_error_ttl: Option<i32>
38}
39
40
41/// struct for typed errors of method [`get_service_settings`]
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(untagged)]
44pub enum GetServiceSettingsError {
45    UnknownValue(serde_json::Value),
46}
47
48/// struct for typed errors of method [`update_service_settings`]
49#[derive(Debug, Clone, Serialize, Deserialize)]
50#[serde(untagged)]
51pub enum UpdateServiceSettingsError {
52    UnknownValue(serde_json::Value),
53}
54
55
56/// Get the settings for a particular service and version.
57pub async fn get_service_settings(configuration: &mut configuration::Configuration, params: GetServiceSettingsParams) -> Result<crate::models::SettingsResponse, Error<GetServiceSettingsError>> {
58    let local_var_configuration = configuration;
59
60    // unbox the parameters
61    let service_id = params.service_id;
62    let version_id = params.version_id;
63
64
65    let local_var_client = &local_var_configuration.client;
66
67    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/settings", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
68    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
69
70    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
71        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
72    }
73    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
74        let local_var_key = local_var_apikey.key.clone();
75        let local_var_value = match local_var_apikey.prefix {
76            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
77            None => local_var_key,
78        };
79        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
80    };
81
82    let local_var_req = local_var_req_builder.build()?;
83    let local_var_resp = local_var_client.execute(local_var_req).await?;
84
85    if "GET" != "GET" && "GET" != "HEAD" {
86      let headers = local_var_resp.headers();
87      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
88          Some(v) => v.to_str().unwrap().parse().unwrap(),
89          None => configuration::DEFAULT_RATELIMIT,
90      };
91      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
92          Some(v) => v.to_str().unwrap().parse().unwrap(),
93          None => 0,
94      };
95    }
96
97    let local_var_status = local_var_resp.status();
98    let local_var_content = local_var_resp.text().await?;
99
100    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
101        serde_json::from_str(&local_var_content).map_err(Error::from)
102    } else {
103        let local_var_entity: Option<GetServiceSettingsError> = serde_json::from_str(&local_var_content).ok();
104        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
105        Err(Error::ResponseError(local_var_error))
106    }
107}
108
109/// Update the settings for a particular service and version. NOTE: If you override TTLs with custom VCL, any general.default_ttl value will not be honored and the expected behavior may change. 
110pub async fn update_service_settings(configuration: &mut configuration::Configuration, params: UpdateServiceSettingsParams) -> Result<crate::models::SettingsResponse, Error<UpdateServiceSettingsError>> {
111    let local_var_configuration = configuration;
112
113    // unbox the parameters
114    let service_id = params.service_id;
115    let version_id = params.version_id;
116    let general_default_host = params.general_default_host;
117    let general_default_ttl = params.general_default_ttl;
118    let general_stale_if_error = params.general_stale_if_error;
119    let general_stale_if_error_ttl = params.general_stale_if_error_ttl;
120
121
122    let local_var_client = &local_var_configuration.client;
123
124    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/settings", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
125    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
126
127    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
128        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
129    }
130    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
131        let local_var_key = local_var_apikey.key.clone();
132        let local_var_value = match local_var_apikey.prefix {
133            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
134            None => local_var_key,
135        };
136        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
137    };
138    let mut local_var_form_params = std::collections::HashMap::new();
139    if let Some(local_var_param_value) = general_default_host {
140        local_var_form_params.insert("general.default_host", local_var_param_value.to_string());
141    }
142    if let Some(local_var_param_value) = general_default_ttl {
143        local_var_form_params.insert("general.default_ttl", local_var_param_value.to_string());
144    }
145    if let Some(local_var_param_value) = general_stale_if_error {
146        local_var_form_params.insert("general.stale_if_error", local_var_param_value.to_string());
147    }
148    if let Some(local_var_param_value) = general_stale_if_error_ttl {
149        local_var_form_params.insert("general.stale_if_error_ttl", local_var_param_value.to_string());
150    }
151    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
152
153    let local_var_req = local_var_req_builder.build()?;
154    let local_var_resp = local_var_client.execute(local_var_req).await?;
155
156    if "PUT" != "GET" && "PUT" != "HEAD" {
157      let headers = local_var_resp.headers();
158      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
159          Some(v) => v.to_str().unwrap().parse().unwrap(),
160          None => configuration::DEFAULT_RATELIMIT,
161      };
162      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
163          Some(v) => v.to_str().unwrap().parse().unwrap(),
164          None => 0,
165      };
166    }
167
168    let local_var_status = local_var_resp.status();
169    let local_var_content = local_var_resp.text().await?;
170
171    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
172        serde_json::from_str(&local_var_content).map_err(Error::from)
173    } else {
174        let local_var_entity: Option<UpdateServiceSettingsError> = serde_json::from_str(&local_var_content).ok();
175        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
176        Err(Error::ResponseError(local_var_error))
177    }
178}
179