Skip to content

Commit 4ba62a4

Browse files
authored
Merge pull request #11 from PyDataBlog/cleanup_api
Cleaned up the draft API and utils
2 parents 820c884 + 7a646b7 commit 4ba62a4

File tree

8 files changed

+70
-87
lines changed

8 files changed

+70
-87
lines changed

docs/make.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ makedocs(;
1515
),
1616
pages=[
1717
"Home" => "index.md",
18+
"Reference" => "reference.md",
19+
"Stocks" => "stocks.md",
20+
"Crypto" => "crypto.md",
21+
"Forex" => "forex.md",
22+
"Streaming" => "streaming.md"
1823
],
1924
)
2025

docs/src/crypto.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Crypto API

docs/src/forex.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Forex API

docs/src/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Reference API

docs/src/stocks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Stocks API

docs/src/streaming.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Streaming API

src/reference_api.jl

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ function tickers(opts::PolyOpts,
2020
# Extract kwargs and add to params
2121
merge!(params, Dict(kwargs))
2222

23-
request_json = HTTP.get(tickers_base_url, query=params).body |> JSON3.read
24-
25-
if opts.sink === nothing
26-
return request_json.results
27-
else
28-
return request_json.results |> opts.sink
29-
end
23+
return generate_output_from_url(tickers_base_url, params, opts.sink; results=true, json_to_array=false)
3024

3125
end
3226

@@ -35,46 +29,32 @@ end
3529
"""
3630
function ticker_types(opts::PolyOpts)
3731
params = Dict("apiKey" => opts.api_key)
38-
request_json = HTTP.get(ticker_types_base_url, query=params).body |> JSON3.read
39-
40-
return request_json.results
32+
return generate_output_from_url(ticker_types_base_url, params, nothing; results=true, json_to_array=false)
4133
end
4234

4335
############ Tickers Details ####################
4436
"""
4537
"""
4638
function ticker_details(opts::PolyOpts, stocksTicker::String)
4739
params = Dict("apiKey" => opts.api_key)
48-
ticker_details_base_url = "https://api.polygon.io/v1/meta/symbols/$stocksTicker/company"
40+
ticker_details_url = "$ticker_details_base_url/$stocksTicker/company"
4941

50-
request_json = HTTP.get(ticker_details_base_url, query=params).body |> JSON3.read
51-
52-
if opts.sink === nothing
53-
return request_json
54-
else
55-
return [request_json] |> opts.sink
56-
end
42+
return generate_output_from_url(ticker_details_url, params, opts.sink; results=false, json_to_array=true)
5743
end
5844

5945
############ Ticker Details vX ####################
6046
"""
6147
"""
6248
function ticker_details_vX(opts::PolyOpts, ticker::String, date::String)
6349
# TODO: Dispatch on proper Date type?
64-
ticker_details_vX_base_url = "https://api.polygon.io/vX/reference/tickers/$ticker"
50+
ticker_details_vX_url = "$ticker_details_vX_base_url/$ticker"
6551

6652
params = Dict(
6753
"apiKey" => opts.api_key,
6854
"date" => date
6955
)
7056

71-
request_json = HTTP.get(ticker_details_vX_base_url, query=params).body |> JSON3.read
72-
73-
if opts.sink === nothing
74-
return request_json.results
75-
else
76-
return [request_json.results] |> opts.sink
77-
end
57+
return generate_output_from_url(ticker_details_vX_url, params, opts.sink; results=true, json_to_array=true)
7858
end
7959

8060
############ Ticker News #######################
@@ -99,9 +79,7 @@ function ticker_news(opts::PolyOpts,
9979
# Extract kwargs and add to params
10080
merge!(params, Dict(kwargs))
10181

102-
request_json = HTTP.get(ticker_new_base_url, query=params).body |> JSON3.read
103-
104-
return request_json.results
82+
return generate_output_from_url(ticker_news_base_url, params, nothing; results=true, json_to_array=false)
10583

10684
end
10785

@@ -110,13 +88,7 @@ end
11088
"""
11189
function markets(opts::PolyOpts)
11290
params = Dict("apiKey" => opts.api_key)
113-
request_json = HTTP.get(markets_base_url, query=params).body |> JSON3.read
114-
115-
if opts.sink === nothing
116-
return request_json.results
117-
else
118-
return request_json.results |> opts.sink
119-
end
91+
return generate_output_from_url(markets_base_url, params, opts.sink; results=true, json_to_array=false)
12092
end
12193

12294

@@ -125,95 +97,66 @@ end
12597
"""
12698
function locales(opts::PolyOpts)
12799
params = Dict("apiKey" => opts.api_key)
128-
request_json = HTTP.get(locales_base_url, query=params).body |> JSON3.read
129-
130-
if opts.sink === nothing
131-
return request_json.results
132-
else
133-
return request_json.results |> opts.sink
134-
end
100+
return generate_output_from_url(locales_base_url, params, opts.sink; results=true, json_to_array=false)
135101
end
136102

137103
############ Stock Splits ####################
138104
"""
139105
"""
140106
function stock_splits(opts::PolyOpts, stocksTicker::String)
141-
stock_splits_base_url = "https://api.polygon.io/v2/reference/splits/$stocksTicker"
107+
stock_splits_url = "$stock_splits_base_url/$stocksTicker"
142108
params = Dict("apiKey" => opts.api_key)
143-
request_json = HTTP.get(stock_splits_base_url, query=params).body |> JSON3.read
144109

145-
return request_json.results
110+
return generate_output_from_url(stock_splits_url, params, nothing; results=true, json_to_array=false)
146111
end
147112

148113
############ Stock Dividends ####################
149114
"""
150115
"""
151116
function stock_dividends(opts::PolyOpts, stocksTicker::String)
152-
stock_dividends_base_url = "https://api.polygon.io/v2/reference/dividends/$stocksTicker"
117+
stock_dividends_url = "$stock_dividends_base_url/$stocksTicker"
153118
params = Dict("apiKey" => opts.api_key)
154119

155-
request_json = HTTP.get(stock_dividends_base_url, query=params).body |> JSON3.read
156-
157-
if opts.sink === nothing
158-
return request_json.results
159-
else
160-
return request_json.results |> opts.sink
161-
end
120+
return generate_output_from_url(stock_dividends_url, params, opts.sink; results=true, json_to_array=false)
162121
end
163122

164123
############ Stock Financials ####################
165124
"""
166125
"""
167126
function stock_financials(opts::PolyOpts, stocksTicker::String; limit=5, kwargs...)
168-
stock_financials_base_url = "https://api.polygon.io/v2/reference/financials/$stocksTicker"
127+
stock_financials_url = "$stock_financials_base_url/$stocksTicker"
169128
params = Dict(
170129
"apiKey" => opts.api_key,
171130
"limit" => limit
172131
)
173132
# Extract kwargs and add to params
174133
merge!(params, Dict(kwargs))
175134

176-
request_json = HTTP.get(stock_financials_base_url, query=params).body |> JSON3.read
177-
178-
if opts.sink === nothing
179-
return request_json.results
180-
else
181-
return request_json.results |> opts.sink
182-
end
135+
return generate_output_from_url(stock_financials_url, params, opts.sink; results=true, json_to_array=false)
183136
end
184137

185138
############ Market Holidays ####################
186139
"""
187140
"""
188141
function market_holidays(opts::PolyOpts)
189142
params = Dict("apiKey" => opts.api_key)
190-
request_json = HTTP.get(market_holidays_base_url, query=params).body |> JSON3.read
191-
192-
if opts.sink === nothing
193-
return request_json
194-
else
195-
return request_json |> opts.sink
196-
end
143+
return generate_output_from_url(market_holidays_base_url, params, opts.sink; results=false, json_to_array=false)
197144
end
198145

199146
############ Market Status ####################
200147
"""
201148
"""
202149
function market_status(opts::PolyOpts)
203150
params = Dict("apiKey" => opts.api_key)
204-
request_json = HTTP.get(market_status_base_url, query=params).body |> JSON3.read
205-
206-
return request_json
151+
return generate_output_from_url(market_status_base_url, params, nothing; results=false, json_to_array=false)
207152
end
208153

209154
############ Stock Exchanges ####################
210155
"""
211156
"""
212157
function stock_exchanges(opts::PolyOpts)
213158
params = Dict("apiKey" => opts.api_key)
214-
request_json = HTTP.get(stock_exchanges_base_url, query=params).body |> JSON3.read
215-
216-
return request_json
159+
return generate_output_from_url(stock_exchanges_base_url, params, nothing; results=false, json_to_array=false)
217160
end
218161

219162
############ Condition Mappings ####################
@@ -223,21 +166,13 @@ function condition_mappings(opts::PolyOpts, tickertype="trades")
223166
condition_mappings_base_url = "https://api.polygon.io/v1/meta/conditions/$tickertype"
224167
params = Dict("apiKey" => opts.api_key)
225168

226-
request_json = HTTP.get(condition_mappings_base_url, query=params).body |> JSON3.read
227-
228-
return request_json
169+
return generate_output_from_url(condition_mappings_base_url, params, nothing; results=false, json_to_array=false)
229170
end
230171

231172
############ Crypto Exchanges ####################
232173
"""
233174
"""
234175
function crypto_exchanges(opts::PolyOpts)
235176
params = Dict("apiKey" => opts.api_key)
236-
request_json = HTTP.get(crypto_exchanges_base_url, query=params).body |> JSON3.read
237-
238-
if opts.sink === nothing
239-
return request_json
240-
else
241-
return request_json |> opts.sink
242-
end
177+
return generate_output_from_url(crypto_exchanges_base_url, params, opts.sink; results=false, json_to_array=false)
243178
end

src/utils.jl

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tickers_base_url = "https://api.polygon.io/v3/reference/tickers"
22

33
ticker_types_base_url = "https://api.polygon.io/v2/reference/types"
44

5-
ticker_new_base_url = "https://api.polygon.io/v2/reference/news"
5+
ticker_news_base_url = "https://api.polygon.io/v2/reference/news"
66

77
markets_base_url = "https://api.polygon.io/v2/reference/markets"
88

@@ -14,4 +14,42 @@ market_status_base_url = "https://api.polygon.io/v1/marketstatus/now"
1414

1515
stock_exchanges_base_url = "https://api.polygon.io/v1/meta/exchanges"
1616

17-
crypto_exchanges_base_url = "https://api.polygon.io/v1/meta/crypto-exchanges"
17+
crypto_exchanges_base_url = "https://api.polygon.io/v1/meta/crypto-exchanges"
18+
19+
ticker_details_base_url = "https://api.polygon.io/v1/meta/symbols"
20+
21+
ticker_details_vX_base_url = "https://api.polygon.io/vX/reference/tickers"
22+
23+
stock_splits_base_url = "https://api.polygon.io/v2/reference/splits"
24+
25+
stock_dividends_base_url = "https://api.polygon.io/v2/reference/dividends"
26+
27+
stock_financials_base_url = "https://api.polygon.io/v2/reference/financials"
28+
29+
"""
30+
"""
31+
function generate_output_from_url(url, params, sink; results=true, json_to_array=false)
32+
request_json = HTTP.get(url, query=params).body |> JSON3.read
33+
34+
if sink === nothing
35+
if results
36+
return request_json.results
37+
else
38+
return request_json
39+
end
40+
else
41+
if results
42+
if json_to_array
43+
return request_json.results |> x -> sink([x])
44+
else
45+
return request_json.results |> sink
46+
end
47+
else
48+
if json_to_array
49+
return request_json |> x -> sink([x])
50+
else
51+
return request_json |> sink
52+
end
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)