fastly_api/apis/
resource_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_resource`]
15#[derive(Clone, Debug, Default)]
16pub struct CreateResourceParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    /// Integer identifying a service version.
20    pub version_id: i32,
21    /// The ID of the underlying linked resource.
22    pub resource_id: Option<String>,
23    /// The name of the resource link.
24    pub name: Option<String>
25}
26
27/// struct for passing parameters to the method [`delete_resource`]
28#[derive(Clone, Debug, Default)]
29pub struct DeleteResourceParams {
30    /// Alphanumeric string identifying the service.
31    pub service_id: String,
32    /// Integer identifying a service version.
33    pub version_id: i32,
34    /// An alphanumeric string identifying the resource link.
35    pub id: String
36}
37
38/// struct for passing parameters to the method [`get_resource`]
39#[derive(Clone, Debug, Default)]
40pub struct GetResourceParams {
41    /// Alphanumeric string identifying the service.
42    pub service_id: String,
43    /// Integer identifying a service version.
44    pub version_id: i32,
45    /// An alphanumeric string identifying the resource link.
46    pub id: String
47}
48
49/// struct for passing parameters to the method [`list_resources`]
50#[derive(Clone, Debug, Default)]
51pub struct ListResourcesParams {
52    /// Alphanumeric string identifying the service.
53    pub service_id: String,
54    /// Integer identifying a service version.
55    pub version_id: i32
56}
57
58/// struct for passing parameters to the method [`update_resource`]
59#[derive(Clone, Debug, Default)]
60pub struct UpdateResourceParams {
61    /// Alphanumeric string identifying the service.
62    pub service_id: String,
63    /// Integer identifying a service version.
64    pub version_id: i32,
65    /// An alphanumeric string identifying the resource link.
66    pub id: String,
67    /// The ID of the underlying linked resource.
68    pub resource_id: Option<String>,
69    /// The name of the resource link.
70    pub name: Option<String>
71}
72
73
74/// struct for typed errors of method [`create_resource`]
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(untagged)]
77pub enum CreateResourceError {
78    UnknownValue(serde_json::Value),
79}
80
81/// struct for typed errors of method [`delete_resource`]
82#[derive(Debug, Clone, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum DeleteResourceError {
85    UnknownValue(serde_json::Value),
86}
87
88/// struct for typed errors of method [`get_resource`]
89#[derive(Debug, Clone, Serialize, Deserialize)]
90#[serde(untagged)]
91pub enum GetResourceError {
92    UnknownValue(serde_json::Value),
93}
94
95/// struct for typed errors of method [`list_resources`]
96#[derive(Debug, Clone, Serialize, Deserialize)]
97#[serde(untagged)]
98pub enum ListResourcesError {
99    UnknownValue(serde_json::Value),
100}
101
102/// struct for typed errors of method [`update_resource`]
103#[derive(Debug, Clone, Serialize, Deserialize)]
104#[serde(untagged)]
105pub enum UpdateResourceError {
106    UnknownValue(serde_json::Value),
107}
108
109
110/// Create a link between a resource and a service version.
111pub async fn create_resource(configuration: &mut configuration::Configuration, params: CreateResourceParams) -> Result<crate::models::ResourceResponse, Error<CreateResourceError>> {
112    let local_var_configuration = configuration;
113
114    // unbox the parameters
115    let service_id = params.service_id;
116    let version_id = params.version_id;
117    let resource_id = params.resource_id;
118    let name = params.name;
119
120
121    let local_var_client = &local_var_configuration.client;
122
123    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/resource", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
124    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
125
126    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
127        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
128    }
129    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
130        let local_var_key = local_var_apikey.key.clone();
131        let local_var_value = match local_var_apikey.prefix {
132            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
133            None => local_var_key,
134        };
135        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
136    };
137    let mut local_var_form_params = std::collections::HashMap::new();
138    if let Some(local_var_param_value) = resource_id {
139        local_var_form_params.insert("resource_id", local_var_param_value.to_string());
140    }
141    if let Some(local_var_param_value) = name {
142        local_var_form_params.insert("name", local_var_param_value.to_string());
143    }
144    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
145
146    let local_var_req = local_var_req_builder.build()?;
147    let local_var_resp = local_var_client.execute(local_var_req).await?;
148
149    if "POST" != "GET" && "POST" != "HEAD" {
150      let headers = local_var_resp.headers();
151      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
152          Some(v) => v.to_str().unwrap().parse().unwrap(),
153          None => configuration::DEFAULT_RATELIMIT,
154      };
155      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
156          Some(v) => v.to_str().unwrap().parse().unwrap(),
157          None => 0,
158      };
159    }
160
161    let local_var_status = local_var_resp.status();
162    let local_var_content = local_var_resp.text().await?;
163
164    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
165        serde_json::from_str(&local_var_content).map_err(Error::from)
166    } else {
167        let local_var_entity: Option<CreateResourceError> = serde_json::from_str(&local_var_content).ok();
168        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
169        Err(Error::ResponseError(local_var_error))
170    }
171}
172
173/// Delete a link between a resource and a service version.
174pub async fn delete_resource(configuration: &mut configuration::Configuration, params: DeleteResourceParams) -> Result<crate::models::InlineResponse200, Error<DeleteResourceError>> {
175    let local_var_configuration = configuration;
176
177    // unbox the parameters
178    let service_id = params.service_id;
179    let version_id = params.version_id;
180    let id = params.id;
181
182
183    let local_var_client = &local_var_configuration.client;
184
185    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/resource/{id}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, id=crate::apis::urlencode(id));
186    let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
187
188    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
189        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
190    }
191    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
192        let local_var_key = local_var_apikey.key.clone();
193        let local_var_value = match local_var_apikey.prefix {
194            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
195            None => local_var_key,
196        };
197        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
198    };
199
200    let local_var_req = local_var_req_builder.build()?;
201    let local_var_resp = local_var_client.execute(local_var_req).await?;
202
203    if "DELETE" != "GET" && "DELETE" != "HEAD" {
204      let headers = local_var_resp.headers();
205      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
206          Some(v) => v.to_str().unwrap().parse().unwrap(),
207          None => configuration::DEFAULT_RATELIMIT,
208      };
209      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
210          Some(v) => v.to_str().unwrap().parse().unwrap(),
211          None => 0,
212      };
213    }
214
215    let local_var_status = local_var_resp.status();
216    let local_var_content = local_var_resp.text().await?;
217
218    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
219        serde_json::from_str(&local_var_content).map_err(Error::from)
220    } else {
221        let local_var_entity: Option<DeleteResourceError> = serde_json::from_str(&local_var_content).ok();
222        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
223        Err(Error::ResponseError(local_var_error))
224    }
225}
226
227/// Display a resource link by its identifier.
228pub async fn get_resource(configuration: &mut configuration::Configuration, params: GetResourceParams) -> Result<crate::models::ResourceResponse, Error<GetResourceError>> {
229    let local_var_configuration = configuration;
230
231    // unbox the parameters
232    let service_id = params.service_id;
233    let version_id = params.version_id;
234    let id = params.id;
235
236
237    let local_var_client = &local_var_configuration.client;
238
239    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/resource/{id}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, id=crate::apis::urlencode(id));
240    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
241
242    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
243        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
244    }
245    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
246        let local_var_key = local_var_apikey.key.clone();
247        let local_var_value = match local_var_apikey.prefix {
248            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
249            None => local_var_key,
250        };
251        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
252    };
253
254    let local_var_req = local_var_req_builder.build()?;
255    let local_var_resp = local_var_client.execute(local_var_req).await?;
256
257    if "GET" != "GET" && "GET" != "HEAD" {
258      let headers = local_var_resp.headers();
259      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
260          Some(v) => v.to_str().unwrap().parse().unwrap(),
261          None => configuration::DEFAULT_RATELIMIT,
262      };
263      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
264          Some(v) => v.to_str().unwrap().parse().unwrap(),
265          None => 0,
266      };
267    }
268
269    let local_var_status = local_var_resp.status();
270    let local_var_content = local_var_resp.text().await?;
271
272    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
273        serde_json::from_str(&local_var_content).map_err(Error::from)
274    } else {
275        let local_var_entity: Option<GetResourceError> = serde_json::from_str(&local_var_content).ok();
276        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
277        Err(Error::ResponseError(local_var_error))
278    }
279}
280
281/// List links between resources and services
282pub async fn list_resources(configuration: &mut configuration::Configuration, params: ListResourcesParams) -> Result<Vec<crate::models::ResourceResponse>, Error<ListResourcesError>> {
283    let local_var_configuration = configuration;
284
285    // unbox the parameters
286    let service_id = params.service_id;
287    let version_id = params.version_id;
288
289
290    let local_var_client = &local_var_configuration.client;
291
292    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/resource", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
293    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
294
295    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
296        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
297    }
298    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
299        let local_var_key = local_var_apikey.key.clone();
300        let local_var_value = match local_var_apikey.prefix {
301            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
302            None => local_var_key,
303        };
304        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
305    };
306
307    let local_var_req = local_var_req_builder.build()?;
308    let local_var_resp = local_var_client.execute(local_var_req).await?;
309
310    if "GET" != "GET" && "GET" != "HEAD" {
311      let headers = local_var_resp.headers();
312      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
313          Some(v) => v.to_str().unwrap().parse().unwrap(),
314          None => configuration::DEFAULT_RATELIMIT,
315      };
316      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
317          Some(v) => v.to_str().unwrap().parse().unwrap(),
318          None => 0,
319      };
320    }
321
322    let local_var_status = local_var_resp.status();
323    let local_var_content = local_var_resp.text().await?;
324
325    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
326        serde_json::from_str(&local_var_content).map_err(Error::from)
327    } else {
328        let local_var_entity: Option<ListResourcesError> = serde_json::from_str(&local_var_content).ok();
329        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
330        Err(Error::ResponseError(local_var_error))
331    }
332}
333
334/// Update a link between a resource and a service version.
335pub async fn update_resource(configuration: &mut configuration::Configuration, params: UpdateResourceParams) -> Result<crate::models::ResourceResponse, Error<UpdateResourceError>> {
336    let local_var_configuration = configuration;
337
338    // unbox the parameters
339    let service_id = params.service_id;
340    let version_id = params.version_id;
341    let id = params.id;
342    let resource_id = params.resource_id;
343    let name = params.name;
344
345
346    let local_var_client = &local_var_configuration.client;
347
348    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/resource/{id}", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id, id=crate::apis::urlencode(id));
349    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
350
351    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
352        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
353    }
354    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
355        let local_var_key = local_var_apikey.key.clone();
356        let local_var_value = match local_var_apikey.prefix {
357            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
358            None => local_var_key,
359        };
360        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
361    };
362    let mut local_var_form_params = std::collections::HashMap::new();
363    if let Some(local_var_param_value) = resource_id {
364        local_var_form_params.insert("resource_id", local_var_param_value.to_string());
365    }
366    if let Some(local_var_param_value) = name {
367        local_var_form_params.insert("name", local_var_param_value.to_string());
368    }
369    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
370
371    let local_var_req = local_var_req_builder.build()?;
372    let local_var_resp = local_var_client.execute(local_var_req).await?;
373
374    if "PUT" != "GET" && "PUT" != "HEAD" {
375      let headers = local_var_resp.headers();
376      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
377          Some(v) => v.to_str().unwrap().parse().unwrap(),
378          None => configuration::DEFAULT_RATELIMIT,
379      };
380      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
381          Some(v) => v.to_str().unwrap().parse().unwrap(),
382          None => 0,
383      };
384    }
385
386    let local_var_status = local_var_resp.status();
387    let local_var_content = local_var_resp.text().await?;
388
389    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
390        serde_json::from_str(&local_var_content).map_err(Error::from)
391    } else {
392        let local_var_entity: Option<UpdateResourceError> = serde_json::from_str(&local_var_content).ok();
393        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
394        Err(Error::ResponseError(local_var_error))
395    }
396}
397