coinpaprika_api/tools/
mod.rs

1use crate::client::{Client, Response};
2use crate::error::Error;
3use reqwest_middleware::RequestBuilder;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7#[derive(Debug, Serialize, Deserialize)]
8/// Price data of a single cryptocurrency on coinpaprika.com
9pub struct PriceConversion {
10    pub base_currency_id: String,
11    pub base_currency_name: String,
12    pub base_price_last_updated: String,
13    pub quote_currency_id: String,
14    pub quote_currency_name: String,
15    pub quote_price_last_updated: String,
16    pub amount: i32,
17    pub price: f64,
18}
19
20/// Request for getting currencies, exchanges, icos, people, tags on coinpaprika.com for a given
21/// search query
22/// [/search](https://api.coinpaprika.com/#tag/Tools/paths/~1search/get)
23pub struct GetSearchRequest<'a> {
24    client: &'a Client,
25    q: String,
26    c: Option<Vec<String>>,
27    modifier: Option<String>,
28    limit: Option<String>,
29}
30
31impl<'a> GetSearchRequest<'a> {
32    pub fn new(client: &'a Client, q: &str) -> Self {
33        Self {
34            client,
35            q: String::from(q),
36            c: None,
37            modifier: None,
38            limit: None,
39        }
40    }
41
42    /// One or more categories to search. Available options: `"currencies"`, `"exchanges"`,
43    /// `"icos"`, `"people"`, `"tags"`
44    ///
45    /// Default: `["currencies", "exchanges", "icos", "people", "tags"]` (all categories are
46    /// returned)
47    pub fn c(&mut self, categories: Vec<&str>) -> &'a mut GetSearchRequest {
48        self.c = Some(categories.iter().map(|&q| String::from(q)).collect());
49        self
50    }
51
52    /// Set modifier for search results. Available options: `symbol_search` - search only by symbol
53    /// (works for currencies only)
54    pub fn modifier(&mut self, modifier: &str) -> &'a mut GetSearchRequest {
55        self.modifier = Some(String::from(modifier));
56        self
57    }
58
59    /// Limit of results per category (max `250`)
60    ///
61    /// Default: `6`
62    pub fn limit(&mut self, limit: i32) -> &'a mut GetSearchRequest {
63        self.limit = Some(limit.to_string());
64        self
65    }
66
67    pub async fn send(&self) -> Result<Value, Error> {
68        let mut query: Vec<(String, String)> = vec![("q".to_string(), self.q.to_string())];
69
70        if let Some(c) = &self.c {
71            query.push(("c".to_string(), c.join(",")));
72        }
73
74        if let Some(modifier) = &self.modifier {
75            query.push(("modifier".to_string(), modifier.to_string()));
76        }
77
78        if let Some(limit) = &self.limit {
79            query.push(("limit".to_string(), limit.to_string()));
80        }
81
82        let request: RequestBuilder = self
83            .client
84            .client
85            .get(format!("{}/search", self.client.api_url))
86            .query(&query);
87
88        let response: Response = self.client.request(request).await?;
89
90        let data: Value = response.response.json().await?;
91
92        Ok(data)
93    }
94}
95
96/// Request for converting a set amount of base currency to quote currency
97/// [/price-converter](https://api.coinpaprika.com/#tag/Tools/paths/~1price-converter/get)
98pub struct GetPriceConversionRequest<'a> {
99    client: &'a Client,
100    base_currency_id: String,
101    quote_currency_id: String,
102    amount: String,
103}
104
105impl<'a> GetPriceConversionRequest<'a> {
106    pub fn new(client: &'a Client, base_currency_id: &str, quote_currency_id: &str) -> Self {
107        Self {
108            client,
109            base_currency_id: String::from(base_currency_id),
110            quote_currency_id: String::from(quote_currency_id),
111            amount: String::from("0"),
112        }
113    }
114
115    /// Default: 0
116    pub fn amount(&mut self, amount: i32) -> &'a mut GetPriceConversionRequest {
117        self.amount = amount.to_string();
118        self
119    }
120
121    pub async fn send(&self) -> Result<PriceConversion, Error> {
122        let query: Vec<(&str, &str)> = vec![
123            ("base_currency_id", self.base_currency_id.as_ref()),
124            ("quote_currency_id", self.quote_currency_id.as_ref()),
125            ("amount", self.amount.as_ref()),
126        ];
127
128        let request: RequestBuilder = self
129            .client
130            .client
131            .get(format!("{}/price-converter", self.client.api_url))
132            .query(&query);
133
134        let response: Response = self.client.request(request).await?;
135
136        let data: PriceConversion = response.response.json().await?;
137
138        Ok(data)
139    }
140}