Skip to content

Commit

Permalink
clean up client code.
Browse files Browse the repository at this point in the history
  • Loading branch information
westonplatter committed Aug 23, 2018
1 parent 8c268a6 commit dab3c76
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions fast_arrow/client.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#
# @TODO this list
#
# here's the idea.
# - [x] create an instance of client
# - [x] pass in username/password
# - [x] authenticate, save bearer token and refresh token
# - [x] pass this to http_requestor for gets and posts
# - [x] if we get a token expired issue, refresh and retry
# - [x] adjust all resources to use client instead of token/bearer
# - [x] allow init without auth means
#
#

import requests
from fast_arrow.resources.user import User
from fast_arrow.exceptions import AuthenticationError
Expand All @@ -25,13 +11,16 @@ class Client(object):

def __init__(self, **kwargs):
self.options = kwargs
self.access_token = None
self.refresh_token = None
self.mfa_code = None
self.scope = None
self.authenticated = False
self.access_token = None
self.refresh_token = None
self.mfa_code = None
self.scope = None
self.authenticated = False

def authenticate(self):
"""
Authenticate using data in `options`
"""
if "username" in self.options and "password" in self.options:
return self.login_oauth2(self.options["username"], self.options["password"])

Expand Down Expand Up @@ -62,15 +51,14 @@ def get(self, url=None, params=None):
attempts -= 1
else:
attempts = False
raise NotImplementedError()


def post(self, url=None, payload=None):
"""
Execute HTTP POST
"""
headers = self._gen_headers(self.access_token)
attempts = 2
attempts = 1
while attempts:
try:
res = requests.post(url, headers=headers, data=payload, timeout=15)
Expand All @@ -83,10 +71,12 @@ def post(self, url=None, payload=None):
attempts -= 1
else:
attempts = False
raise NotImplementedError()


def _gen_headers(self, bearer):
"""
Generate headders, adding in Oauth2 bearer token if present
"""
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
Expand All @@ -99,6 +89,9 @@ def _gen_headers(self, bearer):


def login_oauth2(self, username, password):
"""
Login using username and password
"""
data = {
"grant_type": "password",
"scope": "internal",
Expand All @@ -118,14 +111,17 @@ def login_oauth2(self, username, password):


def relogin_oauth2(self):
"""
(Re)login using the Oauth2 refresh token
"""
url = "https://api.robinhood.com/oauth2/token/"
data = {
"grant_type": "refresh_token",
"refresh_token": self.refresh_token,
"scope": "internal",
"client_id": CLIENT_ID,
"expires_in": 86400,
}
url = "https://api.robinhood.com/oauth2/token/"
res = self.post(url, payload=data)
self.access_token = res["access_token"]
self.refresh_token = res["refresh_token"]
Expand All @@ -137,14 +133,13 @@ def relogin_oauth2(self):

def logout_oauth2(self):
"""
Logout for given token
Logout for given Oauth2 bearer token
"""
url = 'https://api.robinhood.com/oauth2/revoke_token/'
url = "https://api.robinhood.com/oauth2/revoke_token/"
data = {
"client_id": CLIENT_ID,
"token": self.refresh_token,
}
headers = self._gen_headers(self.access_token)
res = self.post(url, payload=data)
result = (True if res == None else False)
self.authenticated = False
Expand Down

0 comments on commit dab3c76

Please sign in to comment.