coinpaprika_api/contracts/
mod.rs

1use crate::client::{Client, Response};
2use crate::error::Error;
3use reqwest_middleware::RequestBuilder;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize)]
7/// Contract information
8pub struct Contract {
9    pub address: String,
10    pub id: String,
11
12    #[serde(rename = "type")]
13    pub contract_type: String,
14}
15
16/// Request for getting all available contract platforms on coinpaprika.com
17/// [/contracts](https://api.coinpaprika.com/#tag/Contracts/operation/getPlatforms)
18pub struct GetContractPlatformsRequest<'a> {
19    client: &'a Client,
20}
21
22impl<'a> GetContractPlatformsRequest<'a> {
23    pub fn new(client: &'a Client) -> Self {
24        Self { client }
25    }
26
27    pub async fn send(&self) -> Result<Vec<String>, Error> {
28        let request: RequestBuilder = self
29            .client
30            .client
31            .get(format!("{}/contracts", self.client.api_url));
32
33        let response: Response = self.client.request(request).await?;
34
35        let data: Vec<String> = response.response.json().await?;
36
37        Ok(data)
38    }
39}
40
41/// Request for getting all available contracts for a given platform on coinpaprika.com
42/// [/contracts/{platform_id}](https://api.coinpaprika.com/#tag/Contracts/operation/getContracts)
43pub struct GetContractsRequest<'a> {
44    client: &'a Client,
45    platform_id: String,
46}
47
48impl<'a> GetContractsRequest<'a> {
49    pub fn new(client: &'a Client, platform_id: &str) -> Self {
50        Self {
51            client,
52            platform_id: String::from(platform_id),
53        }
54    }
55
56    pub async fn send(&self) -> Result<Vec<Contract>, Error> {
57        let request: RequestBuilder = self.client.client.get(format!(
58            "{}/contracts/{}",
59            self.client.api_url, self.platform_id
60        ));
61
62        let response: Response = self.client.request(request).await?;
63
64        let data: Vec<Contract> = response.response.json().await?;
65
66        Ok(data)
67    }
68}