-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathghApiClient.py
executable file
·58 lines (53 loc) · 1.64 KB
/
ghApiClient.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
import os
import time
import urllib2
import httplib
import json
GH_BASE_URL = "https://api.github.com/"
GH_TOKEN = os.environ['GH_TOKEN']
GH_AUTH = "Bearer %s" % GH_TOKEN
def readUrl(name):
try:
request = urllib2.Request(GH_BASE_URL + name)
request.add_header("Authorization", GH_AUTH)
content = urllib2.urlopen(request).read()
jcont = json.loads(content)
return jcont;
except urllib2.HTTPError as e:
print ('HTTPError = ' + str(e.code))
raise e
except urllib2.URLError as e:
print ('URLError = ' + str(e.reason))
raise e
except httplib.HTTPException as e:
print ('HTTPException = ' + str(e))
raise e
except Exception:
import traceback
print ('generic exception: ' + traceback.format_exc())
raise IOError
def postUrl(name, body):
global GH_BASE_URL
try:
time.sleep(0.05)
request = urllib2.Request(GH_BASE_URL + name)
request.add_header("Authorization", GH_AUTH)
request.add_header("Accept", "application/vnd.github.v3+json")
content = urllib2.urlopen(request, body).read()
jcont = json.loads(content)
return jcont;
except urllib2.HTTPError as e:
print ('HTTPError = ' + str(e.code))
print (str(e))
raise e
except urllib2.URLError as e:
print ('URLError = ' + str(e.reason))
raise e
except httplib.HTTPException as e:
print ('HTTPException = ' + str(e))
raise e
except Exception:
import traceback
print ('generic exception: ' + traceback.format_exc())
raise IOError