-
Notifications
You must be signed in to change notification settings - Fork 8
/
GithubClient.py
44 lines (36 loc) · 1.37 KB
/
GithubClient.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
import json
from ConfigParser import ConfigParser
import os.path
import requests
class GithubClient:
_BASE_URL = "https://api.github.com"
_use_raw_response = False
username = None
password = None
def __init__(self, use_raw_response = False):
self._use_raw_response = use_raw_response
# Get the Credentials..so I'm not storing them in this repo
try:
gitconfig = ConfigParser()
gitconfig.readfp(open(os.path.expanduser("~/.gitconfig")))
self.username = gitconfig.get("github", "user")
self.password = gitconfig.get("github", "password")
except:
# You should never use a bare except, except here.
pass
def request(self, method, url, *args, **kwargs):
"""
This is basically requests.api.request
"""
if self.username and self.password:
kwargs["auth"] = (self.username, self.password)
# args[1] would be the data
if len(args) > 1:
args[1] = json.dumps(args[1])
elif kwargs.get("data"):
kwargs["data"] = json.dumps(kwargs["data"])
url = "%s%s" % (self._BASE_URL, url)
response = requests.request(method, url, *args, **kwargs)
if self._use_raw_response:
return response
return json.loads(response.content)