fastly_api/apis/
billing_invoices_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_invoice_by_invoice_id`]
15#[derive(Clone, Debug, Default)]
16pub struct GetInvoiceByInvoiceIdParams {
17    pub invoice_id: i32
18}
19
20/// struct for passing parameters to the method [`list_invoices`]
21#[derive(Clone, Debug, Default)]
22pub struct ListInvoicesParams {
23    pub billing_start_date: Option<String>,
24    pub billing_end_date: Option<String>,
25    /// Number of results per page. The maximum is 200.
26    pub limit: Option<String>,
27    /// Cursor value from the `next_cursor` field of a previous response, used to retrieve the next page. To request the first page, this should be empty.
28    pub cursor: Option<String>
29}
30
31
32/// struct for typed errors of method [`get_invoice_by_invoice_id`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum GetInvoiceByInvoiceIdError {
36    Status401(crate::models::Error),
37    Status404(crate::models::Error),
38    Status500(crate::models::Error),
39    UnknownValue(serde_json::Value),
40}
41
42/// struct for typed errors of method [`get_month_to_date_invoice`]
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum GetMonthToDateInvoiceError {
46    Status401(crate::models::Error),
47    Status404(crate::models::Error),
48    Status422(crate::models::Error),
49    Status500(crate::models::Error),
50    UnknownValue(serde_json::Value),
51}
52
53/// struct for typed errors of method [`list_invoices`]
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[serde(untagged)]
56pub enum ListInvoicesError {
57    Status400(crate::models::Error),
58    Status401(crate::models::Error),
59    Status500(crate::models::Error),
60    UnknownValue(serde_json::Value),
61}
62
63
64/// Returns invoice associated with the invoice id.
65pub async fn get_invoice_by_invoice_id(configuration: &mut configuration::Configuration, params: GetInvoiceByInvoiceIdParams) -> Result<crate::models::EomInvoiceResponse, Error<GetInvoiceByInvoiceIdError>> {
66    let local_var_configuration = configuration;
67
68    // unbox the parameters
69    let invoice_id = params.invoice_id;
70
71
72    let local_var_client = &local_var_configuration.client;
73
74    let local_var_uri_str = format!("{}/billing/v3/invoices/{invoice_id}", local_var_configuration.base_path, invoice_id=invoice_id);
75    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
76
77    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
78        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
79    }
80    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
81        let local_var_key = local_var_apikey.key.clone();
82        let local_var_value = match local_var_apikey.prefix {
83            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
84            None => local_var_key,
85        };
86        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
87    };
88
89    let local_var_req = local_var_req_builder.build()?;
90    let local_var_resp = local_var_client.execute(local_var_req).await?;
91
92    if "GET" != "GET" && "GET" != "HEAD" {
93      let headers = local_var_resp.headers();
94      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
95          Some(v) => v.to_str().unwrap().parse().unwrap(),
96          None => configuration::DEFAULT_RATELIMIT,
97      };
98      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
99          Some(v) => v.to_str().unwrap().parse().unwrap(),
100          None => 0,
101      };
102    }
103
104    let local_var_status = local_var_resp.status();
105    let local_var_content = local_var_resp.text().await?;
106
107    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
108        serde_json::from_str(&local_var_content).map_err(Error::from)
109    } else {
110        let local_var_entity: Option<GetInvoiceByInvoiceIdError> = serde_json::from_str(&local_var_content).ok();
111        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
112        Err(Error::ResponseError(local_var_error))
113    }
114}
115
116/// Returns month-to-date invoice for the current month.
117pub async fn get_month_to_date_invoice(configuration: &mut configuration::Configuration) -> Result<crate::models::MtdInvoiceResponse, Error<GetMonthToDateInvoiceError>> {
118    let local_var_configuration = configuration;
119
120    // unbox the parameters
121
122
123    let local_var_client = &local_var_configuration.client;
124
125    let local_var_uri_str = format!("{}/billing/v3/invoices/month-to-date", local_var_configuration.base_path);
126    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, 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
140    let local_var_req = local_var_req_builder.build()?;
141    let local_var_resp = local_var_client.execute(local_var_req).await?;
142
143    if "GET" != "GET" && "GET" != "HEAD" {
144      let headers = local_var_resp.headers();
145      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
146          Some(v) => v.to_str().unwrap().parse().unwrap(),
147          None => configuration::DEFAULT_RATELIMIT,
148      };
149      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
150          Some(v) => v.to_str().unwrap().parse().unwrap(),
151          None => 0,
152      };
153    }
154
155    let local_var_status = local_var_resp.status();
156    let local_var_content = local_var_resp.text().await?;
157
158    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
159        serde_json::from_str(&local_var_content).map_err(Error::from)
160    } else {
161        let local_var_entity: Option<GetMonthToDateInvoiceError> = serde_json::from_str(&local_var_content).ok();
162        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
163        Err(Error::ResponseError(local_var_error))
164    }
165}
166
167/// Returns the list of invoices, sorted by billing start date (newest to oldest).
168pub async fn list_invoices(configuration: &mut configuration::Configuration, params: ListInvoicesParams) -> Result<crate::models::ListEomInvoicesResponse, Error<ListInvoicesError>> {
169    let local_var_configuration = configuration;
170
171    // unbox the parameters
172    let billing_start_date = params.billing_start_date;
173    let billing_end_date = params.billing_end_date;
174    let limit = params.limit;
175    let cursor = params.cursor;
176
177
178    let local_var_client = &local_var_configuration.client;
179
180    let local_var_uri_str = format!("{}/billing/v3/invoices", local_var_configuration.base_path);
181    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
182
183    if let Some(ref local_var_str) = billing_start_date {
184        local_var_req_builder = local_var_req_builder.query(&[("billing_start_date", &local_var_str.to_string())]);
185    }
186    if let Some(ref local_var_str) = billing_end_date {
187        local_var_req_builder = local_var_req_builder.query(&[("billing_end_date", &local_var_str.to_string())]);
188    }
189    if let Some(ref local_var_str) = limit {
190        local_var_req_builder = local_var_req_builder.query(&[("limit", &local_var_str.to_string())]);
191    }
192    if let Some(ref local_var_str) = cursor {
193        local_var_req_builder = local_var_req_builder.query(&[("cursor", &local_var_str.to_string())]);
194    }
195    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
196        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
197    }
198    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
199        let local_var_key = local_var_apikey.key.clone();
200        let local_var_value = match local_var_apikey.prefix {
201            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
202            None => local_var_key,
203        };
204        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
205    };
206
207    let local_var_req = local_var_req_builder.build()?;
208    let local_var_resp = local_var_client.execute(local_var_req).await?;
209
210    if "GET" != "GET" && "GET" != "HEAD" {
211      let headers = local_var_resp.headers();
212      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
213          Some(v) => v.to_str().unwrap().parse().unwrap(),
214          None => configuration::DEFAULT_RATELIMIT,
215      };
216      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
217          Some(v) => v.to_str().unwrap().parse().unwrap(),
218          None => 0,
219      };
220    }
221
222    let local_var_status = local_var_resp.status();
223    let local_var_content = local_var_resp.text().await?;
224
225    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
226        serde_json::from_str(&local_var_content).map_err(Error::from)
227    } else {
228        let local_var_entity: Option<ListInvoicesError> = serde_json::from_str(&local_var_content).ok();
229        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
230        Err(Error::ResponseError(local_var_error))
231    }
232}
233