Skip to content

Commit 30957fb

Browse files
committed
Added headers in restcli
1 parent c3ac659 commit 30957fb

File tree

2 files changed

+122
-161
lines changed

2 files changed

+122
-161
lines changed

src/bitpay/utils/rest_cli.py

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class RESTcli:
14-
__client = ""
14+
__headers = {}
1515
__baseurl = ""
1616
__eckey = ""
1717
__identity = ""
@@ -25,19 +25,16 @@ def __init__(self, environment, eckey, proxy=None):
2525

2626
def init(self):
2727
try:
28-
config = {
29-
"base_url": self.__baseurl,
30-
"defaults": {
31-
"headers": {
32-
"x-accept-version": env.BITPAYAPIVERSION,
33-
"x-bitpay-plugin-info": env.BITPAYPLUGININFO,
34-
"x-bitpay-api-frame": env.BITPAYAPIFRAME,
35-
"x-bitpay-api-frame-version": env.BITPAYAPIFRAMEVERSION,
36-
},
37-
},
28+
self.__headers = {
29+
"x-accept-version": env.BITPAYAPIVERSION,
30+
"x-bitpay-plugin-info": env.BITPAYPLUGININFO,
31+
"x-bitpay-api-frame": env.BITPAYAPIFRAME,
32+
"x-bitpay-api-frame-version": env.BITPAYAPIFRAMEVERSION,
33+
"content-type": "application/json",
34+
"X-accept-version": "2.0.0"
3835
}
3936
if self.__proxy != "":
40-
config["proxy"] = self.__proxy
37+
self.__headers["proxy"] = self.__proxy
4138

4239
except BitPayException as e:
4340
print(e)
@@ -53,19 +50,11 @@ def post(self, uri, form_data, signature_required=True):
5350
full_url = self.__baseurl + uri
5451
form_data = json.dumps(form_data)
5552

56-
headers = {
57-
"content-type": "application/json",
58-
"X-accept-version": "2.0.0",
59-
"X-bitpay-plugin-info": env.BITPAYPLUGININFO,
60-
"X-bitpay-api-frame": env.BITPAYAPIFRAME,
61-
"X-bitpay-api-frame-version": env.BITPAYAPIFRAMEVERSION,
62-
}
63-
6453
if signature_required:
65-
headers["x-signature"] = sign(full_url + form_data, self.__eckey)
66-
headers["x-identity"] = get_compressed_public_key_from_pem(self.__eckey)
54+
self.__headers["x-signature"] = sign(full_url + form_data, self.__eckey)
55+
self.__headers["x-identity"] = get_compressed_public_key_from_pem(self.__eckey)
6756

68-
response = requests.post(full_url, data=form_data, headers=headers)
57+
response = requests.post(full_url, data=form_data, headers=self.__headers)
6958
json_response = self.response_to_json_string(response)
7059
return json_response
7160

@@ -81,19 +70,11 @@ def get(self, uri, parameters=None, signature_required=True):
8170
if parameters is not None:
8271
full_url = "%s?%s" % (full_url, urllib.parse.urlencode(parameters))
8372

84-
headers = {
85-
"content-type": "application/json",
86-
"X-accept-version": "2.0.0",
87-
"X-bitpay-plugin-info": env.BITPAYPLUGININFO,
88-
"X-bitpay-api-frame": env.BITPAYAPIFRAME,
89-
"X-bitpay-api-frame-version": env.BITPAYAPIFRAMEVERSION,
90-
}
91-
9273
if signature_required:
93-
headers["x-signature"] = sign(full_url, self.__eckey)
94-
headers["x-identity"] = get_compressed_public_key_from_pem(self.__eckey)
74+
self.__headers["x-signature"] = sign(full_url, self.__eckey)
75+
self.__headers["x-identity"] = get_compressed_public_key_from_pem(self.__eckey)
9576

96-
response = requests.get(full_url, headers=headers)
77+
response = requests.get(full_url, headers=self.__headers)
9778
json_response = self.response_to_json_string(response)
9879
return json_response
9980

@@ -109,20 +90,10 @@ def delete(self, uri, parameters=None):
10990
if parameters is not None:
11091
full_url = "%s?%s" % (full_url, urllib.parse.urlencode(parameters))
11192

112-
xidentity = get_compressed_public_key_from_pem(self.__eckey)
113-
xsignature = sign(full_url, self.__eckey)
93+
self.__headers["x-signature"] = sign(full_url, self.__eckey)
94+
self.__headers["x-identity"] = get_compressed_public_key_from_pem(self.__eckey)
11495

115-
headers = {
116-
"content-type": "application/json",
117-
"X-Identity": xidentity,
118-
"X-Signature": xsignature,
119-
"X-accept-version": "2.0.0",
120-
"X-bitpay-plugin-info": env.BITPAYPLUGININFO,
121-
"X-bitpay-api-frame": env.BITPAYAPIFRAME,
122-
"X-bitpay-api-frame-version": env.BITPAYAPIFRAMEVERSION,
123-
}
124-
125-
response = requests.delete(full_url, headers=headers)
96+
response = requests.delete(full_url, headers=self.__headers)
12697
json_response = self.response_to_json_string(response)
12798
return json_response
12899

@@ -135,26 +106,16 @@ def update(self, uri, form_data):
135106
full_url = self.__baseurl + uri
136107
form_data = json.dumps(form_data)
137108

138-
xidentity = get_compressed_public_key_from_pem(self.__eckey)
139-
xsignature = sign(full_url + form_data, self.__eckey)
140-
141-
headers = {
142-
"content-type": "application/json",
143-
"X-Identity": xidentity,
144-
"X-Signature": xsignature,
145-
"X-accept-version": "2.0.0",
146-
"X-bitpay-plugin-info": env.BITPAYPLUGININFO,
147-
"X-bitpay-api-frame": env.BITPAYAPIFRAME,
148-
"X-bitpay-api-frame-version": env.BITPAYAPIFRAMEVERSION,
149-
}
109+
self.__headers["x-signature"] = sign(full_url + form_data, self.__eckey)
110+
self.__headers["x-identity"] = get_compressed_public_key_from_pem(self.__eckey)
150111

151-
response = requests.put(full_url, data=form_data, headers=headers)
112+
response = requests.put(full_url, data=form_data, headers=self.__headers)
152113
json_response = self.response_to_json_string(response)
153114
return json_response
154115

155116
def response_to_json_string(self, response):
156-
# if not response:
157-
# raise BitPayException("Error: HTTP response is null")
117+
if not response:
118+
raise BitPayException("Error: HTTP response is null")
158119

159120
response_obj = response.json()
160121
if "status" in response_obj:

0 commit comments

Comments
 (0)