fastly_api/apis/
tokens_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_revoke_tokens`]
15#[derive(Clone, Debug, Default)]
16pub struct BulkRevokeTokensParams {
17    pub request_body: Option<::std::collections::HashMap<String, serde_json::Value>>
18}
19
20/// struct for passing parameters to the method [`get_token`]
21#[derive(Clone, Debug, Default)]
22pub struct GetTokenParams {
23    /// Alphanumeric string identifying a token.
24    pub token_id: String
25}
26
27/// struct for passing parameters to the method [`list_tokens_customer`]
28#[derive(Clone, Debug, Default)]
29pub struct ListTokensCustomerParams {
30    /// Alphanumeric string identifying the customer.
31    pub customer_id: String
32}
33
34/// struct for passing parameters to the method [`revoke_token`]
35#[derive(Clone, Debug, Default)]
36pub struct RevokeTokenParams {
37    /// Alphanumeric string identifying a token.
38    pub token_id: String
39}
40
41
42/// struct for typed errors of method [`bulk_revoke_tokens`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum BulkRevokeTokensError {
46    UnknownValue(serde_json::Value),
47}
48
49/// struct for typed errors of method [`create_token`]
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(untagged)]
52pub enum CreateTokenError {
53    Status400(crate::models::InlineResponse400),
54    Status422(),
55    UnknownValue(serde_json::Value),
56}
57
58/// struct for typed errors of method [`get_token`]
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(untagged)]
61pub enum GetTokenError {
62    Status401(crate::models::GenericTokenError),
63    Status403(crate::models::GenericTokenError),
64    Status404(crate::models::GenericTokenError),
65    UnknownValue(serde_json::Value),
66}
67
68/// struct for typed errors of method [`get_token_current`]
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[serde(untagged)]
71pub enum GetTokenCurrentError {
72    Status401(crate::models::GenericTokenError),
73    Status403(crate::models::GenericTokenError),
74    UnknownValue(serde_json::Value),
75}
76
77/// struct for typed errors of method [`list_tokens_customer`]
78#[derive(Debug, Clone, Serialize, Deserialize)]
79#[serde(untagged)]
80pub enum ListTokensCustomerError {
81    UnknownValue(serde_json::Value),
82}
83
84/// struct for typed errors of method [`list_tokens_user`]
85#[derive(Debug, Clone, Serialize, Deserialize)]
86#[serde(untagged)]
87pub enum ListTokensUserError {
88    Status401(crate::models::GenericTokenError),
89    Status403(crate::models::GenericTokenError),
90    UnknownValue(serde_json::Value),
91}
92
93/// struct for typed errors of method [`revoke_token`]
94#[derive(Debug, Clone, Serialize, Deserialize)]
95#[serde(untagged)]
96pub enum RevokeTokenError {
97    Status400(crate::models::GenericTokenError),
98    Status401(crate::models::GenericTokenError),
99    Status403(crate::models::GenericTokenError),
100    Status404(crate::models::GenericTokenError),
101    UnknownValue(serde_json::Value),
102}
103
104/// struct for typed errors of method [`revoke_token_current`]
105#[derive(Debug, Clone, Serialize, Deserialize)]
106#[serde(untagged)]
107pub enum RevokeTokenCurrentError {
108    Status400(crate::models::GenericTokenError),
109    Status401(crate::models::GenericTokenError),
110    Status403(crate::models::GenericTokenError),
111    UnknownValue(serde_json::Value),
112}
113
114
115/// Revoke Tokens in bulk format. Users may only revoke their own tokens. Superusers may revoke tokens of others.
116pub async fn bulk_revoke_tokens(configuration: &mut configuration::Configuration, params: BulkRevokeTokensParams) -> Result<(), Error<BulkRevokeTokensError>> {
117    let local_var_configuration = configuration;
118
119    // unbox the parameters
120    let request_body = params.request_body;
121
122
123    let local_var_client = &local_var_configuration.client;
124
125    let local_var_uri_str = format!("{}/tokens", local_var_configuration.base_path);
126    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
127
128    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
129        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
130    }
131    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
132        let local_var_key = local_var_apikey.key.clone();
133        let local_var_value = match local_var_apikey.prefix {
134            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
135            None => local_var_key,
136        };
137        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
138    };
139    local_var_req_builder = local_var_req_builder.json(&request_body);
140
141    let local_var_req = local_var_req_builder.build()?;
142    let local_var_resp = local_var_client.execute(local_var_req).await?;
143
144    if "DELETE" != "GET" && "DELETE" != "HEAD" {
145      let headers = local_var_resp.headers();
146      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
147          Some(v) => v.to_str().unwrap().parse().unwrap(),
148          None => configuration::DEFAULT_RATELIMIT,
149      };
150      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
151          Some(v) => v.to_str().unwrap().parse().unwrap(),
152          None => 0,
153      };
154    }
155
156    let local_var_status = local_var_resp.status();
157    let local_var_content = local_var_resp.text().await?;
158
159    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
160        Ok(())
161    } else {
162        let local_var_entity: Option<BulkRevokeTokensError> = serde_json::from_str(&local_var_content).ok();
163        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
164        Err(Error::ResponseError(local_var_error))
165    }
166}
167
168/// Create an API token. If two-factor authentication is enabled for your account, review [the instructions](https://www.fastly.com/documentation/reference/api/auth-tokens/user/) for including a one-time password in the request. 
169pub async fn create_token(configuration: &mut configuration::Configuration) -> Result<crate::models::TokenCreatedResponse, Error<CreateTokenError>> {
170    let local_var_configuration = configuration;
171
172    // unbox the parameters
173
174
175    let local_var_client = &local_var_configuration.client;
176
177    let local_var_uri_str = format!("{}/tokens", local_var_configuration.base_path);
178    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
179
180    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
181        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
182    }
183    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
184        let local_var_key = local_var_apikey.key.clone();
185        let local_var_value = match local_var_apikey.prefix {
186            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
187            None => local_var_key,
188        };
189        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
190    };
191    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
192        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
193    };
194
195    let local_var_req = local_var_req_builder.build()?;
196    let local_var_resp = local_var_client.execute(local_var_req).await?;
197
198    if "POST" != "GET" && "POST" != "HEAD" {
199      let headers = local_var_resp.headers();
200      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
201          Some(v) => v.to_str().unwrap().parse().unwrap(),
202          None => configuration::DEFAULT_RATELIMIT,
203      };
204      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
205          Some(v) => v.to_str().unwrap().parse().unwrap(),
206          None => 0,
207      };
208    }
209
210    let local_var_status = local_var_resp.status();
211    let local_var_content = local_var_resp.text().await?;
212
213    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
214        serde_json::from_str(&local_var_content).map_err(Error::from)
215    } else {
216        let local_var_entity: Option<CreateTokenError> = serde_json::from_str(&local_var_content).ok();
217        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
218        Err(Error::ResponseError(local_var_error))
219    }
220}
221
222/// Get a single token by its id.
223pub async fn get_token(configuration: &mut configuration::Configuration, params: GetTokenParams) -> Result<crate::models::TokenResponse, Error<GetTokenError>> {
224    let local_var_configuration = configuration;
225
226    // unbox the parameters
227    let token_id = params.token_id;
228
229
230    let local_var_client = &local_var_configuration.client;
231
232    let local_var_uri_str = format!("{}/tokens/{token_id}", local_var_configuration.base_path, token_id=crate::apis::urlencode(token_id));
233    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
234
235    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
236        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
237    }
238    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
239        let local_var_key = local_var_apikey.key.clone();
240        let local_var_value = match local_var_apikey.prefix {
241            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
242            None => local_var_key,
243        };
244        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
245    };
246
247    let local_var_req = local_var_req_builder.build()?;
248    let local_var_resp = local_var_client.execute(local_var_req).await?;
249
250    if "GET" != "GET" && "GET" != "HEAD" {
251      let headers = local_var_resp.headers();
252      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
253          Some(v) => v.to_str().unwrap().parse().unwrap(),
254          None => configuration::DEFAULT_RATELIMIT,
255      };
256      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
257          Some(v) => v.to_str().unwrap().parse().unwrap(),
258          None => 0,
259      };
260    }
261
262    let local_var_status = local_var_resp.status();
263    let local_var_content = local_var_resp.text().await?;
264
265    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
266        serde_json::from_str(&local_var_content).map_err(Error::from)
267    } else {
268        let local_var_entity: Option<GetTokenError> = serde_json::from_str(&local_var_content).ok();
269        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
270        Err(Error::ResponseError(local_var_error))
271    }
272}
273
274/// Get a single token based on the access_token used in the request.
275pub async fn get_token_current(configuration: &mut configuration::Configuration) -> Result<crate::models::TokenResponse, Error<GetTokenCurrentError>> {
276    let local_var_configuration = configuration;
277
278    // unbox the parameters
279
280
281    let local_var_client = &local_var_configuration.client;
282
283    let local_var_uri_str = format!("{}/tokens/self", local_var_configuration.base_path);
284    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
285
286    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
287        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
288    }
289    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
290        let local_var_key = local_var_apikey.key.clone();
291        let local_var_value = match local_var_apikey.prefix {
292            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
293            None => local_var_key,
294        };
295        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
296    };
297
298    let local_var_req = local_var_req_builder.build()?;
299    let local_var_resp = local_var_client.execute(local_var_req).await?;
300
301    if "GET" != "GET" && "GET" != "HEAD" {
302      let headers = local_var_resp.headers();
303      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
304          Some(v) => v.to_str().unwrap().parse().unwrap(),
305          None => configuration::DEFAULT_RATELIMIT,
306      };
307      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
308          Some(v) => v.to_str().unwrap().parse().unwrap(),
309          None => 0,
310      };
311    }
312
313    let local_var_status = local_var_resp.status();
314    let local_var_content = local_var_resp.text().await?;
315
316    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
317        serde_json::from_str(&local_var_content).map_err(Error::from)
318    } else {
319        let local_var_entity: Option<GetTokenCurrentError> = serde_json::from_str(&local_var_content).ok();
320        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
321        Err(Error::ResponseError(local_var_error))
322    }
323}
324
325/// List all tokens belonging to a specific customer.
326pub async fn list_tokens_customer(configuration: &mut configuration::Configuration, params: ListTokensCustomerParams) -> Result<Vec<crate::models::TokenResponse>, Error<ListTokensCustomerError>> {
327    let local_var_configuration = configuration;
328
329    // unbox the parameters
330    let customer_id = params.customer_id;
331
332
333    let local_var_client = &local_var_configuration.client;
334
335    let local_var_uri_str = format!("{}/customer/{customer_id}/tokens", local_var_configuration.base_path, customer_id=crate::apis::urlencode(customer_id));
336    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
337
338    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
339        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
340    }
341    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
342        let local_var_key = local_var_apikey.key.clone();
343        let local_var_value = match local_var_apikey.prefix {
344            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
345            None => local_var_key,
346        };
347        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
348    };
349
350    let local_var_req = local_var_req_builder.build()?;
351    let local_var_resp = local_var_client.execute(local_var_req).await?;
352
353    if "GET" != "GET" && "GET" != "HEAD" {
354      let headers = local_var_resp.headers();
355      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
356          Some(v) => v.to_str().unwrap().parse().unwrap(),
357          None => configuration::DEFAULT_RATELIMIT,
358      };
359      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
360          Some(v) => v.to_str().unwrap().parse().unwrap(),
361          None => 0,
362      };
363    }
364
365    let local_var_status = local_var_resp.status();
366    let local_var_content = local_var_resp.text().await?;
367
368    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
369        serde_json::from_str(&local_var_content).map_err(Error::from)
370    } else {
371        let local_var_entity: Option<ListTokensCustomerError> = serde_json::from_str(&local_var_content).ok();
372        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
373        Err(Error::ResponseError(local_var_error))
374    }
375}
376
377/// List all tokens belonging to the authenticated user.
378pub async fn list_tokens_user(configuration: &mut configuration::Configuration) -> Result<Vec<crate::models::TokenResponse>, Error<ListTokensUserError>> {
379    let local_var_configuration = configuration;
380
381    // unbox the parameters
382
383
384    let local_var_client = &local_var_configuration.client;
385
386    let local_var_uri_str = format!("{}/tokens", local_var_configuration.base_path);
387    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
388
389    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
390        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
391    }
392    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
393        let local_var_key = local_var_apikey.key.clone();
394        let local_var_value = match local_var_apikey.prefix {
395            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
396            None => local_var_key,
397        };
398        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
399    };
400
401    let local_var_req = local_var_req_builder.build()?;
402    let local_var_resp = local_var_client.execute(local_var_req).await?;
403
404    if "GET" != "GET" && "GET" != "HEAD" {
405      let headers = local_var_resp.headers();
406      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
407          Some(v) => v.to_str().unwrap().parse().unwrap(),
408          None => configuration::DEFAULT_RATELIMIT,
409      };
410      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
411          Some(v) => v.to_str().unwrap().parse().unwrap(),
412          None => 0,
413      };
414    }
415
416    let local_var_status = local_var_resp.status();
417    let local_var_content = local_var_resp.text().await?;
418
419    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
420        serde_json::from_str(&local_var_content).map_err(Error::from)
421    } else {
422        let local_var_entity: Option<ListTokensUserError> = serde_json::from_str(&local_var_content).ok();
423        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
424        Err(Error::ResponseError(local_var_error))
425    }
426}
427
428/// Revoke a specific token by its id.
429pub async fn revoke_token(configuration: &mut configuration::Configuration, params: RevokeTokenParams) -> Result<(), Error<RevokeTokenError>> {
430    let local_var_configuration = configuration;
431
432    // unbox the parameters
433    let token_id = params.token_id;
434
435
436    let local_var_client = &local_var_configuration.client;
437
438    let local_var_uri_str = format!("{}/tokens/{token_id}", local_var_configuration.base_path, token_id=crate::apis::urlencode(token_id));
439    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
440
441    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
442        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
443    }
444    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
445        let local_var_key = local_var_apikey.key.clone();
446        let local_var_value = match local_var_apikey.prefix {
447            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
448            None => local_var_key,
449        };
450        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
451    };
452
453    let local_var_req = local_var_req_builder.build()?;
454    let local_var_resp = local_var_client.execute(local_var_req).await?;
455
456    if "DELETE" != "GET" && "DELETE" != "HEAD" {
457      let headers = local_var_resp.headers();
458      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
459          Some(v) => v.to_str().unwrap().parse().unwrap(),
460          None => configuration::DEFAULT_RATELIMIT,
461      };
462      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
463          Some(v) => v.to_str().unwrap().parse().unwrap(),
464          None => 0,
465      };
466    }
467
468    let local_var_status = local_var_resp.status();
469    let local_var_content = local_var_resp.text().await?;
470
471    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
472        Ok(())
473    } else {
474        let local_var_entity: Option<RevokeTokenError> = serde_json::from_str(&local_var_content).ok();
475        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
476        Err(Error::ResponseError(local_var_error))
477    }
478}
479
480/// Revoke a token that is used to authenticate the request.
481pub async fn revoke_token_current(configuration: &mut configuration::Configuration) -> Result<(), Error<RevokeTokenCurrentError>> {
482    let local_var_configuration = configuration;
483
484    // unbox the parameters
485
486
487    let local_var_client = &local_var_configuration.client;
488
489    let local_var_uri_str = format!("{}/tokens/self", local_var_configuration.base_path);
490    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
491
492    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
493        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
494    }
495    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
496        let local_var_key = local_var_apikey.key.clone();
497        let local_var_value = match local_var_apikey.prefix {
498            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
499            None => local_var_key,
500        };
501        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
502    };
503
504    let local_var_req = local_var_req_builder.build()?;
505    let local_var_resp = local_var_client.execute(local_var_req).await?;
506
507    if "DELETE" != "GET" && "DELETE" != "HEAD" {
508      let headers = local_var_resp.headers();
509      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
510          Some(v) => v.to_str().unwrap().parse().unwrap(),
511          None => configuration::DEFAULT_RATELIMIT,
512      };
513      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
514          Some(v) => v.to_str().unwrap().parse().unwrap(),
515          None => 0,
516      };
517    }
518
519    let local_var_status = local_var_resp.status();
520    let local_var_content = local_var_resp.text().await?;
521
522    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
523        Ok(())
524    } else {
525        let local_var_entity: Option<RevokeTokenCurrentError> = serde_json::from_str(&local_var_content).ok();
526        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
527        Err(Error::ResponseError(local_var_error))
528    }
529}
530