Skip to content

Commit fc07672

Browse files
committed
parameter type cleanup and None value handling in request.
1 parent cee3cc6 commit fc07672

File tree

11 files changed

+78
-51
lines changed

11 files changed

+78
-51
lines changed

examples/launchpad/launchpad.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
def getAggsLaunchpad():
66
client = RESTClient()
77

8-
options = RequestOptionBuilder().required_edge_headers(
9-
edge_id="EDGE_ID",
10-
edge_ip_address="EDGE_ID_ADDRESS"
11-
).edge_user_agent_header(
12-
user_agent="EDGE_USER_AGENT"
8+
options = (
9+
RequestOptionBuilder()
10+
.required_edge_headers(edge_id="EDGE_ID", edge_ip_address="EDGE_ID_ADDRESS")
11+
.edge_user_agent_header(user_agent="EDGE_USER_AGENT")
1312
)
1413

1514
trades = []

examples/rest/simple-get.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
client = RESTClient()
55

6-
options = models.required_edge_headers({}, "d", "b")
7-
options = models.edge_user_agent(options, "user-agent")
8-
9-
aggs = client.get_aggs("AAPL", 1, "day", "2022-04-04", "2022-04-04", options=options)
6+
aggs = client.get_aggs(
7+
"AAPL",
8+
1,
9+
"day",
10+
"2022-04-04",
11+
"2022-04-04",
12+
)
1013
print(aggs)

polygon/rest/aggs.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from urllib3 import HTTPResponse
55
from datetime import datetime, date
66

7+
from .models.request import RequestOptionBuilder
8+
79

810
class AggsClient(BaseClient):
911
def get_aggs(
@@ -19,7 +21,7 @@ def get_aggs(
1921
limit: Optional[int] = None,
2022
params: Optional[Dict[str, Any]] = None,
2123
raw: bool = False,
22-
options: Optional[dict] = None,
24+
options: Optional[RequestOptionBuilder] = None,
2325
) -> Union[List[Agg], HTTPResponse]:
2426
"""
2527
Get aggregate bars for a ticker over a given date range in custom time window sizes.
@@ -62,7 +64,7 @@ def get_grouped_daily_aggs(
6264
raw: bool = False,
6365
market_type: str = "stocks",
6466
include_otc: bool = False,
65-
options: Optional[dict] = None,
67+
options: Optional[RequestOptionBuilder] = None,
6668
) -> Union[GroupedDailyAgg, HTTPResponse]:
6769
"""
6870
Get the daily open, high, low, and close (OHLC) for the entire market.
@@ -93,7 +95,7 @@ def get_daily_open_close_agg(
9395
adjusted: Optional[bool] = None,
9496
params: Optional[Dict[str, Any]] = None,
9597
raw: bool = False,
96-
options: Optional[dict] = None,
98+
options: Optional[RequestOptionBuilder] = None,
9799
) -> Union[DailyOpenCloseAgg, HTTPResponse]:
98100
"""
99101
Get the open, close and afterhours prices of a stock symbol on a certain date.
@@ -121,7 +123,7 @@ def get_previous_close_agg(
121123
adjusted: Optional[bool] = None,
122124
params: Optional[Dict[str, Any]] = None,
123125
raw: bool = False,
124-
options: Optional[dict] = None,
126+
options: Optional[RequestOptionBuilder] = None,
125127
) -> Union[PreviousCloseAgg, HTTPResponse]:
126128
"""
127129
Get the previous day's open, high, low, and close (OHLC) for the specified stock ticker.

polygon/rest/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ def _get(
7979
params = {str(k): str(v) for k, v in params.items() if v is not None}
8080
logger.debug("_get %s params %s", path, params)
8181

82+
option = RequestOptionBuilder() if options is None else options
83+
8284
resp = self.client.request(
8385
"GET",
8486
self.BASE + path,
8587
fields=params,
8688
retries=self.retries,
87-
headers={**options.edge_headers, **self.headers}, # merge supplied headers with standard headers
89+
headers={
90+
**option.edge_headers,
91+
**self.headers,
92+
}, # merge supplied headers with standard headers
8893
)
8994

9095
if resp.status != 200:

polygon/rest/indicators.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from urllib3 import HTTPResponse
1212
from datetime import datetime, date
1313

14+
from .models.request import RequestOptionBuilder
15+
1416

1517
class IndicatorsClient(BaseClient):
1618
def get_sma(
@@ -29,7 +31,7 @@ def get_sma(
2931
params: Optional[Dict[str, Any]] = None,
3032
series_type: Optional[Union[str, SeriesType]] = None,
3133
raw: bool = False,
32-
options: Optional[dict] = None,
34+
options: Optional[RequestOptionBuilder] = None,
3335
) -> Union[SMAIndicatorResults, HTTPResponse]:
3436
"""
3537
Get SMA values for a given ticker over a given range with the specified parameters
@@ -82,7 +84,7 @@ def get_ema(
8284
params: Optional[Dict[str, Any]] = None,
8385
series_type: Optional[Union[str, SeriesType]] = None,
8486
raw: bool = False,
85-
options: Optional[dict] = None,
87+
options: Optional[RequestOptionBuilder] = None,
8688
) -> Union[EMAIndicatorResults, HTTPResponse]:
8789
"""
8890
Get EMA values for a given ticker over a given range with the specified parameters
@@ -116,6 +118,7 @@ def get_ema(
116118
result_key="results",
117119
deserializer=EMAIndicatorResults.from_dict,
118120
raw=raw,
121+
options=options,
119122
)
120123

121124
def get_rsi(
@@ -134,7 +137,7 @@ def get_rsi(
134137
params: Optional[Dict[str, Any]] = None,
135138
series_type: Optional[Union[str, SeriesType]] = None,
136139
raw: bool = False,
137-
options: Optional[dict] = None,
140+
options: Optional[RequestOptionBuilder] = None,
138141
) -> Union[RSIIndicatorResults, HTTPResponse]:
139142
"""
140143
Get RSI values for a given ticker over a given range with the specified parameters
@@ -189,7 +192,7 @@ def get_macd(
189192
params: Optional[Dict[str, Any]] = None,
190193
series_type: Optional[Union[str, SeriesType]] = None,
191194
raw: bool = False,
192-
options: Optional[dict] = None,
195+
options: Optional[RequestOptionBuilder] = None,
193196
) -> Union[MACDIndicatorResults, HTTPResponse]:
194197
"""
195198
Get MACD values for a given ticker over a given range with the specified parameters

polygon/rest/models/request.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
X_POLYGON_EDGE_IP_ADDRESS = "X-Polygon-Edge-IP-Address"
55
X_POLYGON_EDGE_USER_AGENT = "X-Polygon-Edge-User-Agent"
66

7-
HEADER = 'header'
7+
HEADER = "header"
88

99

1010
class RequestOptionBuilder:
11-
def __init__(self,
12-
options: Optional[Dict[str, Dict[str, str]]] = None,
13-
edge_headers: Optional[Dict[str, Dict[str, str]]] = None,
14-
):
11+
def __init__(
12+
self,
13+
options: Optional[Dict[str, Dict[str, str]]] = None,
14+
edge_headers: Optional[Dict[str, str]] = None,
15+
):
1516
"""
1617
RequestOptionBuilder is a utility class used to format and produce options for Polygon Requests\
1718
:param options: preset options object to be the base of the options dictionary.
@@ -42,9 +43,9 @@ def __add_to_options(self, key: str, **options):
4243
self.__set_edge_headers(options[key])
4344

4445
def required_edge_headers(
45-
self,
46-
edge_id: str,
47-
edge_ip_address: str,
46+
self,
47+
edge_id: str,
48+
edge_ip_address: str,
4849
):
4950
"""
5051
require_edge_headers adds required headers to the headers' dictionary
@@ -60,7 +61,10 @@ def required_edge_headers(
6061
)
6162
return self
6263

63-
def edge_user_agent_header(self, user_agent: str, ):
64+
def edge_user_agent_header(
65+
self,
66+
user_agent: str,
67+
):
6468
"""
6569
edge_user_agent_header is used to add the optional X-Polygon-Edge-User-Agent key to the header dictionary
6670
:param user_agent: is an optional Launchpad header. It denotes the originating UserAgent of the Edge User requesting data.

polygon/rest/quotes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from urllib3 import HTTPResponse
1313
from datetime import datetime, date
1414

15+
from .models.request import RequestOptionBuilder
16+
17+
1518
# https://polygon.io/docs/stocks
1619
class QuotesClient(BaseClient):
1720
def list_quotes(
@@ -27,7 +30,7 @@ def list_quotes(
2730
order: Optional[Union[str, Order]] = None,
2831
params: Optional[Dict[str, Any]] = None,
2932
raw: bool = False,
30-
options: Optional[dict] = None,
33+
options: Optional[RequestOptionBuilder] = None,
3134
) -> Union[Iterator[Quote], HTTPResponse]:
3235
"""
3336
Get quotes for a ticker symbol in a given time range.
@@ -60,7 +63,7 @@ def get_last_quote(
6063
ticker: str,
6164
params: Optional[Dict[str, Any]] = None,
6265
raw: bool = False,
63-
options: Optional[dict] = None,
66+
options: Optional[RequestOptionBuilder] = None,
6467
) -> Union[LastQuote, HTTPResponse]:
6568
"""
6669
Get the most recent NBBO (Quote) tick for a given stock.
@@ -87,7 +90,7 @@ def get_last_forex_quote(
8790
to: str,
8891
params: Optional[Dict[str, Any]] = None,
8992
raw: bool = False,
90-
options: Optional[dict] = None,
93+
options: Optional[RequestOptionBuilder] = None,
9194
) -> Union[LastForexQuote, HTTPResponse]:
9295
"""
9396
Get the last quote tick for a forex currency pair.
@@ -116,7 +119,7 @@ def get_real_time_currency_conversion(
116119
precision: Union[int, Precision] = 2,
117120
params: Optional[Dict[str, Any]] = None,
118121
raw: bool = False,
119-
options: Optional[dict] = None,
122+
options: Optional[RequestOptionBuilder] = None,
120123
) -> Union[RealTimeCurrencyConversion, HTTPResponse]:
121124
"""
122125
Get currency conversions using the latest market conversion rates.

polygon/rest/reference.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from urllib3 import HTTPResponse
2626
from datetime import date
2727

28+
from .models.request import RequestOptionBuilder
29+
2830

2931
class MarketsClient(BaseClient):
3032
def get_market_holidays(
@@ -85,7 +87,7 @@ def list_tickers(
8587
order: Optional[Union[str, Order]] = "asc",
8688
params: Optional[Dict[str, Any]] = None,
8789
raw: bool = False,
88-
options: Optional[dict] = None,
90+
options: Optional[RequestOptionBuilder] = None,
8991
) -> Union[Iterator[Ticker], HTTPResponse]:
9092
"""
9193
Query all ticker symbols which are supported by Polygon.io. This API currently includes Stocks/Equities, Crypto, and Forex.
@@ -126,7 +128,7 @@ def get_ticker_details(
126128
date: Optional[str] = None,
127129
params: Optional[Dict[str, Any]] = None,
128130
raw: bool = False,
129-
options: Optional[dict] = None,
131+
options: Optional[RequestOptionBuilder] = None,
130132
) -> Union[TickerDetails, HTTPResponse]:
131133
"""
132134
Get a single ticker supported by Polygon.io. This response will have detailed information about the ticker and the company behind it.
@@ -153,7 +155,7 @@ def get_ticker_events(
153155
ticker: Optional[str] = None,
154156
params: Optional[Dict[str, Any]] = None,
155157
raw: bool = False,
156-
options: Optional[dict] = None,
158+
options: Optional[RequestOptionBuilder] = None,
157159
) -> Union[TickerChangeResults, HTTPResponse]:
158160

159161
"""
@@ -191,7 +193,7 @@ def list_ticker_news(
191193
order: Optional[Union[str, Order]] = None,
192194
params: Optional[Dict[str, Any]] = None,
193195
raw: bool = False,
194-
options: Optional[dict] = None,
196+
options: Optional[RequestOptionBuilder] = None,
195197
) -> Union[Iterator[TickerNews], HTTPResponse]:
196198
"""
197199
Get the most recent news articles relating to a stock ticker symbol, including a summary of the article and a link to the original source.
@@ -221,7 +223,7 @@ def get_ticker_types(
221223
locale: Optional[Union[str, Locale]] = None,
222224
params: Optional[Dict[str, Any]] = None,
223225
raw: bool = False,
224-
options: Optional[dict] = None,
226+
options: Optional[RequestOptionBuilder] = None,
225227
) -> Union[List[TickerTypes], HTTPResponse]:
226228
"""
227229
List all ticker types that Polygon.io has.
@@ -263,7 +265,7 @@ def list_splits(
263265
order: Optional[Union[str, Order]] = None,
264266
params: Optional[Dict[str, Any]] = None,
265267
raw: bool = False,
266-
options: Optional[dict] = None,
268+
options: Optional[RequestOptionBuilder] = None,
267269
) -> Union[Iterator[Split], HTTPResponse]:
268270
"""
269271
Get a list of historical stock splits, including the ticker symbol, the execution date, and the factors of the split ratio.
@@ -337,7 +339,7 @@ def list_dividends(
337339
order: Optional[Union[str, Order]] = None,
338340
params: Optional[Dict[str, Any]] = None,
339341
raw: bool = False,
340-
options: Optional[dict] = None,
342+
options: Optional[RequestOptionBuilder] = None,
341343
) -> Union[Iterator[Dividend], HTTPResponse]:
342344
"""
343345
Get a list of historical cash dividends, including the ticker symbol, declaration date, ex-dividend date, record date, pay date, frequency, and amount.
@@ -400,7 +402,7 @@ def list_conditions(
400402
order: Optional[Union[str, Order]] = None,
401403
params: Optional[Dict[str, Any]] = None,
402404
raw: bool = False,
403-
options: Optional[dict] = None,
405+
options: Optional[RequestOptionBuilder] = None,
404406
) -> Union[Iterator[Condition], HTTPResponse]:
405407
"""
406408
List all conditions that Polygon.io uses.
@@ -434,7 +436,7 @@ def get_exchanges(
434436
locale: Optional[Union[str, Locale]] = None,
435437
params: Optional[Dict[str, Any]] = None,
436438
raw: bool = False,
437-
options: Optional[dict] = None,
439+
options: Optional[RequestOptionBuilder] = None,
438440
) -> Union[List[Exchange], HTTPResponse]:
439441
"""
440442
List all exchanges that Polygon.io knows about.
@@ -464,7 +466,7 @@ def get_options_contract(
464466
as_of: Optional[Union[str, date]] = None,
465467
params: Optional[Dict[str, Any]] = None,
466468
raw: bool = False,
467-
options: Optional[dict] = None,
469+
options: Optional[RequestOptionBuilder] = None,
468470
) -> Union[OptionsContract, HTTPResponse]:
469471
"""
470472
Get the most recent trade for a ticker.
@@ -511,7 +513,7 @@ def list_options_contracts(
511513
order: Optional[Union[str, Order]] = None,
512514
params: Optional[Dict[str, Any]] = None,
513515
raw: bool = False,
514-
options: Optional[dict] = None,
516+
options: Optional[RequestOptionBuilder] = None,
515517
) -> Union[Iterator[OptionsContract], HTTPResponse]:
516518
"""
517519
List historical options contracts.

0 commit comments

Comments
 (0)