Skip to content

Commit 6b4ee83

Browse files
author
clickingbuttons
authored
Add CI (#115)
* remove websocket, add linters and github actions for PRs for them * ci tweak * tweak ci (2) * vera feedback * add GH_TOKEN permisions
1 parent a363476 commit 6b4ee83

File tree

19 files changed

+431
-308
lines changed

19 files changed

+431
-308
lines changed

.drone.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

.github/workflows/lint.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- v1
8+
pull_request:
9+
permissions:
10+
contents: read
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
python-version: ['3.7', '3.8', '3.9', '3.10']
18+
name: Lint ${{ matrix.python-version }}
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: Setup Python
22+
uses: actions/setup-python@v3
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Setup Poetry
26+
uses: abatilo/[email protected]
27+
- name: Install pypi deps
28+
run: poetry install
29+
- name: Style lint
30+
run: poetry run black --check polygon
31+
- name: Static lint
32+
run: poetry run mypy polygon
33+
if: always()

poetry.lock

Lines changed: 290 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

polygon/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
from .websocket import WebSocketClient, STOCKS_CLUSTER, FOREX_CLUSTER, CRYPTO_CLUSTER
21
from .rest import RESTClient

polygon/rest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .aggs import AggsClient
22
from .trades import TradesClient
33

4+
45
class RESTClient(AggsClient, TradesClient):
56
pass
6-

polygon/rest/aggs.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
# https://polygon.io/docs/stocks
66
class AggsClient(BaseClient):
7-
def get_aggs(self,
7+
def get_aggs(
8+
self,
89
ticker: str,
910
multiplier: int,
1011
timespan: str,
1112
# "from" is a keyword in python https://www.w3schools.com/python/python_ref_keywords.asp
1213
from_: str,
1314
to: str,
14-
adjusted: Optional[bool]=None,
15-
sort: Optional[Union[str, Sort]]=None,
16-
limit: Optional[int]=None,
17-
params: Optional[Dict[str, Any]]=None,
18-
raw: bool=False
15+
adjusted: Optional[bool] = None,
16+
sort: Optional[Union[str, Sort]] = None,
17+
limit: Optional[int] = None,
18+
params: Optional[Dict[str, Any]] = None,
19+
raw: bool = False,
1920
) -> List[Agg]:
2021
"""
2122
Get aggregate bars for a ticker over a given date range in custom time window sizes.
@@ -35,5 +36,10 @@ def get_aggs(self,
3536
"""
3637
url = f"/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from_}/{to}"
3738

38-
return self._get(path=url, params=self._get_params(self.get_aggs, locals()), resultKey="results", deserializer=Agg.from_dict, raw=raw)
39-
39+
return self._get(
40+
path=url,
41+
params=self._get_params(self.get_aggs, locals()),
42+
resultKey="results",
43+
deserializer=Agg.from_dict,
44+
raw=raw,
45+
)

polygon/rest/base.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,54 @@
55
from enum import Enum
66
from typing import Optional, Any
77

8-
base = 'https://api.polygon.io'
8+
base = "https://api.polygon.io"
99
env_key = "POLYGON_API_KEY"
1010

1111
# https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
1212
class BaseClient:
1313
def __init__(
14-
self,
15-
api_key: Optional[str] = os.getenv(env_key),
16-
connect_timeout: float = 10.0,
17-
read_timeout: float = 10.0,
18-
num_pools: int = 10,
19-
retries = 3,
20-
base: str = base
21-
):
14+
self,
15+
api_key: Optional[str] = os.getenv(env_key),
16+
connect_timeout: float = 10.0,
17+
read_timeout: float = 10.0,
18+
num_pools: int = 10,
19+
retries=3,
20+
base: str = base,
21+
):
2222
if api_key is None:
23-
raise Exception(f"Must specify env var {env_key} or pass api_key in constructor")
23+
raise Exception(
24+
f"Must specify env var {env_key} or pass api_key in constructor"
25+
)
2426
self.API_KEY = api_key
2527
self.BASE = base
2628

2729
# https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool
28-
self.client = urllib3.PoolManager(num_pools=num_pools, headers={
29-
'Authorization': 'Bearer ' + self.API_KEY
30-
})
31-
self.timeout=urllib3.Timeout(connect=connect_timeout, read=read_timeout)
30+
self.client = urllib3.PoolManager(
31+
num_pools=num_pools, headers={"Authorization": "Bearer " + self.API_KEY}
32+
)
33+
self.timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout)
3234
self.retries = retries
3335

