Skip to main content

polygon_io/reference/
ticker_details.rs

1extern crate serde_json;
2extern crate ureq;
3
4use crate::{
5	client::{Client, Result},
6	helpers::*,
7	with_param
8};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12#[derive(Debug, Deserialize, Serialize)]
13pub struct Address {
14	pub address1:    Option<String>,
15	pub address2:    Option<String>,
16	pub city:        Option<String>,
17	pub state:       Option<String>,
18	pub country:     Option<String>,
19	pub postal_code: Option<String>
20}
21
22#[derive(Debug, Deserialize, Serialize)]
23pub struct Branding {
24	pub icon_url: Option<String>,
25	pub logo_url: Option<String>
26}
27
28#[derive(Debug, Deserialize, Serialize)]
29pub struct TickerDetail {
30	pub ticker: String,
31	pub name: String,
32	pub market: String,
33	pub locale: String,
34	pub primary_exchange: Option<String>,
35	pub r#type: Option<String>,
36	pub active: bool,
37	pub currency_name: Option<String>,
38	pub cik: Option<String>,
39	pub composite_figi: Option<String>,
40	pub share_class_figi: Option<String>,
41	pub market_cap: Option<f64>,
42	pub phone_number: Option<String>,
43	//#[serde(flatten)]
44	// pub address: Option<Address>,
45	pub description: Option<String>,
46	pub sic_code: Option<String>,
47	pub sic_description: Option<String>,
48	pub ticker_root: Option<String>,
49	pub homepage_url: Option<String>,
50	pub total_employees: Option<u32>,
51	pub list_date: Option<String>,
52	//#[serde(flatten)]
53	// pub branding: Option<Branding>,
54	pub share_class_shares_outstanding: Option<f64>,
55	pub weighted_shares_outstanding: Option<f64>
56}
57
58#[derive(Debug, Deserialize, Serialize)]
59pub struct TickersResponse {
60	pub results:    TickerDetail,
61	// For debugging
62	pub status:     String,
63	pub request_id: String
64}
65
66pub struct TickerDetailsParams<'a> {
67	pub params: HashMap<&'a str, String>
68}
69
70impl<'a> TickerDetailsParams<'a> {
71	with_param!(date, &str);
72
73	pub fn new() -> Self {
74		Self {
75			params: HashMap::with_capacity(2)
76		}
77	}
78}
79
80impl Client {
81	pub fn get_ticker_details(
82		&self,
83		ticker: &str,
84		params: Option<&HashMap<&str, String>>
85	) -> Result<TickersResponse> {
86		let uri = format!(
87			"{}/v3/reference/tickers/{}{}",
88			self.api_uri,
89			ticker,
90			make_params(params),
91		);
92
93		let resp = self.get_response::<TickersResponse>(&uri)?;
94
95		Ok(resp)
96	}
97}
98
99#[cfg(test)]
100mod tickers {
101	use crate::{
102		client::{Client, Error},
103		reference::ticker_details::TickerDetailsParams
104	};
105
106	#[test]
107	fn works() {
108		let client = Client::new().unwrap();
109		let resp = client.get_ticker_details("AAPL", None).unwrap();
110		assert_eq!(resp.results.market, "stocks");
111	}
112
113	#[test]
114	fn works_day() {
115		let client = Client::new().unwrap();
116		let params = TickerDetailsParams::new().date("2004-01-02").params;
117		let resp = client.get_ticker_details("AAPL", Some(&params)).unwrap();
118		assert_eq!(resp.results.market, "stocks");
119	}
120
121	#[test]
122	fn works_empty() {
123		let client = Client::new().unwrap();
124		let params = TickerDetailsParams::new().date("2004-01-02").params;
125		let resp = client
126			.get_ticker_details("DOESN'T EXIST", Some(&params))
127			.unwrap_err();
128		match resp {
129			Error::EmptyResponse() => {}
130			e => panic!("bad error type {}", e)
131		};
132	}
133}