-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import urllib3 | ||
import json | ||
import logging | ||
|
||
from collections import namedtuple | ||
|
||
Response = namedtuple('Response', ['data', 'status_code', 'ok']) | ||
|
||
log = logging.getLogger(__name__) | ||
http = urllib3.PoolManager() | ||
|
||
|
||
def requests_post_json(url, data, headers=None): | ||
if not headers: | ||
headers = {'Content-Type': 'application/json'} | ||
log.warning("Sending POST to %s / %s / %s", url, headers, data) | ||
|
||
response = http.request( | ||
'POST', | ||
url, | ||
body=json.dumps(data), | ||
headers=headers, | ||
) | ||
|
||
log.debug("Response [status: %s] data: %s", response.status, response.data) | ||
|
||
try: | ||
json_data = json.loads(response.data.decode('utf-8')) | ||
except: | ||
json_data = response.data | ||
|
||
return Response( | ||
data=json_data, | ||
status_code=response.status, | ||
ok=str(response.status)[0] == '2' | ||
) | ||
|
||
|
||
def requests_get_json(url, headers): | ||
if not headers: | ||
headers = {'Content-Type': 'application/json'} | ||
log.warning("Sending GET to %s / headers: %s", url, headers) | ||
|
||
|
||
response = http.request( | ||
'GET', | ||
url, | ||
headers=headers, | ||
) | ||
try: | ||
data = json.loads(response.data.decode('utf-8')) | ||
except: # TODO: exception more restrictive | ||
data = response.data | ||
|
||
return Response( | ||
data=data, | ||
status_code=response.status, | ||
ok=str(response.status)[0] == "2" | ||
) | ||
|