3436
def _decode(self, resp):
35-
return json.loads(resp.data.decode('utf-8'))
37+
return json.loads(resp.data.decode("utf-8"))
3638

37-
def _get(self, path: str, params: Optional[dict] = None, resultKey: Optional[str] = None, deserializer = None, raw: bool = False) -> Any:
39+
def _get(
40+
self,
41+
path: str,
42+
params: Optional[dict] = None,
43+
resultKey: Optional[str] = None,
44+
deserializer=None,
45+
raw: bool = False,
46+
) -> Any:
3847
if params is None:
3948
params = {}
4049
params = {str(k): str(v) for k, v in params.items() if v is not None}
41-
resp = self.client.request('GET', self.BASE + path, fields=params, retries=self.retries)
50+
resp = self.client.request(
51+
"GET", self.BASE + path, fields=params, retries=self.retries
52+
)
4253

4354
if resp.status != 200:
44-
raise Exception(resp.data.decode('utf-8'))
55+
raise Exception(resp.data.decode("utf-8"))
4556

4657
if raw:
4758
return resp
@@ -50,7 +61,7 @@ def _get(self, path: str, params: Optional[dict] = None, resultKey: Optional[str
5061

5162
if resultKey:
5263
obj = obj[resultKey]
53-
64+
5465
if deserializer:
5566
obj = [deserializer(o) for o in obj]
5667

@@ -63,7 +74,7 @@ def _get_params(self, fn, caller_locals):
6374
# https://docs.python.org/3.7/library/inspect.html#inspect.Signature
6475
for argname, v in inspect.signature(fn).parameters.items():
6576
# https://docs.python.org/3.7/library/inspect.html#inspect.Parameter
66-
if argname in ['params', 'raw']:
77+
if argname in ["params", "raw"]:
6778
continue
6879
if v.default != v.empty:
6980
# timestamp_lt -> timestamp.lt
@@ -77,14 +88,16 @@ def _get_params(self, fn, caller_locals):
7788

7889
def _paginate(self, path: str, params: dict, raw: bool, deserializer):
7990
while True:
80-
resp = self._get(path=path, params=params, deserializer=deserializer, raw=True)
91+
resp = self._get(
92+
path=path, params=params, deserializer=deserializer, raw=True
93+
)
8194
if raw:
8295
return resp
8396
decoded = self._decode(resp)
8497
for t in decoded["results"]:
8598
yield deserializer(t)
8699
if "next_url" in decoded:
87-
path = decoded["next_url"].replace(self.BASE, '')
100+
path = decoded["next_url"].replace(self.BASE, "")
88101
params = {}
89102
else:
90103
return

polygon/rest/models/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from .trades import *
33
from enum import Enum
44

5+
56
class Sort(Enum):
6-
ASC = 'asc'
7-
DESC = 'desc'
7+
ASC = "asc"
8+
DESC = "desc"
89

9-
class Order(Enum):
10-
ASC = 'asc'
11-
DESC = 'desc'
1210

11+
class Order(Enum):
12+
ASC = "asc"
13+
DESC = "desc"

polygon/rest/models/aggs.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
22

3+
34
@dataclass
45
class Agg:
56
open: float
@@ -13,14 +14,4 @@ class Agg:
1314

1415
@staticmethod
1516
def from_dict(d):
16-
return Agg(
17-
d['o'],
18-
d['h'],
19-
d['l'],
20-
d['c'],
21-
d['v'],
22-
d['vw'],
23-
d['t'],
24-
d['n']
25-
)
26-
17+
return Agg(d["o"], d["h"], d["l"], d["c"], d["v"], d["vw"], d["t"], d["n"])

polygon/rest/models/trades.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, List
22
from dataclasses import dataclass
33

4+
45
@dataclass
56
class Trade:
67
"Trade contains trade data for a specified ticker symbol."
@@ -20,4 +21,3 @@ class Trade:
2021
@staticmethod
2122
def from_dict(d):
2223
return Trade(**d)
23-

0 commit comments

Comments
 (0)