fastly_api/apis/
billing_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`]
15#[derive(Clone, Debug, Default)]
16pub struct GetInvoiceParams {
17    /// 2-digit month.
18    pub month: String,
19    /// 4-digit year.
20    pub year: String
21}
22
23/// struct for passing parameters to the method [`get_invoice_by_id`]
24#[derive(Clone, Debug, Default)]
25pub struct GetInvoiceByIdParams {
26    /// Alphanumeric string identifying the customer.
27    pub customer_id: String,
28    pub invoice_id: i32
29}
30
31/// struct for passing parameters to the method [`get_invoice_mtd`]
32#[derive(Clone, Debug, Default)]
33pub struct GetInvoiceMtdParams {
34    /// Alphanumeric string identifying the customer.
35    pub customer_id: String,
36    /// 2-digit month.
37    pub month: Option<String>,
38    /// 4-digit year.
39    pub year: Option<String>
40}
41
42
43/// struct for typed errors of method [`get_invoice`]
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(untagged)]
46pub enum GetInvoiceError {
47    UnknownValue(serde_json::Value),
48}
49
50/// struct for typed errors of method [`get_invoice_by_id`]
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum GetInvoiceByIdError {
54    UnknownValue(serde_json::Value),
55}
56
57/// struct for typed errors of method [`get_invoice_mtd`]
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(untagged)]
60pub enum GetInvoiceMtdError {
61    UnknownValue(serde_json::Value),
62}
63
64
65/// Get the invoice for a given year and month. Can be any month from when the Customer was created to the current month.
66pub async fn get_invoice(configuration: &mut configuration::Configuration, params: GetInvoiceParams) -> Result<crate::models::BillingResponse, Error<GetInvoiceError>> {
67    let local_var_configuration = configuration;
68
69    // unbox the parameters
70    let month = params.month;
71    let year = params.year;
72
73
74    let local_var_client = &local_var_configuration.client;
75
76    let local_var_uri_str = format!("{}/billing/v2/year/{year}/month/{month}", local_var_configuration.base_path, month=crate::apis::urlencode(month), year=crate::apis::urlencode(year));
77    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
78
79    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
80        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
81    }
82    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
83        let local_var_key = local_var_apikey.key.clone();
84        let local_var_value = match local_var_apikey.prefix {
85            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
86            None => local_var_key,
87        };
88        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
89    };
90
91    let local_var_req = local_var_req_builder.build()?;
92    let local_var_resp = local_var_client.execute(local_var_req).await?;
93
94    if "GET" != "GET" && "GET" != "HEAD" {
95      let headers = local_var_resp.headers();
96      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
97          Some(v) => v.to_str().unwrap().parse().unwrap(),
98          None => configuration::DEFAULT_RATELIMIT,
99      };
100      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
101          Some(v) => v.to_str().unwrap().parse().unwrap(),
102          None => 0,
103      };
104    }
105
106    let local_var_status = local_var_resp.status();
107    let local_var_content = local_var_resp.text().await?;
108
109    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
110        serde_json::from_str(&local_var_content).map_err(Error::from)
111    } else {
112        let local_var_entity: Option<GetInvoiceError> = serde_json::from_str(&local_var_content).ok();
113        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
114        Err(Error::ResponseError(local_var_error))
115    }
116}
117
118/// Get the invoice for the given invoice_id.
119pub async fn get_invoice_by_id(configuration: &mut configuration::Configuration, params: GetInvoiceByIdParams) -> Result<crate::models::BillingResponse, Error<GetInvoiceByIdError>> {
120    let local_var_configuration = configuration;
121
122    // unbox the parameters
123    let customer_id = params.customer_id;
124    let invoice_id = params.invoice_id;
125
126
127    let local_var_client = &local_var_configuration.client;
128
129    let local_var_uri_str = format!("{}/billing/v2/account_customers/{customer_id}/invoices/{invoice_id}", local_var_configuration.base_path, customer_id=crate::apis::urlencode(customer_id), invoice_id=invoice_id);
130    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
131
132    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
133        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
134    }
135    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
136        let local_var_key = local_var_apikey.key.clone();
137        let local_var_value = match local_var_apikey.prefix {
138            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
139            None => local_var_key,
140        };
141        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
142    };
143
144    let local_var_req = local_var_req_builder.build()?;
145    let local_var_resp = local_var_client.execute(local_var_req).await?;
146
147    if "GET" != "GET" && "GET" != "HEAD" {
148      let headers = local_var_resp.headers();
149      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
150          Some(v) => v.to_str().unwrap().parse().unwrap(),
151          None => configuration::DEFAULT_RATELIMIT,
152      };
153      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
154          Some(v) => v.to_str().unwrap().parse().unwrap(),
155          None => 0,
156      };
157    }
158
159    let local_var_status = local_var_resp.status();
160    let local_var_content = local_var_resp.text().await?;
161
162    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
163        serde_json::from_str(&local_var_content).map_err(Error::from)
164    } else {
165        let local_var_entity: Option<GetInvoiceByIdError> = serde_json::from_str(&local_var_content).ok();
166        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
167        Err(Error::ResponseError(local_var_error))
168    }
169}
170
171/// Get the current month-to-date estimate. This endpoint has two different responses. Under normal circumstances, it generally takes less than 5 seconds to generate but in certain cases can take up to 60 seconds. Once generated the month-to-date estimate is cached for 4 hours, and is available the next request will return the JSON representation of the month-to-date estimate. While a report is being generated in the background, this endpoint will return a `202 Accepted` response. The full format of which can be found in detail in our [billing calculation guide](https://docs.fastly.com/en/guides/how-we-calculate-your-bill). There are certain accounts for which we are unable to generate a month-to-date estimate. For example, accounts who have parent-pay are unable to generate an MTD estimate. The parent accounts are able to generate a month-to-date estimate but that estimate will not include the child accounts amounts at this time.
172pub async fn get_invoice_mtd(configuration: &mut configuration::Configuration, params: GetInvoiceMtdParams) -> Result<crate::models::BillingEstimateResponse, Error<GetInvoiceMtdError>> {
173    let local_var_configuration = configuration;
174
175    // unbox the parameters
176    let customer_id = params.customer_id;
177    let month = params.month;
178    let year = params.year;
179
180
181    let local_var_client = &local_var_configuration.client;
182
183    let local_var_uri_str = format!("{}/billing/v2/account_customers/{customer_id}/mtd_invoice", local_var_configuration.base_path, customer_id=crate::apis::urlencode(customer_id));
184    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
185
186    if let Some(ref local_var_str) = month {
187        local_var_req_builder = local_var_req_builder.query(&[("month", &local_var_str.to_string())]);
188    }
189    if let Some(ref local_var_str) = year {
190        local_var_req_builder = local_var_req_builder.query(&[("year", &local_var_str.to_string())]);
191    }
192    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
193        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
194    }
195    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
196        let local_var_key = local_var_apikey.key.clone();
197        let local_var_value = match local_var_apikey.prefix {
198            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
199            None => local_var_key,
200        };
201        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
202    };
203
204    let local_var_req = local_var_req_builder.build()?;
205    let local_var_resp = local_var_client.execute(local_var_req).await?;
206
207    if "GET" != "GET" && "GET" != "HEAD" {
208      let headers = local_var_resp.headers();
209      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
210          Some(v) => v.to_str().unwrap().parse().unwrap(),
211          None => configuration::DEFAULT_RATELIMIT,
212      };
213      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
214          Some(v) => v.to_str().unwrap().parse().unwrap(),
215          None => 0,
216      };
217    }
218
219    let local_var_status = local_var_resp.status();
220    let local_var_content = local_var_resp.text().await?;
221
222    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
223        serde_json::from_str(&local_var_content).map_err(Error::from)
224    } else {
225        let local_var_entity: Option<GetInvoiceMtdError> = serde_json::from_str(&local_var_content).ok();
226        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
227        Err(Error::ResponseError(local_var_error))
228    }
229}
230