space_traders/apis/
contracts_api.rs

1//!
2//!
3//! Generated by: <https://openapi-generator.tech>
4//!
5//! Version of specification: `2.0.0`
6
7use reqwest::StatusCode;
8
9use super::{configuration, Error};
10use crate::apis::ResponseContent;
11
12/// Enum for successes of method [`accept_contract`].
13#[derive(Debug, Clone)]
14pub enum AcceptContractSuccess {
15    /// Response for status code 200.
16    Status200(crate::models::AcceptContract200Response),
17}
18
19impl AcceptContractSuccess {
20    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
21        // Attempt to deserialize the response for the given status code.
22        match status.as_u16() {
23            200 => Some(match serde_json::from_str(body) {
24                Ok(data) => Ok(Self::Status200(data)),
25                Err(err) => Err(err),
26            }),
27            _ => None,
28        }
29    }
30}
31
32/// Enum for successes of method [`deliver_contract`].
33#[derive(Debug, Clone)]
34pub enum DeliverContractSuccess {
35    /// Response for status code 200.
36    Status200(crate::models::DeliverContract200Response),
37}
38
39impl DeliverContractSuccess {
40    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
41        // Attempt to deserialize the response for the given status code.
42        match status.as_u16() {
43            200 => Some(match serde_json::from_str(body) {
44                Ok(data) => Ok(Self::Status200(data)),
45                Err(err) => Err(err),
46            }),
47            _ => None,
48        }
49    }
50}
51
52/// Enum for successes of method [`fulfill_contract`].
53#[derive(Debug, Clone)]
54pub enum FulfillContractSuccess {
55    /// Response for status code 200.
56    Status200(crate::models::FulfillContract200Response),
57}
58
59impl FulfillContractSuccess {
60    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
61        // Attempt to deserialize the response for the given status code.
62        match status.as_u16() {
63            200 => Some(match serde_json::from_str(body) {
64                Ok(data) => Ok(Self::Status200(data)),
65                Err(err) => Err(err),
66            }),
67            _ => None,
68        }
69    }
70}
71
72/// Enum for successes of method [`get_contract`].
73#[derive(Debug, Clone)]
74pub enum GetContractSuccess {
75    /// Response for status code 200.
76    Status200(crate::models::GetContract200Response),
77}
78
79impl GetContractSuccess {
80    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
81        // Attempt to deserialize the response for the given status code.
82        match status.as_u16() {
83            200 => Some(match serde_json::from_str(body) {
84                Ok(data) => Ok(Self::Status200(data)),
85                Err(err) => Err(err),
86            }),
87            _ => None,
88        }
89    }
90}
91
92/// Enum for successes of method [`get_contracts`].
93#[derive(Debug, Clone)]
94pub enum GetContractsSuccess {
95    /// Response for status code 200.
96    Status200(crate::models::GetContracts200Response),
97}
98
99impl GetContractsSuccess {
100    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
101        // Attempt to deserialize the response for the given status code.
102        match status.as_u16() {
103            200 => Some(match serde_json::from_str(body) {
104                Ok(data) => Ok(Self::Status200(data)),
105                Err(err) => Err(err),
106            }),
107            _ => None,
108        }
109    }
110}
111
112/// Enum for known errors of method [`accept_contract`].
113#[derive(Debug, Clone)]
114pub enum AcceptContractError {}
115
116impl AcceptContractError {
117    #[allow(unused_variables, clippy::match_single_binding)]
118    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
119        // Attempt to deserialize the response for the given status code.
120        match status.as_u16() {
121            _ => None,
122        }
123    }
124}
125
126/// Enum for known errors of method [`deliver_contract`].
127#[derive(Debug, Clone)]
128pub enum DeliverContractError {}
129
130impl DeliverContractError {
131    #[allow(unused_variables, clippy::match_single_binding)]
132    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
133        // Attempt to deserialize the response for the given status code.
134        match status.as_u16() {
135            _ => None,
136        }
137    }
138}
139
140/// Enum for known errors of method [`fulfill_contract`].
141#[derive(Debug, Clone)]
142pub enum FulfillContractError {}
143
144impl FulfillContractError {
145    #[allow(unused_variables, clippy::match_single_binding)]
146    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
147        // Attempt to deserialize the response for the given status code.
148        match status.as_u16() {
149            _ => None,
150        }
151    }
152}
153
154/// Enum for known errors of method [`get_contract`].
155#[derive(Debug, Clone)]
156pub enum GetContractError {}
157
158impl GetContractError {
159    #[allow(unused_variables, clippy::match_single_binding)]
160    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
161        // Attempt to deserialize the response for the given status code.
162        match status.as_u16() {
163            _ => None,
164        }
165    }
166}
167
168/// Enum for known errors of method [`get_contracts`].
169#[derive(Debug, Clone)]
170pub enum GetContractsError {}
171
172impl GetContractsError {
173    #[allow(unused_variables, clippy::match_single_binding)]
174    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
175        // Attempt to deserialize the response for the given status code.
176        match status.as_u16() {
177            _ => None,
178        }
179    }
180}
181
182/// Accept a contract by ID.   You can only accept contracts that were offered to you, were not accepted yet, and whose deadlines has not passed yet.
183pub async fn accept_contract(
184    configuration: &configuration::Configuration,
185    contract_id: &str,
186) -> Result<ResponseContent<AcceptContractSuccess>, Error<AcceptContractError>> {
187    let client = &configuration.client;
188
189    // Create the request from a path.
190    // Make sure to url encode any user given text.
191    let uri_str = format!(
192        "{}/my/contracts/{contractId}/accept",
193        configuration.base_path,
194        contractId = crate::apis::urlencode(contract_id)
195    );
196    let mut req_builder = client.request(reqwest::Method::POST, &uri_str);
197
198    // === Add headers to request ===
199
200    // Set the user agent string if given.
201    if let Some(user_agent) = &configuration.user_agent {
202        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
203    }
204
205    // === Add auth to request ===
206
207    if let Some(token) = &configuration.bearer_access_token {
208        req_builder = req_builder.bearer_auth(token.to_owned());
209    };
210
211    // === Request is built.
212
213    // Execute the request.
214    let req = req_builder.build()?;
215    let resp = client.execute(req).await?;
216
217    // Get the response.
218    let status = resp.status();
219    let response_body = resp.text().await?;
220
221    if !status.is_client_error() && !status.is_server_error() {
222        // Try to parse the OK response.
223        match AcceptContractSuccess::from_body(status, &response_body) {
224            Some(Ok(content)) => Ok(ResponseContent {
225                status,
226                response_body,
227                content,
228            }),
229            Some(Err(err)) => Err(err.into()),
230            None => Err(Error::UnknownResponse {
231                status,
232                is_error: false,
233                response_body,
234            }),
235        }
236    } else {
237        // Try to parse the Err response.
238        match AcceptContractError::from_body(status, &response_body) {
239            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
240                status,
241                response_body,
242                content,
243            })),
244            Some(Err(err)) => Err(err.into()),
245            None => Err(Error::UnknownResponse {
246                status,
247                is_error: true,
248                response_body,
249            }),
250        }
251    }
252}
253
254/// Deliver cargo to a contract.  In order to use this API, a ship must be at the delivery location (denoted in the delivery terms as `destinationSymbol` of a contract) and must have a number of units of a good required by this contract in its cargo.  Cargo that was delivered will be removed from the ship's cargo.
255pub async fn deliver_contract(
256    configuration: &configuration::Configuration,
257    contract_id: &str,
258    deliver_contract_request: Option<crate::models::DeliverContractRequest>,
259) -> Result<ResponseContent<DeliverContractSuccess>, Error<DeliverContractError>> {
260    let client = &configuration.client;
261
262    // Create the request from a path.
263    // Make sure to url encode any user given text.
264    let uri_str = format!(
265        "{}/my/contracts/{contractId}/deliver",
266        configuration.base_path,
267        contractId = crate::apis::urlencode(contract_id)
268    );
269    let mut req_builder = client.request(reqwest::Method::POST, &uri_str);
270
271    // === Add headers to request ===
272
273    // Set the user agent string if given.
274    if let Some(user_agent) = &configuration.user_agent {
275        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
276    }
277
278    // === Add auth to request ===
279
280    if let Some(token) = &configuration.bearer_access_token {
281        req_builder = req_builder.bearer_auth(token.to_owned());
282    };
283
284    // === Add body to request ===
285
286    req_builder = req_builder.json(&deliver_contract_request);
287
288    // === Request is built.
289
290    // Execute the request.
291    let req = req_builder.build()?;
292    let resp = client.execute(req).await?;
293
294    // Get the response.
295    let status = resp.status();
296    let response_body = resp.text().await?;
297
298    if !status.is_client_error() && !status.is_server_error() {
299        // Try to parse the OK response.
300        match DeliverContractSuccess::from_body(status, &response_body) {
301            Some(Ok(content)) => Ok(ResponseContent {
302                status,
303                response_body,
304                content,
305            }),
306            Some(Err(err)) => Err(err.into()),
307            None => Err(Error::UnknownResponse {
308                status,
309                is_error: false,
310                response_body,
311            }),
312        }
313    } else {
314        // Try to parse the Err response.
315        match DeliverContractError::from_body(status, &response_body) {
316            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
317                status,
318                response_body,
319                content,
320            })),
321            Some(Err(err)) => Err(err.into()),
322            None => Err(Error::UnknownResponse {
323                status,
324                is_error: true,
325                response_body,
326            }),
327        }
328    }
329}
330
331/// Fulfill a contract. Can only be used on contracts that have all of their delivery terms fulfilled.
332pub async fn fulfill_contract(
333    configuration: &configuration::Configuration,
334    contract_id: &str,
335) -> Result<ResponseContent<FulfillContractSuccess>, Error<FulfillContractError>> {
336    let client = &configuration.client;
337
338    // Create the request from a path.
339    // Make sure to url encode any user given text.
340    let uri_str = format!(
341        "{}/my/contracts/{contractId}/fulfill",
342        configuration.base_path,
343        contractId = crate::apis::urlencode(contract_id)
344    );
345    let mut req_builder = client.request(reqwest::Method::POST, &uri_str);
346
347    // === Add headers to request ===
348
349    // Set the user agent string if given.
350    if let Some(user_agent) = &configuration.user_agent {
351        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
352    }
353
354    // === Add auth to request ===
355
356    if let Some(token) = &configuration.bearer_access_token {
357        req_builder = req_builder.bearer_auth(token.to_owned());
358    };
359
360    // === Request is built.
361
362    // Execute the request.
363    let req = req_builder.build()?;
364    let resp = client.execute(req).await?;
365
366    // Get the response.
367    let status = resp.status();
368    let response_body = resp.text().await?;
369
370    if !status.is_client_error() && !status.is_server_error() {
371        // Try to parse the OK response.
372        match FulfillContractSuccess::from_body(status, &response_body) {
373            Some(Ok(content)) => Ok(ResponseContent {
374                status,
375                response_body,
376                content,
377            }),
378            Some(Err(err)) => Err(err.into()),
379            None => Err(Error::UnknownResponse {
380                status,
381                is_error: false,
382                response_body,
383            }),
384        }
385    } else {
386        // Try to parse the Err response.
387        match FulfillContractError::from_body(status, &response_body) {
388            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
389                status,
390                response_body,
391                content,
392            })),
393            Some(Err(err)) => Err(err.into()),
394            None => Err(Error::UnknownResponse {
395                status,
396                is_error: true,
397                response_body,
398            }),
399        }
400    }
401}
402
403/// Get the details of a contract by ID.
404pub async fn get_contract(
405    configuration: &configuration::Configuration,
406    contract_id: &str,
407) -> Result<ResponseContent<GetContractSuccess>, Error<GetContractError>> {
408    let client = &configuration.client;
409
410    // Create the request from a path.
411    // Make sure to url encode any user given text.
412    let uri_str = format!(
413        "{}/my/contracts/{contractId}",
414        configuration.base_path,
415        contractId = crate::apis::urlencode(contract_id)
416    );
417    let mut req_builder = client.request(reqwest::Method::GET, &uri_str);
418
419    // === Add headers to request ===
420
421    // Set the user agent string if given.
422    if let Some(user_agent) = &configuration.user_agent {
423        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
424    }
425
426    // === Add auth to request ===
427
428    if let Some(token) = &configuration.bearer_access_token {
429        req_builder = req_builder.bearer_auth(token.to_owned());
430    };
431
432    // === Request is built.
433
434    // Execute the request.
435    let req = req_builder.build()?;
436    let resp = client.execute(req).await?;
437
438    // Get the response.
439    let status = resp.status();
440    let response_body = resp.text().await?;
441
442    if !status.is_client_error() && !status.is_server_error() {
443        // Try to parse the OK response.
444        match GetContractSuccess::from_body(status, &response_body) {
445            Some(Ok(content)) => Ok(ResponseContent {
446                status,
447                response_body,
448                content,
449            }),
450            Some(Err(err)) => Err(err.into()),
451            None => Err(Error::UnknownResponse {
452                status,
453                is_error: false,
454                response_body,
455            }),
456        }
457    } else {
458        // Try to parse the Err response.
459        match GetContractError::from_body(status, &response_body) {
460            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
461                status,
462                response_body,
463                content,
464            })),
465            Some(Err(err)) => Err(err.into()),
466            None => Err(Error::UnknownResponse {
467                status,
468                is_error: true,
469                response_body,
470            }),
471        }
472    }
473}
474
475/// Return a paginated list of all your contracts.
476pub async fn get_contracts(
477    configuration: &configuration::Configuration,
478    page: Option<u32>,
479    limit: Option<u32>,
480) -> Result<ResponseContent<GetContractsSuccess>, Error<GetContractsError>> {
481    let client = &configuration.client;
482
483    // Create the request from a path.
484    // Make sure to url encode any user given text.
485    let uri_str = format!("{}/my/contracts", configuration.base_path);
486    let mut req_builder = client.request(reqwest::Method::GET, &uri_str);
487
488    // === Add queries to request ===
489
490    if let Some(s) = &page {
491        req_builder = req_builder.query(&[("page", &s.to_string())]);
492    }
493    // === Add queries to request ===
494
495    if let Some(s) = &limit {
496        req_builder = req_builder.query(&[("limit", &s.to_string())]);
497    }
498
499    // === Add headers to request ===
500
501    // Set the user agent string if given.
502    if let Some(user_agent) = &configuration.user_agent {
503        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
504    }
505
506    // === Add auth to request ===
507
508    if let Some(token) = &configuration.bearer_access_token {
509        req_builder = req_builder.bearer_auth(token.to_owned());
510    };
511
512    // === Request is built.
513
514    // Execute the request.
515    let req = req_builder.build()?;
516    let resp = client.execute(req).await?;
517
518    // Get the response.
519    let status = resp.status();
520    let response_body = resp.text().await?;
521
522    if !status.is_client_error() && !status.is_server_error() {
523        // Try to parse the OK response.
524        match GetContractsSuccess::from_body(status, &response_body) {
525            Some(Ok(content)) => Ok(ResponseContent {
526                status,
527                response_body,
528                content,
529            }),
530            Some(Err(err)) => Err(err.into()),
531            None => Err(Error::UnknownResponse {
532                status,
533                is_error: false,
534                response_body,
535            }),
536        }
537    } else {
538        // Try to parse the Err response.
539        match GetContractsError::from_body(status, &response_body) {
540            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
541                status,
542                response_body,
543                content,
544            })),
545            Some(Err(err)) => Err(err.into()),
546            None => Err(Error::UnknownResponse {
547                status,
548                is_error: true,
549                response_body,
550            }),
551        }
552    }
553}