Fix bad request response handling

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-02-17 20:35:50 +00:00
parent ae5044142d
commit 152a0b4682
10 changed files with 46 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
use crate::{
config::{Config, ALPACA_MODE},
database,
types::alpaca::{self, api, shared::Sort},
types::alpaca::{self, shared::Sort},
};
use log::{info, warn};
use std::{collections::HashMap, sync::Arc};
@@ -45,11 +45,11 @@ pub async fn rehydrate_orders(config: &Arc<Config>) {
let mut orders = vec![];
let mut after = OffsetDateTime::UNIX_EPOCH;
while let Some(message) = api::incoming::order::get(
while let Some(message) = alpaca::api::incoming::order::get(
&config.alpaca_client,
&config.alpaca_rate_limiter,
&api::outgoing::order::Order {
status: Some(api::outgoing::order::Status::All),
&alpaca::api::outgoing::order::Order {
status: Some(alpaca::api::outgoing::order::Status::All),
limit: Some(500),
after: Some(after),
until: None,
@@ -70,7 +70,7 @@ pub async fn rehydrate_orders(config: &Arc<Config>) {
let orders = orders
.into_iter()
.flat_map(&api::incoming::order::Order::normalize)
.flat_map(&alpaca::api::incoming::order::Order::normalize)
.collect::<Vec<_>>();
database::orders::upsert_batch(&config.clickhouse_client, &orders)

View File

@@ -7,7 +7,7 @@ use crate::{
database,
types::{
alpaca::{
api,
self,
shared::{Sort, Source},
},
news::Prediction,
@@ -167,7 +167,7 @@ struct BarHandler {
fetch_from: OffsetDateTime,
fetch_to: OffsetDateTime,
next_page_token: Option<String>,
) -> api::outgoing::bar::Bar,
) -> alpaca::api::outgoing::bar::Bar,
}
fn us_equity_query_constructor(
@@ -175,14 +175,14 @@ fn us_equity_query_constructor(
fetch_from: OffsetDateTime,
fetch_to: OffsetDateTime,
next_page_token: Option<String>,
) -> api::outgoing::bar::Bar {
api::outgoing::bar::Bar::UsEquity {
) -> alpaca::api::outgoing::bar::Bar {
alpaca::api::outgoing::bar::Bar::UsEquity {
symbols: vec![symbol],
timeframe: ONE_MINUTE,
start: Some(fetch_from),
end: Some(fetch_to),
limit: Some(10000),
adjustment: None,
adjustment: Some(alpaca::api::outgoing::bar::Adjustment::All),
asof: None,
feed: Some(*ALPACA_SOURCE),
currency: None,
@@ -196,8 +196,8 @@ fn crypto_query_constructor(
fetch_from: OffsetDateTime,
fetch_to: OffsetDateTime,
next_page_token: Option<String>,
) -> api::outgoing::bar::Bar {
api::outgoing::bar::Bar::Crypto {
) -> alpaca::api::outgoing::bar::Bar {
alpaca::api::outgoing::bar::Bar::Crypto {
symbols: vec![symbol],
timeframe: ONE_MINUTE,
start: Some(fetch_from),
@@ -241,7 +241,7 @@ impl Handler for BarHandler {
let mut next_page_token = None;
loop {
let Ok(message) = api::incoming::bar::get_historical(
let Ok(message) = alpaca::api::incoming::bar::get_historical(
&self.config.alpaca_client,
&self.config.alpaca_rate_limiter,
self.data_url,
@@ -328,10 +328,10 @@ impl Handler for NewsHandler {
let mut next_page_token = None;
loop {
let Ok(message) = api::incoming::news::get_historical(
let Ok(message) = alpaca::api::incoming::news::get_historical(
&self.config.alpaca_client,
&self.config.alpaca_rate_limiter,
&api::outgoing::news::News {
&alpaca::api::outgoing::news::News {
symbols: vec![symbol.clone()],
start: Some(fetch_from),
end: Some(fetch_to),

View File

@@ -95,7 +95,9 @@ pub async fn get(
.await?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN) => backoff::Error::Permanent(e),
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Account>()

View File

@@ -63,9 +63,11 @@ pub async fn get_by_symbol(
.await?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN | reqwest::StatusCode::NOT_FOUND) => {
backoff::Error::Permanent(e)
}
Some(
reqwest::StatusCode::BAD_REQUEST
| reqwest::StatusCode::FORBIDDEN
| reqwest::StatusCode::NOT_FOUND,
) => backoff::Error::Permanent(e),
_ => e.into(),
})?
.json::<Asset>()

View File

@@ -68,7 +68,9 @@ pub async fn get_historical(
.await?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN) => backoff::Error::Permanent(e),
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Message>()

View File

@@ -33,7 +33,9 @@ pub async fn get(
.await?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN) => backoff::Error::Permanent(e),
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Clock>()

View File

@@ -88,7 +88,13 @@ pub async fn get_historical(
.query(query)
.send()
.await?
.error_for_status()?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Message>()
.await
.map_err(backoff::Error::Permanent)

View File

@@ -27,7 +27,9 @@ pub async fn get(
.await?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN) => backoff::Error::Permanent(e),
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Vec<Order>>()

View File

@@ -81,7 +81,9 @@ pub async fn get(
.await?
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN) => backoff::Error::Permanent(e),
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Vec<Position>>()
@@ -121,7 +123,9 @@ pub async fn get_by_symbol(
response
.error_for_status()
.map_err(|e| match e.status() {
Some(reqwest::StatusCode::FORBIDDEN) => backoff::Error::Permanent(e),
Some(reqwest::StatusCode::BAD_REQUEST | reqwest::StatusCode::FORBIDDEN) => {
backoff::Error::Permanent(e)
}
_ => e.into(),
})?
.json::<Position>()

View File

@@ -7,6 +7,7 @@ use std::time::Duration;
use time::OffsetDateTime;
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
#[allow(dead_code)]
pub enum Adjustment {
Raw,