Skip to main content

fastly_api/apis/
purge_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 [`bulk_purge_tag`]
15#[derive(Clone, Debug, Default)]
16pub struct BulkPurgeTagParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    /// If present, this header triggers the purge to be 'soft', which marks the affected object as stale rather than making it inaccessible.  Typically set to \"1\" when used, but the value is not important.
20    pub fastly_soft_purge: Option<i32>,
21    /// Set to `staging` to purge the cache of the service's [staging environment](https://www.fastly.com/documentation/guides/getting-started/services/working-with-staging/) instead of its production environment. If this header is not present, the purge applies to the production environment.
22    pub fastly_purge_environment: Option<String>,
23    /// Purge multiple surrogate key tags using a request header. Not required if a JSON POST body is specified.
24    pub surrogate_key: Option<String>,
25    pub purge_response: Option<crate::models::PurgeResponse>
26}
27
28/// struct for passing parameters to the method [`purge_all`]
29#[derive(Clone, Debug, Default)]
30pub struct PurgeAllParams {
31    /// Alphanumeric string identifying the service.
32    pub service_id: String,
33    /// Set to `staging` to purge the cache of the service's [staging environment](https://www.fastly.com/documentation/guides/getting-started/services/working-with-staging/) instead of its production environment. If this header is not present, the purge applies to the production environment.
34    pub fastly_purge_environment: Option<String>
35}
36
37/// struct for passing parameters to the method [`purge_single_url`]
38#[derive(Clone, Debug, Default)]
39pub struct PurgeSingleUrlParams {
40    /// URL of object in cache to be purged.
41    pub cached_url: String,
42    /// If present, this header triggers the purge to be 'soft', which marks the affected object as stale rather than making it inaccessible.  Typically set to \"1\" when used, but the value is not important.
43    pub fastly_soft_purge: Option<i32>
44}
45
46/// struct for passing parameters to the method [`purge_tag`]
47#[derive(Clone, Debug, Default)]
48pub struct PurgeTagParams {
49    /// Alphanumeric string identifying the service.
50    pub service_id: String,
51    /// Surrogate keys are used to efficiently purge content from cache. Instead of purging your entire site or individual URLs, you can tag related assets (like all images and descriptions associated with a single product) with surrogate keys, and these grouped URLs can be purged in a single request.
52    pub surrogate_key: String,
53    /// If present, this header triggers the purge to be 'soft', which marks the affected object as stale rather than making it inaccessible.  Typically set to \"1\" when used, but the value is not important.
54    pub fastly_soft_purge: Option<i32>,
55    /// Set to `staging` to purge the cache of the service's [staging environment](https://www.fastly.com/documentation/guides/getting-started/services/working-with-staging/) instead of its production environment. If this header is not present, the purge applies to the production environment.
56    pub fastly_purge_environment: Option<String>
57}
58
59
60/// struct for typed errors of method [`bulk_purge_tag`]
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(untagged)]
63pub enum BulkPurgeTagError {
64    UnknownValue(serde_json::Value),
65}
66
67/// struct for typed errors of method [`purge_all`]
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(untagged)]
70pub enum PurgeAllError {
71    UnknownValue(serde_json::Value),
72}
73
74/// struct for typed errors of method [`purge_single_url`]
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(untagged)]
77pub enum PurgeSingleUrlError {
78    UnknownValue(serde_json::Value),
79}
80
81/// struct for typed errors of method [`purge_tag`]
82#[derive(Debug, Clone, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum PurgeTagError {
85    UnknownValue(serde_json::Value),
86}
87
88
89/// Instant Purge a particular service of items tagged with surrogate keys. Up to 256 surrogate keys can be purged in one batch request. As an alternative to sending the keys in a JSON object in the body of the request, this endpoint also supports listing keys in a <code>Surrogate-Key</code> request header, e.g. <code>Surrogate-Key: key_1 key_2 key_3</code>. 
90pub async fn bulk_purge_tag(configuration: &mut configuration::Configuration, params: BulkPurgeTagParams) -> Result<::std::collections::HashMap<String, String>, Error<BulkPurgeTagError>> {
91    let local_var_configuration = configuration;
92
93    // unbox the parameters
94    let service_id = params.service_id;
95    let fastly_soft_purge = params.fastly_soft_purge;
96    let fastly_purge_environment = params.fastly_purge_environment;
97    let surrogate_key = params.surrogate_key;
98    let purge_response = params.purge_response;
99
100
101    let local_var_client = &local_var_configuration.client;
102
103    let local_var_uri_str = format!("{}/service/{service_id}/purge", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id));
104    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
105
106    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
107        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
108    }
109    if let Some(local_var_param_value) = fastly_soft_purge {
110        local_var_req_builder = local_var_req_builder.header("fastly-soft-purge", local_var_param_value.to_string());
111    }
112    if let Some(local_var_param_value) = fastly_purge_environment {
113        local_var_req_builder = local_var_req_builder.header("fastly-purge-environment", local_var_param_value.to_string());
114    }
115    if let Some(local_var_param_value) = surrogate_key {
116        local_var_req_builder = local_var_req_builder.header("surrogate-key", local_var_param_value.to_string());
117    }
118    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
119        let local_var_key = local_var_apikey.key.clone();
120        let local_var_value = match local_var_apikey.prefix {
121            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
122            None => local_var_key,
123        };
124        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
125    };
126    local_var_req_builder = local_var_req_builder.json(&purge_response);
127
128    let local_var_req = local_var_req_builder.build()?;
129    let local_var_resp = local_var_client.execute(local_var_req).await?;
130
131    if "POST" != "GET" && "POST" != "HEAD" {
132      let headers = local_var_resp.headers();
133      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
134          Some(v) => v.to_str().unwrap().parse().unwrap(),
135          None => configuration::DEFAULT_RATELIMIT,
136      };
137      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
138          Some(v) => v.to_str().unwrap().parse().unwrap(),
139          None => 0,
140      };
141    }
142
143    let local_var_status = local_var_resp.status();
144    let local_var_content = local_var_resp.text().await?;
145
146    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
147        serde_json::from_str(&local_var_content).map_err(Error::from)
148    } else {
149        let local_var_entity: Option<BulkPurgeTagError> = serde_json::from_str(&local_var_content).ok();
150        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
151        Err(Error::ResponseError(local_var_error))
152    }
153}
154
155/// Instant Purge everything from a service.  Purge-all requests cannot be done in soft mode and will always immediately invalidate all cached content associated with the service. To do a soft-purge-all, consider applying a constant [surrogate key](https://www.fastly.com/documentation/guides/full-site-delivery/purging/working-with-surrogate-keys/) tag (e.g., `\"all\"`) to all objects. 
156pub async fn purge_all(configuration: &mut configuration::Configuration, params: PurgeAllParams) -> Result<crate::models::InlineResponse200, Error<PurgeAllError>> {
157    let local_var_configuration = configuration;
158
159    // unbox the parameters
160    let service_id = params.service_id;
161    let fastly_purge_environment = params.fastly_purge_environment;
162
163
164    let local_var_client = &local_var_configuration.client;
165
166    let local_var_uri_str = format!("{}/service/{service_id}/purge_all", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id));
167    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
168
169    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
170        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
171    }
172    if let Some(local_var_param_value) = fastly_purge_environment {
173        local_var_req_builder = local_var_req_builder.header("fastly-purge-environment", local_var_param_value.to_string());
174    }
175    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
176        let local_var_key = local_var_apikey.key.clone();
177        let local_var_value = match local_var_apikey.prefix {
178            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
179            None => local_var_key,
180        };
181        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
182    };
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<PurgeAllError> = 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/// Instant Purge an individual URL.
212pub async fn purge_single_url(configuration: &mut configuration::Configuration, params: PurgeSingleUrlParams) -> Result<crate::models::PurgeResponse, Error<PurgeSingleUrlError>> {
213    let local_var_configuration = configuration;
214
215    // unbox the parameters
216    let cached_url = params.cached_url;
217    let fastly_soft_purge = params.fastly_soft_purge;
218
219
220    let local_var_client = &local_var_configuration.client;
221
222    let local_var_uri_str = format!("{}/purge/{cached_url}", local_var_configuration.base_path, cached_url=cached_url);
223    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
224
225    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
226        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
227    }
228    if let Some(local_var_param_value) = fastly_soft_purge {
229        local_var_req_builder = local_var_req_builder.header("fastly-soft-purge", local_var_param_value.to_string());
230    }
231    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
232        let local_var_key = local_var_apikey.key.clone();
233        let local_var_value = match local_var_apikey.prefix {
234            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
235            None => local_var_key,
236        };
237        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
238    };
239
240    let local_var_req = local_var_req_builder.build()?;
241    let local_var_resp = local_var_client.execute(local_var_req).await?;
242
243    if "POST" != "GET" && "POST" != "HEAD" {
244      let headers = local_var_resp.headers();
245      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
246          Some(v) => v.to_str().unwrap().parse().unwrap(),
247          None => configuration::DEFAULT_RATELIMIT,
248      };
249      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
250          Some(v) => v.to_str().unwrap().parse().unwrap(),
251          None => 0,
252      };
253    }
254
255    let local_var_status = local_var_resp.status();
256    let local_var_content = local_var_resp.text().await?;
257
258    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
259        serde_json::from_str(&local_var_content).map_err(Error::from)
260    } else {
261        let local_var_entity: Option<PurgeSingleUrlError> = serde_json::from_str(&local_var_content).ok();
262        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
263        Err(Error::ResponseError(local_var_error))
264    }
265}
266
267/// Instant Purge a particular service of items tagged with a Surrogate Key. Only one surrogate key can be purged at a time. Multiple keys can be purged using a batch surrogate key purge request.
268pub async fn purge_tag(configuration: &mut configuration::Configuration, params: PurgeTagParams) -> Result<crate::models::PurgeResponse, Error<PurgeTagError>> {
269    let local_var_configuration = configuration;
270
271    // unbox the parameters
272    let service_id = params.service_id;
273    let surrogate_key = params.surrogate_key;
274    let fastly_soft_purge = params.fastly_soft_purge;
275    let fastly_purge_environment = params.fastly_purge_environment;
276
277
278    let local_var_client = &local_var_configuration.client;
279
280    let local_var_uri_str = format!("{}/service/{service_id}/purge/{surrogate_key}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), surrogate_key=surrogate_key);
281    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
282
283    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
284        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
285    }
286    if let Some(local_var_param_value) = fastly_soft_purge {
287        local_var_req_builder = local_var_req_builder.header("fastly-soft-purge", local_var_param_value.to_string());
288    }
289    if let Some(local_var_param_value) = fastly_purge_environment {
290        local_var_req_builder = local_var_req_builder.header("fastly-purge-environment", local_var_param_value.to_string());
291    }
292    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
293        let local_var_key = local_var_apikey.key.clone();
294        let local_var_value = match local_var_apikey.prefix {
295            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
296            None => local_var_key,
297        };
298        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
299    };
300
301    let local_var_req = local_var_req_builder.build()?;
302    let local_var_resp = local_var_client.execute(local_var_req).await?;
303
304    if "POST" != "GET" && "POST" != "HEAD" {
305      let headers = local_var_resp.headers();
306      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
307          Some(v) => v.to_str().unwrap().parse().unwrap(),
308          None => configuration::DEFAULT_RATELIMIT,
309      };
310      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
311          Some(v) => v.to_str().unwrap().parse().unwrap(),
312          None => 0,
313      };
314    }
315
316    let local_var_status = local_var_resp.status();
317    let local_var_content = local_var_resp.text().await?;
318
319    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
320        serde_json::from_str(&local_var_content).map_err(Error::from)
321    } else {
322        let local_var_entity: Option<PurgeTagError> = serde_json::from_str(&local_var_content).ok();
323        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
324        Err(Error::ResponseError(local_var_error))
325    }
326}
327