-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.py
50 lines (34 loc) · 1.34 KB
/
github.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
from typing import Union, Dict, List, Any
from requests.auth import HTTPBasicAuth
from rest import RestClient
GITHUB_DEFAULT_HEADERS = {'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'python-requests'}
JSONType = Union[None, bool, int, float, str, List[Any], Dict[str, Any]]
class GitHubError(Exception):
"""
Error from GitHub REST API.
"""
class GitHubClient(RestClient):
BASE_URL = 'https://api.github.com'
def __init__(self, username: str, token: str, headers: Dict[str, str] = None):
auth = HTTPBasicAuth(username, token)
headers = headers or GITHUB_DEFAULT_HEADERS
super().__init__(self.BASE_URL, auth=auth, headers=headers)
def __enter__(self):
return self
@property
def username(self) -> str:
return self._session.auth.username
@username.setter
def username(self, username: str):
self._session.auth.username = username
@property
def token(self) -> str:
return self._session.auth.password
@token.setter
def token(self, token: str):
self._session.auth.password = token
def get_public_repos(self) -> List[Dict[str, Any]]:
return self.get('user/repos', params={'visibility': 'public'})
def get_user(self, user: str) -> Dict[str, Any]:
return self.get(f'users/{user}')