Skip to content

Rest mvp #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ steps:
- name: test
image: python:3.7.5-alpine
commands:
- echo "testing"
- pip install -U tox
- tox

trigger:
branch:
Expand Down
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
venv/
.python-version

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

#Ipython Notebook
.ipynb_checkpoints
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018 The Python Packaging Authority

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[![Build Status](https://drone.polygon.io/api/badges/Polygon-io/polygon-client-python/status.svg)](https://drone.polygon.io/Polygon-io/polygon-client-python)

# A Python client library for Polgyon's WebSocket and RESTful APIs

Currently this repo only supports the WebSocket API
# A Python client library for Polygon's WebSocket and RESTful APIs

## Getting Started

For a basic product overview, check out our [setup and use documentation](https://polygon.io/sockets)

### Install

`pip install polygon-api-client`

## Simple Demo
## Simple WebSocket Demo
```python
import time


from polygon_client import WebSocketClient, STOCKS_CLUSTER
from polygon import WebSocketClient, STOCKS_CLUSTER


def my_customer_process_message(message):
Expand All @@ -36,3 +37,27 @@ if __name__ == "__main__":
main()

```

## Simple REST Demo
```python
from polygon import RESTClient


def main():
key = "your api key"
client = RESTClient(key)

resp = client.stocks_equities_daily_open_close("AAPL", "2018-3-2")
print(f"On: {resp.from_} Apple opened at {resp.open} and closed at {resp.close}")


if __name__ == '__main__':
main()
```


## Notes about the REST Client

We use swagger as our API spec and we used this swagger to generate most of the code that defines the REST client.
We made this decision due to the size of our API, many endpoints and object definitions, and to accommodate future changes.

2 changes: 2 additions & 0 deletions polygon/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .websocket import WebSocketClient, STOCKS_CLUSTER, FOREX_CLUSTER, CRYPTO_CLUSTER
from .rest import RESTClient
1 change: 1 addition & 0 deletions polygon/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .client import RESTClient
201 changes: 201 additions & 0 deletions polygon/rest/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
from typing import Dict, Type

import requests

from polygon.rest import models
from polygon.rest.models import unmarshal


class RESTClient:
""" This is a custom generated class """
DEFAULT_HOST = "api.polygon.io"

def __init__(self, auth_key: str):
self.auth_key = auth_key
self.url = "https://" + self.DEFAULT_HOST

self._session = requests.Session()
self._session.params["apiKey"] = self.auth_key

def _handle_response(self, response_type: str, endpoint: str, params: Dict[str, str]) -> Type[models.AnyDefinition]:
resp: requests.Response = self._session.get(endpoint, params=params)
if resp.status_code == 200:
return unmarshal.unmarshal_json(response_type, resp.json())
else:
resp.raise_for_status()

def reference_tickers(self, **query_params) -> models.ReferenceTickersApiResponse:
endpoint = f"{self.url}/v2/reference/tickers"
return self._handle_response("ReferenceTickersApiResponse", endpoint, query_params)

def reference_ticker_types(self, **query_params) -> models.ReferenceTickerTypesApiResponse:
endpoint = f"{self.url}/v2/reference/types"
return self._handle_response("ReferenceTickerTypesApiResponse", endpoint, query_params)

def reference_ticker_details(self, symbol, **query_params) -> models.ReferenceTickerDetailsApiResponse:
endpoint = f"{self.url}/v1/meta/symbols/{symbol}/company"
return self._handle_response("ReferenceTickerDetailsApiResponse", endpoint, query_params)

def reference_ticker_news(self, symbol, **query_params) -> models.ReferenceTickerNewsApiResponse:
endpoint = f"{self.url}/v1/meta/symbols/{symbol}/news"
return self._handle_response("ReferenceTickerNewsApiResponse", endpoint, query_params)

def reference_markets(self, **query_params) -> models.ReferenceMarketsApiResponse:
endpoint = f"{self.url}/v2/reference/markets"
return self._handle_response("ReferenceMarketsApiResponse", endpoint, query_params)

def reference_locales(self, **query_params) -> models.ReferenceLocalesApiResponse:
endpoint = f"{self.url}/v2/reference/locales"
return self._handle_response("ReferenceLocalesApiResponse", endpoint, query_params)

def reference_stock_splits(self, symbol, **query_params) -> models.ReferenceStockSplitsApiResponse:
endpoint = f"{self.url}/v2/reference/splits/{symbol}"
return self._handle_response("ReferenceStockSplitsApiResponse", endpoint, query_params)

def reference_stock_dividends(self, symbol, **query_params) -> models.ReferenceStockDividendsApiResponse:
endpoint = f"{self.url}/v2/reference/dividends/{symbol}"
return self._handle_response("ReferenceStockDividendsApiResponse", endpoint, query_params)

def reference_stock_financials(self, symbol, **query_params) -> models.ReferenceStockFinancialsApiResponse:
endpoint = f"{self.url}/v2/reference/financials/{symbol}"
return self._handle_response("ReferenceStockFinancialsApiResponse", endpoint, query_params)

def reference_market_status(self, **query_params) -> models.ReferenceMarketStatusApiResponse:
endpoint = f"{self.url}/v1/marketstatus/now"
return self._handle_response("ReferenceMarketStatusApiResponse", endpoint, query_params)

def reference_market_holidays(self, **query_params) -> models.ReferenceMarketHolidaysApiResponse:
endpoint = f"{self.url}/v1/marketstatus/upcoming"
return self._handle_response("ReferenceMarketHolidaysApiResponse", endpoint, query_params)

def stocks_equities_exchanges(self, **query_params) -> models.StocksEquitiesExchangesApiResponse:
endpoint = f"{self.url}/v1/meta/exchanges"
return self._handle_response("StocksEquitiesExchangesApiResponse", endpoint, query_params)

def stocks_equities_historic_trades(self, symbol, date,
**query_params) -> models.StocksEquitiesHistoricTradesApiResponse:
endpoint = f"{self.url}/v1/historic/trades/{symbol}/{date}"
return self._handle_response("StocksEquitiesHistoricTradesApiResponse", endpoint, query_params)

def historic_trades_v2(self, ticker, date, **query_params) -> models.HistoricTradesV2ApiResponse:
endpoint = f"{self.url}/v2/ticks/stocks/trades/{ticker}/{date}"
return self._handle_response("HistoricTradesV2ApiResponse", endpoint, query_params)

def stocks_equities_historic_quotes(self, symbol, date,
**query_params) -> models.StocksEquitiesHistoricQuotesApiResponse:
endpoint = f"{self.url}/v1/historic/quotes/{symbol}/{date}"
return self._handle_response("StocksEquitiesHistoricQuotesApiResponse", endpoint, query_params)

def historic_n___bbo_quotes_v2(self, ticker, date, **query_params) -> models.HistoricNBboQuotesV2ApiResponse:
endpoint = f"{self.url}/v2/ticks/stocks/nbbo/{ticker}/{date}"
return self._handle_response("HistoricNBboQuotesV2ApiResponse", endpoint, query_params)

def stocks_equities_last_trade_for_a_symbol(self, symbol,
**query_params) -> models.StocksEquitiesLastTradeForASymbolApiResponse:
endpoint = f"{self.url}/v1/last/stocks/{symbol}"
return self._handle_response("StocksEquitiesLastTradeForASymbolApiResponse", endpoint, query_params)

def stocks_equities_last_quote_for_a_symbol(self, symbol,
**query_params) -> models.StocksEquitiesLastQuoteForASymbolApiResponse:
endpoint = f"{self.url}/v1/last_quote/stocks/{symbol}"
return self._handle_response("StocksEquitiesLastQuoteForASymbolApiResponse", endpoint, query_params)

def stocks_equities_daily_open_close(self, symbol, date,
**query_params) -> models.StocksEquitiesDailyOpenCloseApiResponse:
endpoint = f"{self.url}/v1/open-close/{symbol}/{date}"
return self._handle_response("StocksEquitiesDailyOpenCloseApiResponse", endpoint, query_params)

def stocks_equities_condition_mappings(self, ticktype,
**query_params) -> models.StocksEquitiesConditionMappingsApiResponse:
endpoint = f"{self.url}/v1/meta/conditions/{ticktype}"
return self._handle_response("StocksEquitiesConditionMappingsApiResponse", endpoint, query_params)

def stocks_equities_snapshot_all_tickers(self,
**query_params) -> models.StocksEquitiesSnapshotAllTickersApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/us/markets/stocks/tickers"
return self._handle_response("StocksEquitiesSnapshotAllTickersApiResponse", endpoint, query_params)

def stocks_equities_snapshot_single_ticker(self, ticker,
**query_params) -> models.StocksEquitiesSnapshotSingleTickerApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/us/markets/stocks/tickers/{ticker}"
return self._handle_response("StocksEquitiesSnapshotSingleTickerApiResponse", endpoint, query_params)

def stocks_equities_snapshot_gainers_losers(self, direction,
**query_params) -> models.StocksEquitiesSnapshotGainersLosersApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/us/markets/stocks/{direction}"
return self._handle_response("StocksEquitiesSnapshotGainersLosersApiResponse", endpoint, query_params)

def stocks_equities_previous_close(self, ticker, **query_params) -> models.StocksEquitiesPreviousCloseApiResponse:
endpoint = f"{self.url}/v2/aggs/ticker/{ticker}/prev"
return self._handle_response("StocksEquitiesPreviousCloseApiResponse", endpoint, query_params)

def stocks_equities_aggregates(self, ticker, multiplier, timespan, from_, to,
**query_params) -> models.StocksEquitiesAggregatesApiResponse:
endpoint = f"{self.url}/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from_}/{to}"
return self._handle_response("StocksEquitiesAggregatesApiResponse", endpoint, query_params)

def stocks_equities_grouped_daily(self, locale, market, date,
**query_params) -> models.StocksEquitiesGroupedDailyApiResponse:
endpoint = f"{self.url}/v2/aggs/grouped/locale/{locale}/market/{market}/{date}"
return self._handle_response("StocksEquitiesGroupedDailyApiResponse", endpoint, query_params)

def forex_currencies_historic_forex_ticks(self, from_, to, date,
**query_params) -> models.ForexCurrenciesHistoricForexTicksApiResponse:
endpoint = f"{self.url}/v1/historic/forex/{from_}/{to}/{date}"
return self._handle_response("ForexCurrenciesHistoricForexTicksApiResponse", endpoint, query_params)

def forex_currencies_real_time_currency_conversion(self, from_, to,
**query_params) -> models.ForexCurrenciesRealTimeCurrencyConversionApiResponse:
endpoint = f"{self.url}/v1/conversion/{from_}/{to}"
return self._handle_response("ForexCurrenciesRealTimeCurrencyConversionApiResponse", endpoint, query_params)

def forex_currencies_last_quote_for_a_currency_pair(self, from_, to,
**query_params) -> models.ForexCurrenciesLastQuoteForACurrencyPairApiResponse:
endpoint = f"{self.url}/v1/last_quote/currencies/{from_}/{to}"
return self._handle_response("ForexCurrenciesLastQuoteForACurrencyPairApiResponse", endpoint, query_params)

def forex_currencies_snapshot_all_tickers(self,
**query_params) -> models.ForexCurrenciesSnapshotAllTickersApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/global/markets/forex/tickers"
return self._handle_response("ForexCurrenciesSnapshotAllTickersApiResponse", endpoint, query_params)

def forex_currencies_snapshot_gainers_losers(self, direction,
**query_params) -> models.ForexCurrenciesSnapshotGainersLosersApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/global/markets/forex/{direction}"
return self._handle_response("ForexCurrenciesSnapshotGainersLosersApiResponse", endpoint, query_params)

def crypto_crypto_exchanges(self, **query_params) -> models.CryptoCryptoExchangesApiResponse:
endpoint = f"{self.url}/v1/meta/crypto-exchanges"
return self._handle_response("CryptoCryptoExchangesApiResponse", endpoint, query_params)

def crypto_last_trade_for_a_crypto_pair(self, from_, to,
**query_params) -> models.CryptoLastTradeForACryptoPairApiResponse:
endpoint = f"{self.url}/v1/last/crypto/{from_}/{to}"
return self._handle_response("CryptoLastTradeForACryptoPairApiResponse", endpoint, query_params)

def crypto_daily_open_close(self, from_, to, date, **query_params) -> models.CryptoDailyOpenCloseApiResponse:
endpoint = f"{self.url}/v1/open-close/crypto/{from_}/{to}/{date}"
return self._handle_response("CryptoDailyOpenCloseApiResponse", endpoint, query_params)

def crypto_historic_crypto_trades(self, from_, to, date,
**query_params) -> models.CryptoHistoricCryptoTradesApiResponse:
endpoint = f"{self.url}/v1/historic/crypto/{from_}/{to}/{date}"
return self._handle_response("CryptoHistoricCryptoTradesApiResponse", endpoint, query_params)

def crypto_snapshot_all_tickers(self, **query_params) -> models.CryptoSnapshotAllTickersApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/global/markets/crypto/tickers"
return self._handle_response("CryptoSnapshotAllTickersApiResponse", endpoint, query_params)

def crypto_snapshot_single_ticker(self, ticker, **query_params) -> models.CryptoSnapshotSingleTickerApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/global/markets/crypto/tickers/{ticker}"
return self._handle_response("CryptoSnapshotSingleTickerApiResponse", endpoint, query_params)

def crypto_snapshot_single_ticker_full_book(self, ticker,
**query_params) -> models.CryptoSnapshotSingleTickerFullBookApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/global/markets/crypto/tickers/{ticker}/book"
return self._handle_response("CryptoSnapshotSingleTickerFullBookApiResponse", endpoint, query_params)

def crypto_snapshot_gainers_losers(self, direction,
**query_params) -> models.CryptoSnapshotGainersLosersApiResponse:
endpoint = f"{self.url}/v2/snapshot/locale/global/markets/crypto/{direction}"
return self._handle_response("CryptoSnapshotGainersLosersApiResponse", endpoint, query_params)
Loading