fastly_api/apis/
package_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_package`]
15#[derive(Clone, Debug, Default)]
16pub struct GetPackageParams {
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 [`put_package`]
24#[derive(Clone, Debug, Default)]
25pub struct PutPackageParams {
26    /// Alphanumeric string identifying the service.
27    pub service_id: String,
28    /// Integer identifying a service version.
29    pub version_id: i32,
30    /// We recommend using the Expect header because it may identify issues with the request based upon the headers alone instead of requiring you to wait until the entire binary package upload has completed.
31    pub expect: Option<String>,
32    /// The content of the Wasm binary package.
33    pub package: Option<std::path::PathBuf>
34}
35
36
37/// struct for typed errors of method [`get_package`]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum GetPackageError {
41    UnknownValue(serde_json::Value),
42}
43
44/// struct for typed errors of method [`put_package`]
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(untagged)]
47pub enum PutPackageError {
48    UnknownValue(serde_json::Value),
49}
50
51
52/// List detailed information about the Compute package for the specified service.
53pub async fn get_package(configuration: &mut configuration::Configuration, params: GetPackageParams) -> Result<crate::models::PackageResponse, Error<GetPackageError>> {
54    let local_var_configuration = configuration;
55
56    // unbox the parameters
57    let service_id = params.service_id;
58    let version_id = params.version_id;
59
60
61    let local_var_client = &local_var_configuration.client;
62
63    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/package", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
64    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
65
66    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
67        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
68    }
69    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
70        let local_var_key = local_var_apikey.key.clone();
71        let local_var_value = match local_var_apikey.prefix {
72            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
73            None => local_var_key,
74        };
75        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
76    };
77
78    let local_var_req = local_var_req_builder.build()?;
79    let local_var_resp = local_var_client.execute(local_var_req).await?;
80
81    if "GET" != "GET" && "GET" != "HEAD" {
82      let headers = local_var_resp.headers();
83      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
84          Some(v) => v.to_str().unwrap().parse().unwrap(),
85          None => configuration::DEFAULT_RATELIMIT,
86      };
87      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
88          Some(v) => v.to_str().unwrap().parse().unwrap(),
89          None => 0,
90      };
91    }
92
93    let local_var_status = local_var_resp.status();
94    let local_var_content = local_var_resp.text().await?;
95
96    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
97        serde_json::from_str(&local_var_content).map_err(Error::from)
98    } else {
99        let local_var_entity: Option<GetPackageError> = serde_json::from_str(&local_var_content).ok();
100        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
101        Err(Error::ResponseError(local_var_error))
102    }
103}
104
105/// Upload a Compute package associated with the specified service version.
106pub async fn put_package(configuration: &mut configuration::Configuration, params: PutPackageParams) -> Result<crate::models::PackageResponse, Error<PutPackageError>> {
107    let local_var_configuration = configuration;
108
109    // unbox the parameters
110    let service_id = params.service_id;
111    let version_id = params.version_id;
112    let expect = params.expect;
113    let package = params.package;
114
115
116    let local_var_client = &local_var_configuration.client;
117
118    let local_var_uri_str = format!("{}/service/{service_id}/version/{version_id}/package", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id), version_id=version_id);
119    let mut local_var_req_builder = local_var_client.request(reqwest::Method::PUT, local_var_uri_str.as_str());
120
121    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
122        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
123    }
124    if let Some(local_var_param_value) = expect {
125        local_var_req_builder = local_var_req_builder.header("expect", local_var_param_value.to_string());
126    }
127    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
128        let local_var_key = local_var_apikey.key.clone();
129        let local_var_value = match local_var_apikey.prefix {
130            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
131            None => local_var_key,
132        };
133        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
134    };
135    let mut local_var_form = reqwest::multipart::Form::new();
136    // TODO: support file upload for 'package' parameter
137    local_var_req_builder = local_var_req_builder.multipart(local_var_form);
138
139    let local_var_req = local_var_req_builder.build()?;
140    let local_var_resp = local_var_client.execute(local_var_req).await?;
141
142    if "PUT" != "GET" && "PUT" != "HEAD" {
143      let headers = local_var_resp.headers();
144      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
145          Some(v) => v.to_str().unwrap().parse().unwrap(),
146          None => configuration::DEFAULT_RATELIMIT,
147      };
148      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
149          Some(v) => v.to_str().unwrap().parse().unwrap(),
150          None => 0,
151      };
152    }
153
154    let local_var_status = local_var_resp.status();
155    let local_var_content = local_var_resp.text().await?;
156
157    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
158        serde_json::from_str(&local_var_content).map_err(Error::from)
159    } else {
160        let local_var_entity: Option<PutPackageError> = serde_json::from_str(&local_var_content).ok();
161        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
162        Err(Error::ResponseError(local_var_error))
163    }
164}
165