Skip to content

Commit

Permalink
expose previous_close endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRollen committed May 18, 2021
1 parent e384e86 commit 35287e2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polygon"
version = "0.8.4"
version = "0.9.0"
authors = ["Sebastian Rollen <[email protected]>"]
edition = "2018"

Expand Down
66 changes: 66 additions & 0 deletions src/rest/stocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,53 @@ pub struct TradeSnapshot {
pub x: u8,
}

// Previous close

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetPreviousClose<'a> {
pub ticker: &'a str,
pub unadjusted: bool,
}

impl Request for GetPreviousClose<'_> {
type Body = Self;
type Response = PreviousCloseWrapper;

fn endpoint(&self) -> Cow<str> {
format!("/v2/aggs/ticker/{}/prev", self.ticker).into()
}

fn body(&self) -> RequestBody<&Self> {
RequestBody::Query(&self)
}
}

#[derive(Deserialize, Debug, Clone)]
pub struct PreviousCloseWrapper {
pub ticker: String,
pub status: String,
pub adjusted: bool,
#[serde(rename = "queryCount")]
pub query_count: usize,
#[serde(rename = "resultsCount")]
pub results_count: usize,
pub request_id: String,
pub results: Vec<PreviousClose>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct PreviousClose {
pub o: Decimal,
pub h: Decimal,
pub l: Decimal,
pub c: Decimal,
pub v: u64,
pub vw: Decimal,
pub t: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub n: Option<u32>,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -366,4 +413,23 @@ mod test {
let req = GetTickerSnapshot("AAPL");
client.send(req).await.unwrap();
}

#[tokio::test]
async fn get_previous_close() {
let _m = mock("GET", "/v2/aggs/ticker/AAPL/prev")
.match_query(Matcher::AllOf(vec![
Matcher::UrlEncoded("apiKey".into(), "TOKEN".into()),
Matcher::UrlEncoded("unadjusted".into(), "false".into())
]))
.with_body(r#"{"ticker":"AAPL","status":"OK","queryCount":1,"resultsCount":1,"adjusted":true,"results":[{"T":"AAPL","v":131704427,"vw":116.3058,"o":115.55,"c":115.97,"h":117.59,"l":114.13,"t":1605042000000}],"request_id":"6a7e466379af0a71039d60cc78e72282"}"#).create();

let url = mockito::server_url();

let client = Client::new(&url, "TOKEN");
let req = GetPreviousClose {
ticker: "AAPL",
unadjusted: false,
};
client.send(req).await.unwrap();
}
}

0 comments on commit 35287e2

Please sign in to comment.