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

Forex + Trend #1

Merged
merged 5 commits into from
Sep 1, 2024
Merged
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
12 changes: 12 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ cargo run --release --example "candle"
```bash
cargo run --release --example "fundingrate"
```

```bash
cargo run --release --example "forex"
```

```bash
cargo run --release --example "naver"
```

```bash
cargo run --release --example "google"
```
15 changes: 15 additions & 0 deletions examples/forex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use datamaxi::{api::Datamaxi, forex::*};

fn main() {
let api_key: &str = "API_KEY";
let forex: Forex = Datamaxi::new(api_key.to_string());

match forex.symbols() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
match forex.get() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
}
15 changes: 15 additions & 0 deletions examples/google.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use datamaxi::{api::Datamaxi, google::*};

fn main() {
let api_key: &str = "API_KEY";
let google: GoogleTrend = Datamaxi::new(api_key.to_string());

match google.keywords() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
match google.get("Bitcoin") {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
}
15 changes: 15 additions & 0 deletions examples/naver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use datamaxi::{api::Datamaxi, naver::*};

fn main() {
let api_key: &str = "API_KEY";
let naver: NaverTrend = Datamaxi::new(api_key.to_string());

match naver.symbols() {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
match naver.get("AAVE") {
Ok(answer) => println!("{:?}", answer),
Err(e) => println!("Error: {}", e),
}
}
58 changes: 58 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use crate::candle::Candle;
use crate::client::Client;
use crate::forex::Forex;
use crate::fundingrate::FundingRate;
use crate::google::GoogleTrend;
use crate::naver::NaverTrend;

pub enum API {
CandleApi(CandleApi),
FundingRateApi(FundingRateApi),
ForexApi(ForexApi),
NaverTrendApi(NaverTrendApi),
GoogleTrendApi(GoogleTrendApi),
}

pub enum CandleApi {
Expand All @@ -20,6 +27,21 @@ pub enum FundingRateApi {
LatestFundingRate,
}

pub enum ForexApi {
Symbols,
Forex,
}

pub enum NaverTrendApi {
Symbols,
Trend,
}

pub enum GoogleTrendApi {
Keywords,
Trend,
}

impl From<API> for String {
fn from(item: API) -> Self {
String::from(match item {
Expand All @@ -35,6 +57,18 @@ impl From<API> for String {
FundingRateApi::HistoricalFundingRate => "/funding-rate",
FundingRateApi::LatestFundingRate => "/funding-rate/latest",
},
API::ForexApi(route) => match route {
ForexApi::Symbols => "/forex/symbols",
ForexApi::Forex => "/forex",
},
API::NaverTrendApi(route) => match route {
NaverTrendApi::Symbols => "/naver/symbols",
NaverTrendApi::Trend => "/naver/trend",
},
API::GoogleTrendApi(route) => match route {
GoogleTrendApi::Keywords => "/google/keywords",
GoogleTrendApi::Trend => "/google/trend",
},
})
}
}
Expand All @@ -58,3 +92,27 @@ impl Datamaxi for FundingRate {
}
}
}

impl Datamaxi for Forex {
fn new(api_key: String) -> Forex {
Forex {
client: Client::new(api_key),
}
}
}

impl Datamaxi for NaverTrend {
fn new(api_key: String) -> NaverTrend {
NaverTrend {
client: Client::new(api_key),
}
}
}

impl Datamaxi for GoogleTrend {
fn new(api_key: String) -> GoogleTrend {
GoogleTrend {
client: Client::new(api_key),
}
}
}
23 changes: 23 additions & 0 deletions src/forex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::api::ForexApi;
use crate::api::API;
use crate::client::Client;
use crate::error::Result;
use crate::models::ForexDetail;

pub struct Forex {
pub client: Client,
}

impl Forex {
pub fn symbols(&self) -> Result<Vec<String>> {
let symbols: Vec<String> = self.client.get(API::ForexApi(ForexApi::Symbols), None)?;

Ok(symbols)
}

pub fn get(&self) -> Result<Vec<ForexDetail>> {
let forex_data: Vec<ForexDetail> = self.client.get(API::ForexApi(ForexApi::Forex), None)?;

Ok(forex_data)
}
}
34 changes: 34 additions & 0 deletions src/google.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::api::GoogleTrendApi;
use crate::api::API;
use crate::client::Client;
use crate::error::Result;
use crate::utils::build_request;
use std::collections::BTreeMap;

pub struct GoogleTrend {
pub client: Client,
}

impl GoogleTrend {
pub fn keywords(&self) -> Result<Vec<String>> {
let keywords: Vec<String> = self
.client
.get(API::GoogleTrendApi(GoogleTrendApi::Keywords), None)?;

Ok(keywords)
}

pub fn get<K>(&self, keyword: K) -> Result<Vec<Vec<String>>>
where
K: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("keyword".into(), keyword.into());
let request = build_request(parameters);
let forex_data: Vec<Vec<String>> = self
.client
.get(API::GoogleTrendApi(GoogleTrendApi::Trend), Some(request))?;

Ok(forex_data)
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ pub mod client;
pub mod api;
pub mod candle;
pub mod error;
pub mod forex;
pub mod fundingrate;
pub mod google;
pub mod models;
pub mod naver;
pub mod utils;
7 changes: 7 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ pub struct LatestFundingRateDetail {
pub q: String,
pub r: f64,
}

#[derive(Deserialize, Debug)]
pub struct ForexDetail {
pub s: String,
pub d: i64,
pub r: f64,
}
34 changes: 34 additions & 0 deletions src/naver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::api::NaverTrendApi;
use crate::api::API;
use crate::client::Client;
use crate::error::Result;
use crate::utils::build_request;
use std::collections::BTreeMap;

pub struct NaverTrend {
pub client: Client,
}

impl NaverTrend {
pub fn symbols(&self) -> Result<Vec<String>> {
let symbols: Vec<String> = self
.client
.get(API::NaverTrendApi(NaverTrendApi::Symbols), None)?;

Ok(symbols)
}

pub fn get<S>(&self, symbol: S) -> Result<Vec<Vec<String>>>
where
S: Into<String>,
{
let mut parameters: BTreeMap<String, String> = BTreeMap::new();
parameters.insert("symbol".into(), symbol.into());
let request = build_request(parameters);
let forex_data: Vec<Vec<String>> = self
.client
.get(API::NaverTrendApi(NaverTrendApi::Trend), Some(request))?;

Ok(forex_data)
}
}
Loading