-
Notifications
You must be signed in to change notification settings - Fork 1
/
urllib_utils.py
32 lines (25 loc) · 1.01 KB
/
urllib_utils.py
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
import json
import urllib3
from urllib3.exceptions import InsecureRequestWarning
import settings
from exceptions import EncodingException
from utils import get_http_headers, decode_bytes
def create_gist(description, filename, content, public=True):
decoded_content = decode_bytes(content)
if not decoded_content:
raise EncodingException('Cannot detect file encoding. Is this a binary file?')
encoded_body = {
"description": description,
"public": 'true' if public else 'false',
"files": {
filename: {
"content": decoded_content
}
}
}
r = get_request_contents('POST', settings.GITHUB_API_URL, get_http_headers(), encoded_body)
return json.loads(r.decode('utf-8')).get('html_url')
def get_request_contents(method, url, headers=None, body=None):
http = urllib3.PoolManager()
urllib3.disable_warnings(InsecureRequestWarning)
return http.request(method=method, url=url, headers=headers, body=json.dumps(body)).data