forked from dfkthf125/api-v1-client-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
38 lines (31 loc) · 1.09 KB
/
util.py
File metadata and controls
38 lines (31 loc) · 1.09 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
from .exceptions import *
import sys
BASE_URL = "https://blockchain.info/"
TIMEOUT = 10
py_version = sys.version_info[0]
if py_version >= 3:
# Python 3.0 and later
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
else:
# Python 2.x
from urllib2 import urlopen
from urllib2 import HTTPError
from urllib import urlencode
def call_api(resource, data=None, base_url=None):
base_url = BASE_URL if base_url is None else base_url
try:
payload = None if data is None else urlencode(data)
if py_version >= 3 and payload is not None:
payload = payload.encode('UTF-8')
response = urlopen(base_url + resource, payload, timeout=TIMEOUT).read()
return handle_response(response)
except HTTPError as e:
raise APIException(handle_response(e.read()), e.code)
def handle_response(response):
# urllib returns different types in Python 2 and 3 (str vs bytes)
if isinstance(response, str):
return response
else:
return response.decode('utf-8')