Skip to content

Commit

Permalink
related westonplatter#11. swap bearer for client in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
westonplatter committed Aug 23, 2018
1 parent f832538 commit f2f583f
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 114 deletions.
13 changes: 7 additions & 6 deletions examples/humanize_option_positions_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import json
import configparser
from fast_arrow import (
Auth,
OptionChain,
Option,
OptionPosition,
OptionMarketdata,
)
from fast_arrow.client import Client

#
# get the authentication configs
Expand All @@ -20,15 +20,16 @@


#
# login and get the bearer token
# initialize and authenticate Client
#
bearer = Auth.login_oauth2(username, password)
client = Client(username, password)
client.client.authenticate()


#
# fetch option_positions
#
all_option_positions = OptionPosition.all(bearer)
all_option_positions = OptionPosition.all(client)


#
Expand All @@ -40,14 +41,14 @@
#
# append marketdata to each position
#
ops = OptionPosition.mergein_marketdata_list(bearer, ops)
ops = OptionPosition.mergein_marketdata_list(client, ops)



#
# append instrument data to each position
#
ops = OptionPosition.mergein_instrumentdata_list(bearer, ops)
ops = OptionPosition.mergein_instrumentdata_list(client, ops)


#
Expand Down
14 changes: 8 additions & 6 deletions examples/option_chain_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
OptionChain,
Option
)
from fast_arrow.client import Client


#
Expand All @@ -18,23 +19,24 @@


#
# get the bearer token
# initialize and authenticate Client
#
bearer = Auth.login_oauth2(username, password)
client = Client(username, password)
client.client.authenticate()


#
# fetch the stock info for TLT
#
symbol = "TLT"
stock = Stock.fetch(bearer, symbol)
stock = Stock.fetch(client, symbol)


#
# get the TLT option chain info
#
stock_id = stock["id"]
option_chain = OptionChain.fetch(bearer, stock_id)
option_chain = OptionChain.fetch(client, stock_id)
option_chain_id = option_chain["id"]
expiration_dates = option_chain['expiration_dates']

Expand All @@ -48,9 +50,9 @@
#
# get all options on the TLT option chain
#
ops = Option.in_chain(bearer, option_chain_id, expiration_dates=next_3_expiration_dates)
ops = Option.in_chain(client, option_chain_id, expiration_dates=next_3_expiration_dates)

#
# merge in market data fro TLT option instruments
#
ops = Option.mergein_marketdata_list(bearer, ops)
ops = Option.mergein_marketdata_list(client, ops)
8 changes: 5 additions & 3 deletions examples/option_event_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Auth,
OptionEvent
)
from fast_arrow.client import Client


#
Expand All @@ -16,12 +17,13 @@


#
# login and get the bearer token
# initialize and authenticate Client
#
bearer = Auth.login_oauth2(username, password)
client = Client(username, password)
client.client.authenticate()


#
# fetch all option events
#
events = OptionEvent.all(bearer)
events = OptionEvent.all(client)
13 changes: 8 additions & 5 deletions examples/option_positions_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Option,
OptionPosition
)
from fast_arrow.client import Client


#
# get the authentication configs
Expand All @@ -17,15 +19,16 @@


#
# login and get the bearer token
# initialize and authenticate Client
#
bearer = Auth.login_oauth2(username, password)
client = Client(username, password)
client.client.authenticate()


#
# fetch option_positions
#
all_option_positions = OptionPosition.all(bearer)
all_option_positions = OptionPosition.all(client)


#
Expand All @@ -37,10 +40,10 @@
#
# append marketdata to each position
#
option_position_with_marketdata = OptionPosition.mergein_marketdata_list(bearer, open_option_positions)
option_position_with_marketdata = OptionPosition.mergein_marketdata_list(client, open_option_positions)


#
# append instrument data to each position
#
option_position_with_marketdata_and_instrument_data = OptionPosition.mergein_instrumentdata_list(bearer, option_position_with_marketdata)
option_position_with_marketdata_and_instrument_data = OptionPosition.mergein_instrumentdata_list(client, option_position_with_marketdata)
9 changes: 6 additions & 3 deletions examples/stock_positions_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Auth,
StockPosition
)
from fast_arrow.client import Client


#
Expand All @@ -16,14 +17,16 @@


#
# login and get the bearer token
# initialize and authenticate Client
#
bearer = Auth.login_oauth2(username, password)
client = Client(username, password)
client.client.authenticate()


#
# fetch stock positions
#
all_stock_positions = StockPosition.all(bearer)
all_stock_positions = StockPosition.all(client)


#
Expand Down
60 changes: 30 additions & 30 deletions fast_arrow/api_requestor.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import requests


def gen_headers(bearer):
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
if bearer:
headers["Authorization"] = "Bearer {0}".format(bearer)
return headers


def get(url=None, client=None, params={}):
"""
Execute HTTP GET
"""
bearer = (client.access_token if client else None)
headers = gen_headers(bearer)
res = requests.get(url, headers=headers, params=params, timeout=15)
return res.json()


