Issues (15)

examples/test_get_ledgers.py (3 issues)

1
2
import bitpay.bitpay_key_utils as bku
3
from bitpay.bitpay_client import *
4
import pprint
5
import requests
6
import json
7
import re
8
import os.path
9
10
#API_HOST = "https://bitpay.com" #for production, live bitcoin
11
API_HOST = "https://test.bitpay.com" #for testing, testnet bitcoin
12
KEY_FILE = "/tmp/key.priv"
13
TOKEN_FILE = "/tmp/token.priv"
14
15
# check if there is a preexisting key file
16
if os.path.isfile(KEY_FILE):
17
    f = open(KEY_FILE, 'r')
18
    key = f.read()
19
    f.close()
20
    print("Creating a bitpay client using existing private key from disk.")
21
else:
22
    key = bku.generate_pem()
23
    f = open(KEY_FILE, 'w')
24
    f.write(key)
25
    f.close()
26
27
client = Client(API_HOST, False, key)
28
29 View Code Duplication
def fetch_token(facade):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
30
    if os.path.isfile(TOKEN_FILE + facade):
31
        f = open(TOKEN_FILE + facade, 'r')
32
        token = f.read()
33
        f.close()
34
        print("Reading " + facade + " token from disk.")
35
        #global client
36
        #client = Client(API_HOST, False, key, {facade: token})
37
        client.tokens[facade] = token
38
    else:
39
        pairingCode = client.create_token(facade)
40
        print("Creating " + facade + " token.")
41
        print("Please go to:  %s/dashboard/merchant/api-tokens  then enter \"%s\" then click the \"Find\" button, then click \"Approve\"" % (API_HOST, pairingCode))
42
        raw_input("When you've complete the above, hit enter to continue...")
43
        print("token is: %s" % client.tokens[facade])
44
        f = open(TOKEN_FILE + facade, 'w')
45
        f.write(client.tokens[facade])
46
        f.close() 
47
48 View Code Duplication
def get_from_bitpay_api(client, uri, token):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
49
    if "?" in uri:    
50
        payload = "&token=%s" % token
51
    else:
52
        payload = "?token=%s" % token
53
    xidentity = bku.get_compressed_public_key_from_pem(client.pem)
54
    xsignature = bku.sign(uri + payload, client.pem)
55
    headers = {"content-type": "application/json",
56
                "X-Identity": xidentity,
57
                "X-Signature": xsignature, "X-accept-version": "2.0.0"}
58
    try:
59
        pp.pprint(headers)
60
        print(uri + payload)
61
        response = requests.get(uri + payload, headers=headers, verify=client.verify)
62
    except Exception as pro:
63
        raise BitPayConnectionError(pro.args)
64
    if response.ok:
65
        response = response.json()
66
        return response
67
    client.response_error(response)
68
69
"""
70
POST to any resource
71
Make sure to include the proper token in the params
72
"""
73 View Code Duplication
def post_to_bitpay_api(client, uri, resource, params):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
74
    payload = json.dumps(params)
75
    print("payload = " + payload)
76
    uri = uri + "/" + resource
77
    xidentity = key_utils.get_compressed_public_key_from_pem(client.pem)
78
    xsignature = key_utils.sign(uri + payload, client.pem)
79
    headers = {"content-type": "application/json",
80
               "accept": "application/json", "X-Identity": xidentity,
81
               "X-Signature": xsignature, "X-accept-version": "2.0.0"}
82
    try:
83
        response = requests.post(uri, data=payload, headers=headers,
84
                                 verify=client.verify)
85
    except Exception as pro:
86
        raise BitPayConnectionError(pro.args)
87
    if response.ok:
88
        return response.json()['data']
89
    client.response_error(response)
90
91
fetch_token("merchant")
92
93
pp = pprint.PrettyPrinter(indent=4)
94
95
#Now we assume that the pairing code that we generated along with the crypto keys is paired with your merchant account
96
97
print("reading ledger balances")
98
token = client.tokens['merchant']
99
ledger_balances = get_from_bitpay_api(client, client.uri + "/ledgers/", token)
100
pp.pprint(ledger_balances)
101
102
print("reading ledger entries")
103
token = client.tokens['merchant']
104
ledger_entries = get_from_bitpay_api(client, client.uri + "/ledgers/USD?startDate=2017-09-01&endDate=2017-09-20", token)
105
pp.pprint(ledger_entries)
106