Skip to content

Instantly share code, notes, and snippets.

@trAve3113r
Created February 9, 2018 09:28
Show Gist options
  • Save trAve3113r/1423df36d3f98f377bd7351309430590 to your computer and use it in GitHub Desktop.
Save trAve3113r/1423df36d3f98f377bd7351309430590 to your computer and use it in GitHub Desktop.
"""
Updating http://bit.ly/2nRmaXc to
v2 oanda API
"""
class API(EndPointsMixin,object):
"""updated 'API' class """
def __init__(self,
environment=None,
access_token=None,
headers=None
):
if environment == 'sandbox':
self.api_url = 'http://api-sandbox.oanda.com'
elif environment == 'practice':
self.api_url = 'https://api-fxpractice.oanda.com'
elif environment == 'live':
self.api_url = 'https://api-fxtrade.oanda.com'
else:
pass
self.access_token = access_token
#self.client = requests.Session()
self.headers = {'Authorization' : "Bearer {}".format(self.token),
'X-Accept-Datetime-Format' : 'unix',
}
def request(self,endpoint,method=None,params=None):
try:
s = requests.Session()
url = '%s/%s' % (self.api_url, endpoint)
method = method.lower()
params = params or {}
req = requests.Request(method,url,headers=self.headers,params=params)
prep = req.prepare()
resp = s.send(prep,)
except Exception as e:
s.close()
print("Caught exception when connecting to Oanda API\n" + str(e))
return resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment