Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0566c9e
commit eb19319
Showing
18 changed files
with
17,171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
OANDA v20 API Change Log | ||
|
||
Version 3.0.1 (June 09, 2016) | ||
|
||
* Added TransactRejectReason enum | ||
|
||
|
||
Version 3.0.0 (April 26, 2016) | ||
|
||
* Initial Release | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 OANDA | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include LICENSE.txt | ||
include ChangeLog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
The OANDA v20 REST API provides programmatic access to OANDA's next generation | ||
v20 trading engine. | ||
|
||
Installation | ||
############ | ||
|
||
Install using pip:: | ||
|
||
pip install v20 | ||
|
||
If you don't wish to use pip:: | ||
|
||
git clone https://github.com/oanda/v20-python.git | ||
|
||
Documentation | ||
############# | ||
|
||
For documentation, usage and examples, see: http://developer.oanda.com/rest-live-v20/introduction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ---------------------------------------------------------------------- | ||
# setup.py -- v20 setup script | ||
# | ||
# Copyright (C) 2016, OANDA Corporation. | ||
# ---------------------------------------------------------------------- | ||
|
||
import os | ||
import codecs | ||
|
||
from setuptools import setup | ||
|
||
HERE = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
|
||
def read(*parts): | ||
""" | ||
Build an absolute path from *parts* and and return the contents of the | ||
resulting file. Assume UTF-8 encoding. | ||
""" | ||
with codecs.open(os.path.join(HERE, *parts), "rb", "utf-8") as f: | ||
return f.read() | ||
|
||
|
||
def main(): | ||
# setup dictionary | ||
setup_options = { | ||
"name": "v20", | ||
"version": "3.0.1.0", | ||
"description": "OANDA v20 bindings for Python", | ||
"long_description": read("README.rst"), | ||
"author": "OANDA Corporation", | ||
"author_email": "api@oanda.com", | ||
"url": "https://github.com/oanda/v20-python", | ||
"license": "MIT", | ||
"install_requires": ['requests'], | ||
"packages": ["v20"], | ||
"data_files": [('', ['LICENSE.txt', 'ChangeLog'])], | ||
"platforms": ["any"], | ||
"zip_safe": True, | ||
"classifiers": [ | ||
'Development Status :: 5 - Production/Stable', | ||
'Environment :: Console', | ||
'Intended Audience :: Developers', | ||
'License :: Other/Proprietary License', | ||
'Natural Language :: English', | ||
'Operating System :: OS Independent', | ||
'Topic :: Software Development', | ||
'Topic :: Software Development :: Libraries', | ||
'Topic :: Utilities' | ||
] | ||
} | ||
|
||
# Run the setup | ||
setup(**setup_options) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import requests | ||
import account | ||
import transaction | ||
import authorization | ||
import trade | ||
import pricing | ||
import position | ||
import order | ||
from response import Response | ||
|
||
class AuthenticationError(Exception): | ||
def __init__(self, username, status, reason): | ||
self.username = username | ||
self.status = status | ||
self.reason = reason | ||
|
||
def __str__(self): | ||
return "Authenticating {} resulted in a {} ({}) response".format( | ||
self.username, | ||
self.status, | ||
self.reason | ||
) | ||
|
||
class Context(object): | ||
def __init__(self, hostname, port, ssl=False): | ||
"""Create an API context for a specific host/port""" | ||
|
||
# Currnet username for the context | ||
self.username = None | ||
|
||
# Context headers to add to every request sent to the server | ||
self._headers = { | ||
"Content-Type" : "application/json", | ||
"User-Agent" : "OANDA/3.0.1 (client; python)" | ||
} | ||
|
||
# Current authentication token | ||
self.token = None | ||
|
||
# The list of accounts accessible for the current token | ||
self.token_account_ids = [] | ||
|
||
# The base URL for every request made using the context | ||
self._base_url = "http{}://{}:{}".format( | ||
"s" if ssl else "", | ||
hostname, | ||
port | ||
) | ||
|
||
# The session used for communicating with the REST server | ||
self._session = requests.Session() | ||
|
||
self.account = account.EntitySpec(self) | ||
self.transaction = transaction.EntitySpec(self) | ||
self.authorization = authorization.EntitySpec(self) | ||
self.trade = trade.EntitySpec(self) | ||
self.pricing = pricing.EntitySpec(self) | ||
self.position = position.EntitySpec(self) | ||
self.order = order.EntitySpec(self) | ||
|
||
def authenticate(self, username, password): | ||
"""Log in using a username and password. Store the authorization token | ||
and fetch the list of Accounts accessible for that token""" | ||
|
||
response = self.authorization.login( | ||
username=username, | ||
password=password | ||
) | ||
|
||
if response.status != 200: | ||
raise AuthenticationError( | ||
username, | ||
response.status, | ||
response.reason | ||
) | ||
|
||
self.token = response.body.get("token") | ||
|
||
self.set_header( | ||
'Authorization', | ||
"Bearer {}".format( | ||
self.token | ||
) | ||
) | ||
|
||
self.username = username | ||
|
||
response = self.account.list() | ||
|
||
self.token_account_ids = response.body.get("accounts") | ||
|
||
def set_header(self, key, value): | ||
self._headers[key] = value | ||
|
||
def delete_header(self, key): | ||
if key in self._headers: | ||
del self._headers[key] | ||
|
||
def request(self, request): | ||
|
||
url = "{}{}".format(self._base_url, request.path) | ||
|
||
http_response = self._session.request( | ||
request.method, | ||
url, | ||
headers=self._headers, | ||
params=request.params, | ||
data=request.body, | ||
stream=request.stream | ||
) | ||
|
||
response = Response( | ||
request.method, | ||
http_response.url, | ||
http_response.status_code, | ||
http_response.reason, | ||
http_response.headers.get("content-type", None), | ||
) | ||
|
||
if request.stream: | ||
response.set_line_parser( | ||
request.line_parser | ||
) | ||
|
||
response.set_lines( | ||
http_response.iter_lines(1) | ||
) | ||
else: | ||
response.set_raw_body(http_response.text) | ||
|
||
return response |
Oops, something went wrong.