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

Reflect Data API changes (v0.3.0) #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion examples/cex-candle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {

// CEX Candle Data
let candle_options = datamaxi::cex::CandleOptions::new();
let candle_response = candle.get("binance", "ETH-USDT", candle_options);
let candle_response = candle.get("binance", "spot", "ETH-USDT", candle_options);
match candle_response {
Ok(answer) => match serde_json::to_string(&answer) {
Ok(json) => println!("{}", json),
Expand Down
46 changes: 23 additions & 23 deletions examples/dex-candle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ fn main() {
let candle: datamaxi::dex::Candle = datamaxi::api::Datamaxi::new(api_key);

// DEX Candle Intervals
match candle.intervals() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
// match candle.intervals() {
// Ok(answer) => println!("{:?}", answer),
// Err(e) => println!("Error: {}", e),
// }

// DEX Candle Exchanges
match candle.exchanges() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
// // DEX Candle Exchanges
// match candle.exchanges() {
// Ok(answer) => println!("{:?}", answer),
// Err(e) => println!("Error: {}", e),
// }

// DEX Candle Chains
match candle.chains() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
// // DEX Candle Chains
// match candle.chains() {
// Ok(answer) => println!("{:?}", answer),
// Err(e) => println!("Error: {}", e),
// }

// DEX Candle Pools
let pools_options = datamaxi::dex::PoolsOptions::new();
let pools_response = candle.pools(pools_options);
match pools_response {
Ok(answer) => match serde_json::to_string(&answer) {
Ok(json) => println!("{}", json),
Err(e) => println!("Error: {}", e),
},
Err(e) => println!("Error: {}", e),
}
// let pools_options = datamaxi::dex::PoolsOptions::new();
// let pools_response = candle.pools(pools_options);
// match pools_response {
// Ok(answer) => match serde_json::to_string(&answer) {
// Ok(json) => println!("{}", json),
// Err(e) => println!("Error: {}", e),
// },
// Err(e) => println!("Error: {}", e),
// }

// DEX Candle Data
let params = datamaxi::dex::CandleOptions::new();
Expand Down
36 changes: 18 additions & 18 deletions examples/dex-trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ fn main() {
let trade: datamaxi::dex::Trade = datamaxi::api::Datamaxi::new(api_key);

// DEX Trade Exchanges
match trade.exchanges() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
// match trade.exchanges() {
// Ok(answer) => println!("{:?}", answer),
// Err(e) => println!("Error: {}", e),
// }

// DEX Trade Chains
match trade.chains() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
// // DEX Trade Chains
// match trade.chains() {
// Ok(answer) => println!("{:?}", answer),
// Err(e) => println!("Error: {}", e),
// }

// DEX Trade Pools
let pools_options = datamaxi::dex::PoolsOptions::new();
let pools_response = trade.pools(pools_options);
match pools_response {
Ok(answer) => match serde_json::to_string(&answer) {
Ok(json) => println!("{}", json),
Err(e) => println!("Error: {}", e),
},
Err(e) => println!("Error: {}", e),
}
// let pools_options = datamaxi::dex::PoolsOptions::new();
// let pools_response = trade.pools(pools_options);
// match pools_response {
// Ok(answer) => match serde_json::to_string(&answer) {
// Ok(json) => println!("{}", json),
// Err(e) => println!("Error: {}", e),
// },
// Err(e) => println!("Error: {}", e),
// }

// DEX Trade Data
let trade_options = datamaxi::dex::TradeOptions::new().limit(5);
Expand Down
17 changes: 4 additions & 13 deletions src/cex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,35 @@ impl Candle {
/// Retrieves candle data for a specified exchange and symbol. Additional parameters can be
/// provided to filter and sort the results. The response will contain an array of candle data
/// objects, each representing a single candle with open, high, low, close, and volume values.
pub fn get<E, S>(
pub fn get<E, M, S>(
&self,
exchange: E,
market: M,
symbol: S,
options: CandleOptions,
) -> Result<CandleResponse>
where
E: Into<String>,
M: Into<String>,
S: Into<String>,
{
let mut parameters = HashMap::new();

// required
parameters.insert("exchange".to_string(), exchange.into());
parameters.insert("market".to_string(), market.into());
parameters.insert("symbol".to_string(), symbol.into());

// optional
parameters.extend(
[
options
.market
.map(|market| ("market".to_string(), market.to_string())),
options
.interval
.map(|interval| ("interval".to_string(), interval.to_string())),
options
.page
.map(|page| ("page".to_string(), page.to_string())),
options
.limit
.map(|limit| ("limit".to_string(), limit.to_string())),
options
.from
.map(|from| ("from".to_string(), from.to_string())),
options.to.map(|to| ("to".to_string(), to.to_string())),
options
.sort
.map(|sort| ("sort".to_string(), sort.to_string())),
]
.into_iter()
.flatten(),
Expand Down
158 changes: 80 additions & 78 deletions src/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,70 +35,20 @@ impl Candle {
// optional
parameters.extend(
[
options
.market
.map(|market| ("market".to_string(), market.to_string())),
options
.interval
.map(|interval| ("interval".to_string(), interval.to_string())),
options
.page
.map(|page| ("page".to_string(), page.to_string())),
options
.limit
.map(|limit| ("limit".to_string(), limit.to_string())),
options
.from
.map(|from| ("from".to_string(), from.to_string())),
options.to.map(|to| ("to".to_string(), to.to_string())),
options
.sort
.map(|sort| ("sort".to_string(), sort.to_string())),
]
.into_iter()
.flatten(),
);

self.client.get("/dex/candle", Some(parameters))
}

/// Retrieves information about available pools, including details about the chain, exchange,
/// base and quote symbols, and pool address. Optional parameters can be provided to filter the
/// results by chain and exchange.
pub fn pools(&self, options: PoolsOptions) -> Result<Vec<PoolsResponse>> {
let mut parameters = HashMap::new();

// optional
parameters.extend(
[
options
.exchange
.map(|exchange| ("exchange".to_string(), exchange.to_string())),
options
.chain
.map(|chain| ("chain".to_string(), chain.to_string())),
]
.into_iter()
.flatten(),
);

self.client.get("/dex/candle/pools", Some(parameters))
}

/// Retrieves a list of available chains for candle data.
pub fn chains(&self) -> Result<Vec<String>> {
self.client.get("/dex/candle/chains", None)
}

/// Retrieves a list of available exchanges for candle data.
pub fn exchanges(&self) -> Result<Vec<String>> {
self.client.get("/dex/candle/exchanges", None)
}

/// Retrieves a list of available intervals for candle data.
pub fn intervals(&self) -> Result<Vec<String>> {
self.client.get("/dex/candle/intervals", None)
}
}

/// Implements the `Datamaxi` trait for `Candle`, providing methods
Expand Down Expand Up @@ -204,87 +154,139 @@ impl Trade {

self.client.get("/dex/trade", Some(parameters))
}
}

/// Implements the `Datamaxi` trait for `Trade`, providing methods
/// to create new instances of `Trade` with or without a custom base URL.
impl Datamaxi for Trade {
/// Creates a new `Trade` instance with the default base URL.
///
/// # Parameters
/// - `api_key`: A `String` containing the API key to authenticate requests.
///
/// # Returns
/// A new `Trade` instance configured with the default base URL and the provided `api_key`.
///
/// # Example
/// ```rust
/// use crate::datamaxi::api::Datamaxi;
/// let trade = datamaxi::dex::Trade::new("my_api_key".to_string());
/// ```
fn new(api_key: String) -> Trade {
let config = Config {
base_url: None, // Use the default base URL
api_key, // Provided API key
};
Trade {
client: Client::new(config), // Create a new client with the given config
}
}

/// Creates a new `Trade` instance with a custom base URL.
///
/// # Parameters
/// - `api_key`: A `String` containing the API key to authenticate requests.
/// - `base_url`: A `String` specifying the custom base URL for API requests.
///
/// # Returns
/// A new `Trade` instance configured with the provided `base_url` and `api_key`.
///
/// # Example
/// ```rust
/// use crate::datamaxi::api::Datamaxi;
/// let trade = datamaxi::dex::Trade::new_with_base_url("my_api_key".to_string(), "https://custom-api.example.com".to_string());
/// ```
fn new_with_base_url(api_key: String, base_url: String) -> Trade {
let config = Config {
base_url: Some(base_url), // Use the provided custom base URL
api_key, // Provided API key
};
Trade {
client: Client::new(config), // Create a new client with the given config
}
}
}

#[derive(Clone)]
pub struct Pool {
pub client: Client,
}

impl Pool {
/// Retrieves information about available pools, including details about the chain, exchange,
/// base and quote symbols, and pool address. Optional parameters can be provided to filter the
/// results by chain and exchange.
pub fn pools(&self, options: PoolsOptions) -> Result<Vec<PoolsResponse>> {
pub fn get(
&self,
options: PoolsOptions,
) -> Result<PoolsResponse>
{
let mut parameters = HashMap::new();

// optional
parameters.extend(
[
options
.chain
.map(|chain| ("chain".to_string(), chain.to_string())),
options
.exchange
.map(|exchange| ("exchange".to_string(), exchange.to_string())),
options
.chain
.map(|chain| ("chain".to_string(), chain.to_string())),
]
.into_iter()
.flatten(),
);

self.client.get("/dex/trade/pools", Some(parameters))
}

/// Retrieves a list of available intervals for trade data.
pub fn chains(&self) -> Result<Vec<String>> {
self.client.get("/dex/trade/chains", None)
}

/// Retrieves a list of available exchanges for trade data.
pub fn exchanges(&self) -> Result<Vec<String>> {
self.client.get("/dex/trade/exchanges", None)
self.client.get("/dex/pools", Some(parameters))
}
}

/// Implements the `Datamaxi` trait for `Trade`, providing methods
/// to create new instances of `Trade` with or without a custom base URL.
impl Datamaxi for Trade {
/// Creates a new `Trade` instance with the default base URL.
/// Implements the `Datamaxi` trait for `Pool`, providing methods
/// to create new instances of `Pool` with or without a custom base URL.
impl Datamaxi for Pool {
/// Creates a new `Pool` instance with the default base URL.
///
/// # Parameters
/// - `api_key`: A `String` containing the API key to authenticate requests.
///
/// # Returns
/// A new `Trade` instance configured with the default base URL and the provided `api_key`.
/// A new `Pool` instance configured with the default base URL and the provided `api_key`.
///
/// # Example
/// ```rust
/// use crate::datamaxi::api::Datamaxi;
/// let trade = datamaxi::dex::Trade::new("my_api_key".to_string());
/// let pool = datamaxi::dex::Pool::new("my_api_key".to_string());
/// ```
fn new(api_key: String) -> Trade {
fn new(api_key: String) -> Pool {
let config = Config {
base_url: None, // Use the default base URL
api_key, // Provided API key
};
Trade {
Pool {
client: Client::new(config), // Create a new client with the given config
}
}

/// Creates a new `Trade` instance with a custom base URL.
/// Creates a new `Pool` instance with a custom base URL.
///
/// # Parameters
/// - `api_key`: A `String` containing the API key to authenticate requests.
/// - `base_url`: A `String` specifying the custom base URL for API requests.
///
/// # Returns
/// A new `Trade` instance configured with the provided `base_url` and `api_key`.
/// A new `Pool` instance configured with the provided `base_url` and `api_key`.
///
/// # Example
/// ```rust
/// use crate::datamaxi::api::Datamaxi;
/// let trade = datamaxi::dex::Trade::new_with_base_url("my_api_key".to_string(), "https://custom-api.example.com".to_string());
/// let pool = datamaxi::dex::Pool::new_with_base_url("my_api_key".to_string(), "https://custom-api.example.com".to_string());
/// ```
fn new_with_base_url(api_key: String, base_url: String) -> Trade {
fn new_with_base_url(api_key: String, base_url: String) -> Pool {
let config = Config {
base_url: Some(base_url), // Use the provided custom base URL
api_key, // Provided API key
};
Trade {
Pool {
client: Client::new(config), // Create a new client with the given config
}
}
Expand Down
Loading
Loading