Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Sandbox #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub mod tools;
use crate::api::tools::*;

pub(crate) const CMC_API_URL: &str = "https://pro-api.coinmarketcap.com/";
pub(crate) const CMC_SANDBOX_API_URL: &str = "https://sandbox-api.coinmarketcap.com/";
pub(crate) const CMC_SANDBOX_API_KEY: &str = "b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c";
pub type CmcResult<T> = Result<T, CmcErrors>;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -197,6 +199,13 @@ impl Cmc {
CmcBuilder::new(api_key).build()
}

/// Constructs a new sandbox CoinMarketCap Client.
pub fn sandbox() -> Self {
CmcBuilder::new(CMC_SANDBOX_API_KEY)
.base_url(CMC_SANDBOX_API_URL)
.build()
}

fn add_endpoint(&self, endpoint: &str) -> RequestBuilder {
self.client
.get(format!("{}{}", self.config.base_url, endpoint))
Expand Down Expand Up @@ -618,6 +627,7 @@ impl Cmc {
let price = root.data[0]
.quote
.get(&convert.to_uppercase())
.or_else(|| root.data[0].quote.get(&convert.to_lowercase()))
.unwrap()
.price;
if let Some(price) = price {
Expand Down
18 changes: 13 additions & 5 deletions src/api/cryptocurrency/quotes_latest_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,24 @@ pub struct CryptoCurrency {
pub is_active: i64,
pub platform: Value,
pub cmc_rank: Value,
pub is_fiat: i64,
pub is_fiat: Option<i64>,
pub self_reported_circulating_supply: Value,
pub self_reported_market_cap: Value,
#[serde(default)]
pub tvl_ratio: Value,
pub last_updated: String,
pub quote: HashMap<String, Currency>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tag {
pub slug: String,
pub name: String,
pub category: String,
#[serde(untagged)]
pub enum Tag {
Simple(String),
Explicit {
slug: String,
name: String,
category: String,
},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -69,11 +74,14 @@ pub struct Currency {
pub percent_change_24h: Value,
pub percent_change_7d: Value,
pub percent_change_30d: Value,
#[serde(default)]
pub percent_change_60d: Value,
#[serde(default)]
pub percent_change_90d: Value,
pub market_cap: Value,
pub market_cap_dominance: Value,
pub fully_diluted_market_cap: Value,
#[serde(default)]
pub tvl: Value,
pub last_updated: String,
}
10 changes: 5 additions & 5 deletions src/api/key/key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ pub struct Usage {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CurrentMinute {
pub requests_made: i64,
pub requests_left: i64,
pub requests_left: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CurrentDay {
pub credits_used: i64,
pub credits_used: Option<i64>,
pub credits_left: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CurrentMonth {
pub credits_used: i64,
pub credits_left: i64,
pub credits_used: Option<i64>,
pub credits_left: Option<i64>,
}

impl Display for KeyInfo {
Expand All @@ -66,7 +66,7 @@ impl Display for KeyInfo {
self.plan.credit_limit_monthly,
self.plan.credit_limit_monthly_reset,
self.plan.rate_limit_minute,
self.usage.current_month.credits_left
self.usage.current_month.credits_left.unwrap_or_default()
)
}
}
23 changes: 21 additions & 2 deletions src/api/tools/price_conversion_v2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::ops::Index;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PCv2Symbol {
pub status: Status,
pub data: Vec<ConversionResult>,
pub data: SomeConversionResult,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SomeConversionResult {
Map(HashMap<String, ConversionResult>),
Array(Vec<ConversionResult>),
}

impl Index<usize> for SomeConversionResult {
type Output = ConversionResult;

fn index(&self, index: usize) -> &Self::Output {
match self {
SomeConversionResult::Map(hash_map) => hash_map.iter().nth(index).unwrap().1,
SomeConversionResult::Array(vec) => vec.get(index).unwrap(),
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -26,7 +45,7 @@ pub struct Status {

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConversionResult {
pub id: i64,
pub id: Value,
pub symbol: String,
pub name: String,
pub amount: f64,
Expand Down
7 changes: 7 additions & 0 deletions src/async_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ impl Cmc {
CmcBuilder::new(api_key).build()
}

/// Constructs a new sandbox CoinMarketCap async Client.
pub fn sandbox() -> Self {
CmcBuilder::new(crate::api::CMC_SANDBOX_API_KEY)
.base_url(crate::api::CMC_SANDBOX_API_URL)
.build()
}

fn add_endpoint(&self, endpoint: &str) -> RequestBuilder {
self.client
.get(format!("{}{}", self.config.base_url, endpoint))
Expand Down
Loading