def post(url=None, client=None, payload=None):
"""
Execute HTTP POST
"""
bearer = (client.access_token if client else None)
headers = gen_headers(bearer)
res = requests.post(url, headers=headers, data=payload, timeout=15)
return res.json()
# def gen_headers(bearer):
# headers = {
# "Accept": "*/*",
# "Accept-Encoding": "gzip, deflate",
# "Accept-Language": "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5",
# "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
# }
# if bearer:
# headers["Authorization"] = "Bearer {0}".format(bearer)
# return headers
#
#
# def get(url=None, client=None, params={}):
# """
# Execute HTTP GET
# """
# bearer = (client.access_token if client else None)
# headers = gen_headers(bearer)
# res = requests.get(url, headers=headers, params=params, timeout=15)
# return res.json()
#
#
# def post(url=None, client=None, payload=None):
# """
# Execute HTTP POST
# """
# bearer = (client.access_token if client else None)
# headers = gen_headers(bearer)
# res = requests.post(url, headers=headers, data=payload, timeout=15)
# return res.json()
22 changes: 11 additions & 11 deletions fast_arrow/resources/option.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from fast_arrow.api_requestor import get
# from fast_arrow.api_requestor import get
from fast_arrow.util import chunked_list
from fast_arrow.resources.option_marketdata import OptionMarketdata


class Option(object):

@classmethod
def fetch_by_ids(cls, bearer, ids):
def fetch_by_ids(cls, client, ids):
results = []
params = {"ids": ",".join(ids)}
request_url = "https://api.robinhood.com/options/instruments/"
data = get(request_url, bearer=bearer, params=params)
data = client.get(request_url, params=params)
results = data["results"]
while data["next"]:
data = get(data["next"], bearer=bearer)
Expand All @@ -20,16 +20,16 @@ def fetch_by_ids(cls, bearer, ids):

# deprecate me
@classmethod
def fetch(cls, bearer, _id):
def fetch(cls, client, _id):
"""
fetch by instrument id
"""
return cls.fetch_list(bearer, [_id])[0]
return cls.fetch_list(client, [_id])[0]


# deprecate me
@classmethod
def fetch_list(cls, bearer, ids):
def fetch_list(cls, client, ids):
"""
fetch instruments by ids
"""
Expand All @@ -39,7 +39,7 @@ def fetch_list(cls, bearer, ids):
for _ids in chunked_list(ids, 50):

params = {"ids": ",".join(_ids)}
data = get(request_url, bearer=bearer, params=params)
data = client.get(request_url, params=params)
partial_results = data["results"]

while data["next"]:
Expand All @@ -51,7 +51,7 @@ def fetch_list(cls, bearer, ids):


@classmethod
def in_chain(cls, bearer, chain_id, expiration_dates=[]):
def in_chain(cls, client, chain_id, expiration_dates=[]):
"""
fetch all option instruments in an options chain
- expiration_dates = optionally scope
Expand All @@ -63,7 +63,7 @@ def in_chain(cls, bearer, chain_id, expiration_dates=[]):
"expiration_dates": ",".join(expiration_dates)
}

data = get(request_url, bearer=bearer, params=params)
data = client.get(request_url, params=params)
results = data['results']

while data['next']:
Expand All @@ -73,9 +73,9 @@ def in_chain(cls, bearer, chain_id, expiration_dates=[]):


@classmethod
def mergein_marketdata_list(cls, bearer, options):
def mergein_marketdata_list(cls, client, options):
ids = [x["id"] for x in options]
mds = OptionMarketdata.quotes_by_instrument_ids(bearer, ids)
mds = OptionMarketdata.quotes_by_instrument_ids(client, ids)
mds = [x for x in mds if x]

results = []
Expand Down
7 changes: 3 additions & 4 deletions fast_arrow/resources/option_chain.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from fast_arrow.api_requestor import get

# from fast_arrow.api_requestor import get

class OptionChain(object):

@classmethod
def fetch(cls, bearer, id):
def fetch(cls, client, id):
"""
fetch option chain for instrument
"""
Expand All @@ -14,5 +13,5 @@ def fetch(cls, bearer, id):
"state": "active",
"tradability": "tradable"
}
data = get(url, bearer=bearer, params=params)
data = client.get(url, params=params)
return data['results'][0]
10 changes: 5 additions & 5 deletions fast_arrow/resources/option_event.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from fast_arrow.api_requestor import get
# from fast_arrow.api_requestor import get
from fast_arrow import util
from fast_arrow.resources.option import Option


class OptionEvent(object):

@classmethod
def all(cls, bearer):
def all(cls, client):
"""
fetch all option positions
"""
url = 'https://api.robinhood.com/options/events/'
params = { }
data = get(url, bearer=bearer, params=params)
data = client.get(url, params=params)
results = data["results"]
while data["next"]:
data = get(data["next"], token)
Expand All @@ -21,10 +21,10 @@ def all(cls, bearer):


@classmethod
def mergein_instrumentdata_list(cls, bearer, option_events):
def mergein_instrumentdata_list(cls, client, option_events):
results = []
ids = [util.get_last_path(oe['option']) for oe in option_events]
idatas = Option.fetch_by_ids(bearer, ids)
idatas = Option.fetch_by_ids(client, ids)
for oe in option_events:
idata = [x for x in idatas if x['url'] == oe['option']][0]
merge_me = {
Expand Down
Loading

0 comments on commit f2f583f

Please sign in to comment.