-
Notifications
You must be signed in to change notification settings - Fork 5
/
fxbattleclient.py
56 lines (43 loc) · 1.48 KB
/
fxbattleclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from urllib.request import urlopen
from urllib.error import URLError
import json
class FxClientError(Exception):
def __init__(self, message='Generic error'):
self.message = message
class FxClient:
"""
FxClient(endpoint, api_key)
args:
endpoint: the full http url to the exchange incl port. no ending slash
e.g. 'https://127.0.0.1:8080'
api_key : the key
e.g. 'api_key1'
raises:
connection errors alone will be raised as FxClientError
all failures returned by the api will be returned
e.g. res = client.buy("GBPUSD", -1)
will _not_ raise FxClientError
but ("error" in res) == True
"""
def __init__(self, endpoint, api_key):
self._endpoint = endpoint
self._api_key = api_key
#private
def _get_json(self, url):
try:
return json.loads(urlopen(url).read().decode('latin-1'))
except(URLError):
raise FxClientError()
#public interface
def buy(self, ccy_pair, amount):
url = "{}/trade/{}/buy/{}/{}".format(self._endpoint, self._api_key, ccy_pair, amount)
return self._get_json(url)
def sell(self, ccy_pair, amount):
url = "{}/trade/{}/sell/{}/{}".format(self._endpoint, self._api_key, ccy_pair, amount)
return self._get_json(url)
def account(self):
url = "{}/account/{}".format(self._endpoint, self._api_key)
return self._get_json(url)
def market(self):
url = "{}/market".format(self._endpoint)
return self._get_json(url)