Skip to content

Commit

Permalink
Merge pull request #171 from bitbytelabio/develop
Browse files Browse the repository at this point in the history
Repair for Release 0.0.1
  • Loading branch information
nguyenthdat authored Jan 11, 2024
2 parents 4b8bdaf + e520c3a commit 93aa741
Show file tree
Hide file tree
Showing 23 changed files with 653 additions and 422 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish to crates.io

on:
push:
tags:
- "*"

jobs:
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: login
args: ${{ secrets.CRATES_IO_TOKEN }}
- uses: actions-rs/cargo@v1
with:
command: publish
25 changes: 12 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,36 @@ path = "src/lib.rs"
[features]
default = ["user"]
user = ["dep:google-authenticator"]
protobuf = ["dep:prost"]

[dependencies]
async-trait = "0.1" # Remove this when Rust 1.75 async trait is stable
tokio = { version = "1", default-features = false }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1" }
reqwest = { version = "0.11", features = ["rustls-tls", "json", "cookies"] }
lazy_static = "^1.4"
url = "^2"
urlencoding = "^2"
lazy_static = "1.4"
url = "2"
urlencoding = "2"
rand = "^0.8"
regex = "^1"
tokio-tungstenite = { version = "^0.21", features = [
"rustls-tls-webpki-roots",
] }
regex = "1"
tokio-tungstenite = { version = "0.21", features = ["rustls-tls-webpki-roots"] }
tracing = "0.1"
thiserror = "^1.0"
thiserror = "1"
futures-util = { version = "0.3", default-features = false, features = [
"sink",
"std",
] }
async-trait = "^0.1"
iso_currency = { version = "0.4", features = ["with-serde"] }
zip = "^0.6"
base64 = "^0.21"
google-authenticator = { version = "^0.4", optional = true }
zip = "0.6"
base64 = "0.21"
google-authenticator = { version = "0.4", optional = true }
prost = { version = "0.12", optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
criterion = { version = "0.5", features = ["html_reports"] }
anyhow = "1"
protobuf = "3"
dotenv = "0.15.0"
tracing-subscriber = "0.3"
chrono = { version = "0.4.31", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ example bin:
cargo run --package tradingview-rs --example {{bin}}

lines-of-code:
@git ls-files | grep '\.rs' | xargs wc -l
@cloc $(git ls-files)

creds-scan:
@ggshield secret scan repo ./
Expand Down
19 changes: 9 additions & 10 deletions examples/all.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use dotenv::dotenv;
use std::env;

use tracing::info;
use tradingview::{
chart,
chart::ChartOptions,
feeder::Feeder,
data_loader::DataLoader,
models::{pine_indicator::ScriptType, Interval},
quote,
socket::{DataServer, SocketSession},
Expand All @@ -19,19 +18,19 @@ async fn main() -> anyhow::Result<()> {

let socket = SocketSession::new(DataServer::ProData, auth_token).await?;

let publisher = Feeder::new();
let publisher: DataLoader = DataLoader::default();

let mut chart = chart::session::WebSocket::new(publisher.clone(), socket.clone());
let mut quote = quote::session::WebSocket::new(publisher.clone(), socket.clone());

// subscriber.subscribe(&mut chart, &mut socket);

let opts = ChartOptions::new("BINANCE:BTCUSDT", Interval::OneMinute).bar_count(100);
let opts2 = ChartOptions::new("BINANCE:BTCUSDT", Interval::Daily).study_config(
"STD;Candlestick%1Pattern%1Bearish%1Abandoned%1Baby",
"33.0",
ScriptType::IntervalScript,
);
let opts2 = ChartOptions::new("BINANCE:BTCUSDT", Interval::Daily)
.bar_count(1)
.study_config(
"STD;Candlestick%1Pattern%1Bearish%1Abandoned%1Baby",
"33.0",
ScriptType::IntervalScript,
);
let opts3 = ChartOptions::new("BINANCE:BTCUSDT", Interval::OneHour)
.replay_mode(true)
.replay_from(1698624060);
Expand Down
3 changes: 2 additions & 1 deletion examples/user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![cfg(feature = "user")]
use tradingview::user::UserCookies;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let user = UserCookies::new()
let user = UserCookies::default()
.login("testuser", "testpassword", None)
.await?;

Expand Down
2 changes: 0 additions & 2 deletions sonar-project.properties

This file was deleted.

116 changes: 116 additions & 0 deletions src/callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::{
chart::{
models::{DataPoint, StudyResponseData, SymbolInfo},
ChartOptions, StudyOptions,
},
quote::models::QuoteValue,
socket::TradingViewDataEvent,
Error,
};
use futures_util::{future::BoxFuture, Future};
use serde_json::Value;
use std::sync::Arc;
use tracing::{error, info};

pub type AsyncCallback<'a, T> = Box<dyn (Fn(T) -> BoxFuture<'a, ()>) + Send + Sync + 'a>;

#[derive(Clone)]
pub struct Callbacks<'a> {
pub(crate) on_chart_data: Arc<AsyncCallback<'a, (ChartOptions, Vec<DataPoint>)>>,
pub(crate) on_quote_data: Arc<AsyncCallback<'a, QuoteValue>>,
pub(crate) on_study_data: Arc<AsyncCallback<'a, (StudyOptions, StudyResponseData)>>,
pub(crate) on_error: Arc<AsyncCallback<'a, Error>>,
pub(crate) on_symbol_info: Arc<AsyncCallback<'a, SymbolInfo>>,
pub(crate) on_other_event: Arc<AsyncCallback<'a, (TradingViewDataEvent, Vec<Value>)>>,
}

impl Default for Callbacks<'_> {
fn default() -> Self {
Self {
on_chart_data: Arc::new(Box::new(|data| {
Box::pin(async move { info!("default callback logging && handling: {:?}", data) })
})),
on_quote_data: Arc::new(Box::new(|data| {
Box::pin(async move { info!("default callback logging && handling: {:?}", data) })
})),
on_study_data: Arc::new(Box::new(|data| {
Box::pin(async move { info!("default callback logging && handling: {:?}", data) })
})),

on_error: Arc::new(Box::new(|e| {
Box::pin(async move { error!("default callback logging && handling: {e}") })
})),
on_symbol_info: Arc::new(Box::new(|data| {
Box::pin(async move { info!("default callback logging && handling: {:?}", data) })
})),
on_other_event: Arc::new(Box::new(|data| {
Box::pin(async move { info!("default callback logging && handling: {:?}", data) })
})),
}
}
}

impl<'a> Callbacks<'a> {
pub fn on_chart_data<Fut>(
&mut self,
f: impl Fn((ChartOptions, Vec<DataPoint>)) -> Fut + Send + Sync + 'a,
) -> &mut Self
where
Fut: Future<Output = ()> + Send + 'a,
{
self.on_chart_data = Arc::new(Box::new(move |data| Box::pin(f(data))));
self
}

pub fn on_quote_data<Fut>(
&mut self,
f: impl Fn(QuoteValue) -> Fut + Send + Sync + 'a,
) -> &mut Self
where
Fut: Future<Output = ()> + Send + 'a,
{
self.on_quote_data = Arc::new(Box::new(move |data| Box::pin(f(data))));
self
}

pub fn on_study_data<Fut>(
&mut self,
f: impl Fn((StudyOptions, StudyResponseData)) -> Fut + Send + Sync + 'a,
) -> &mut Self
where
Fut: Future<Output = ()> + Send + 'a,
{
self.on_study_data = Arc::new(Box::new(move |data| Box::pin(f(data))));
self
}

pub fn on_error<Fut>(&mut self, f: impl Fn(Error) -> Fut + Send + Sync + 'a) -> &mut Self
where
Fut: Future<Output = ()> + Send + 'a,
{
self.on_error = Arc::new(Box::new(move |data| Box::pin(f(data))));
self
}

pub fn on_symbol_info<Fut>(
&mut self,
f: impl Fn(SymbolInfo) -> Fut + Send + Sync + 'a,
) -> &mut Self
where
Fut: Future<Output = ()> + Send + 'a,
{
self.on_symbol_info = Arc::new(Box::new(move |data| Box::pin(f(data))));
self
}

pub fn on_other_event<Fut>(
&mut self,
f: impl Fn((TradingViewDataEvent, Vec<Value>)) -> Fut + Send + Sync + 'a,
) -> &mut Self
where
Fut: Future<Output = ()> + Send + 'a,
{
self.on_other_event = Arc::new(Box::new(move |data| Box::pin(f(data))));
self
}
}
8 changes: 4 additions & 4 deletions src/chart/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use iso_currency::Currency;

use crate::models::{ MarketAdjustment, SessionType, pine_indicator::ScriptType, Interval };
use crate::models::{pine_indicator::ScriptType, Interval, MarketAdjustment, SessionType};

pub mod session;
pub mod study;
pub mod models;
pub(crate) mod options;
pub mod session;
pub mod study;
pub(crate) mod utils;

#[derive(Default, Debug, Clone)]
Expand All @@ -24,7 +24,7 @@ pub struct ChartOptions {
adjustment: Option<MarketAdjustment>,
currency: Option<Currency>,
session_type: Option<SessionType>,
study_config: Option<StudyOptions>,
pub study_config: Option<StudyOptions>,
}

#[derive(Default, Debug, Clone)]
Expand Down
Loading

0 comments on commit 93aa741

Please sign in to comment.