Skip to content

Commit

Permalink
Merge pull request #8 from roykim98/roy/manual-auth
Browse files Browse the repository at this point in the history
Add a channel messenger trait for custom user implementations for manual auth workflows
  • Loading branch information
z-Wind authored Jan 2, 2025
2 parents 4b23422 + 49fb207 commit a5fdf24
Show file tree
Hide file tree
Showing 14 changed files with 664 additions and 266 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ async fn main() {
let certs_dir = PathBuf::from("your_certs_dir");
let client = Client::new();
let token_checker = TokenChecker::new(path, key, secret, callback_url, certs_dir, client)
.await
.unwrap();
let token_checker = TokenChecker::new_with_local_server(path, key, secret, callback_url, certs_dir, client.clone())
.await
.unwrap();
let api = api::Api::new(token_checker, client).await.unwrap();
Expand Down
38 changes: 28 additions & 10 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use parameter::{Market, Projection, TransactionType};
/// Interacting with the Schwab API.
#[derive(Debug)]
pub struct Api<T: Tokener> {
tokener: T,
pub tokener: T,
client: Client,
}

Expand Down Expand Up @@ -445,16 +445,26 @@ mod tests {
use crate::model::trader::order::ExecutionType;
use crate::model::trader::order_request::InstrumentRequest;
use crate::model::trader::preview_order::Instruction;
use crate::token::channel_messenger::compound_messenger::CompoundMessenger;
use crate::token::channel_messenger::local_server::LocalServerMessenger;
use crate::token::channel_messenger::stdio_messenger::StdioMessenger;
use crate::token::channel_messenger::ChannelMessenger;
use crate::token::TokenChecker;

async fn client() -> Api<TokenChecker> {
async fn client() -> Api<TokenChecker<impl ChannelMessenger>> {
#[allow(clippy::option_env_unwrap)]
let key = option_env!("SCHWAB_API_KEY")
.expect("There should be SCHWAB API KEY")
.expect("The environment variable SCHWAB_API_KEY sholud be set")
.to_string();

#[allow(clippy::option_env_unwrap)]
let secret = option_env!("SCHWAB_SECRET")
.expect("There should be SCHWAB SECRET")
.expect("The environment variable SCHWAB_SECRET sholud be set")
.to_string();

#[allow(clippy::option_env_unwrap)]
let callback_url = option_env!("SCHWAB_CALLBACK_URL")
.expect("The environment variable SCHWAB_CALLBACK_URL sholud be set")
.to_string();

let path = dirs::home_dir()
Expand All @@ -463,14 +473,22 @@ mod tests {
.join("Schwab-rust.json");

let certs_dir = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/certs"));

let callback_url = "https://127.0.0.1:8080".to_string();
let messenger = CompoundMessenger::new(
LocalServerMessenger::new(&certs_dir).await,
StdioMessenger::new(),
);

let client = Client::new();
let token_checker =
TokenChecker::new(path, key, secret, callback_url, certs_dir, client.clone())
.await
.unwrap();
let token_checker = TokenChecker::new_with_custom_auth(
path,
key,
secret,
callback_url,
client.clone(),
messenger,
)
.await
.unwrap();

Api::new(token_checker, client).await.unwrap()
}
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ pub enum Error {
Service(crate::model::ServiceError),
#[error("Json error: {0}")]
Json(#[from] serde_json::Error),
#[error("ChannelMessenger error: {0}")]
ChannelMessenger(String),
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! let certs_dir = PathBuf::from("your_certs_dir");
//!
//! let client = Client::new();
//! let token_checker = TokenChecker::new(path, key, secret, callback_url, certs_dir, client.clone())
//! let token_checker = TokenChecker::new_with_local_server(path, key, secret, callback_url, certs_dir, client.clone())
//! .await
//! .unwrap();
//!
Expand Down
1 change: 1 addition & 0 deletions src/model/market_data/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ mod custom_date_format {

const FORMAT: &str = "%Y-%m-%d %H:%M:%S%.f";

#[allow(clippy::ref_option)]
pub fn serialize<S>(date: &Option<NaiveDateTime>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down
5 changes: 2 additions & 3 deletions src/model/market_data/quote_response/equity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ pub struct QuoteEquity {
///
/// Number of shares for ask
pub ask_size: i64,
#[serde_as(as = "TimestampMilliSeconds<i64>")]

/// example: 1621376892336
///
/// Last ask time in milliseconds since Epoch
#[serde_as(as = "TimestampMilliSeconds<i64>")]
pub ask_time: chrono::DateTime<chrono::Utc>,

/// example: XNYS
Expand All @@ -244,11 +244,11 @@ pub struct QuoteEquity {
///
/// Number of shares for bid
pub bid_size: i64,
#[serde_as(as = "TimestampMilliSeconds<i64>")]

/// example: 1621376892336
///
/// Last bid time in milliseconds since Epoch
#[serde_as(as = "TimestampMilliSeconds<i64>")]
pub bid_time: chrono::DateTime<chrono::Utc>,

/// example: 126.27
Expand Down Expand Up @@ -490,7 +490,6 @@ pub enum DivFrequency {
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum FundStrategy {
/// "A" - Active
#[serde(rename = "A")]
Active,

Expand Down
2 changes: 1 addition & 1 deletion src/model/market_data/quote_response/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ pub struct QuoteFuture {
///
/// Day's high trade price
pub high_price: f64,
#[serde(rename = "lastMICId")]

/// example: XNYS
///
/// Last MIC Code
#[serde(rename = "lastMICId")]
pub last_micid: Option<String>,

/// example: 4083
Expand Down
1 change: 0 additions & 1 deletion src/model/market_data/quote_response/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ pub enum ExerciseType {
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ExpirationType {
/// M for End Of Month Expiration Calendar Cycle. (To match the last business day of the month)
#[serde(rename = "M")]
Month,

Expand Down
Loading

0 comments on commit a5fdf24

Please sign in to comment.