forked from adversary-org/python-btcmarkets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderHistory.py
More file actions
92 lines (73 loc) · 2.86 KB
/
OrderHistory.py
File metadata and controls
92 lines (73 loc) · 2.86 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
import base64
import hashlib
import hmac
import json
import time
from collections import OrderedDict
from pprint import pprint
import requests
from config import apikey_public
from config import apikey_secret
from config import domain
# Define Global Vars to use
uri = "/order/trade/history"
url = domain + uri
api_secret_key = apikey_secret.encode("utf-8")
std_secret_key = base64.standard_b64decode(api_secret_key)
pub_key = apikey_public.encode("utf-8")
body = OrderedDict([("currency", "AUD"),
("instrument", "ETH"),
("limit", 3),
("since", 0)])
def build_headers(path, api_pub_key, secret_key):
"""Build timestamp, format and encode everything, and construct string to
sign with api key. Use HmacSHA512 algorithm in order to sign.
Lastly build the headers to send... In order to ensure the correct order
of key value pairs in the JSON payload, build an ordered dictionary out
of a list of tuples.
"""
# Build timestamp
tstamp = time.time()
ctstamp = int(tstamp * 1000) # or int(tstamp * 1000) or round(tstamp * 1000)
str_ctstamp = str(ctstamp)
# Build and sign to construct body
sbody = path + "\n" + str_ctstamp + "\n" + json.dumps(body, separators=(',', ':'))
print(repr(sbody))
# dictionary string
rbody = sbody.encode("utf-8")
rsig = hmac.new(secret_key, rbody, hashlib.sha512)
bsig = base64.standard_b64encode(rsig.digest()).decode("utf-8")
print(api_pub_key)
# Construct header list of key value pairs
headers_list = OrderedDict([("Accept", "application/json"),
("Accept-Charset", "UTF-8"),
("Content-Type", "application/json"),
("apikey", api_pub_key),
("timestamp", str_ctstamp),
("signature", bsig)])
# http://docs.python-requests.org/en/master/user/advanced/#header-ordering
# maybe returning the ordered list to requests may provide headers with ordering
return headers_list
def order_history():
""" Build the request body by invoking header function with config
params specified as global variables at top and formatting in json the body as well
returns a response object from requests
"""
res = build_headers(uri, pub_key, std_secret_key)
r = requests.post(url, headers=res, json=body)
return r
def print_history():
"""
prints the order history, json loads converts to a dictionary, json dumps converts to text
"""
response = order_history()
# print(json.dumps(response.json(), indent=2))
pprint(json.loads(response.text), indent=1, compact=False)
def main():
"""
TODO: Add in functionality to pass options for the CLI.
"""
print_history()
if __name__ == "__main__":
main()