Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Calls :epkg:`github` API. 

4""" 

5import requests 

6 

7 

8class GitHubApiException(Exception): 

9 """ 

10 Exception raised when a call to github rest api failed. 

11 """ 

12 

13 def __init__(self, response, url): 

14 """ 

15 Merges everything into a string. 

16 """ 

17 Exception.__init__(self, "response={0}\nurl={1}\ntext={2}\nstatus={3}".format( 

18 response, url, response.text, response.status_code)) 

19 

20 

21def call_github_api(owner, repo, ask, auth=None, headers=None): 

22 """ 

23 Calls `GitHub REST API <https://developer.github.com/v3/>`_. 

24 

25 @param owner owner of the project 

26 @param auth tuple *(user, password)* 

27 @param repo repository name 

28 @param ask query (see below) 

29 @param header dictionary 

30 @return json 

31 

32 Example for *ask*: 

33 

34 * ``commits`` 

35 * ``downloads`` 

36 * ``forks`` 

37 * ``issues`` 

38 * ``pulls`` 

39 * ``stats/code_frequency`` - Needs authentification 

40 * ``stats/commit_activity`` - Needs authentification 

41 * ``stats/punch_card`` - Needs authentification 

42 * ``traffic/popular/referrers`` - Must have push access to repository 

43 * ``traffic/popular/paths`` - Must have push access to repository 

44 * ``traffic/views`` - Must have push access to repository 

45 * ``traffic/clones`` - Must have push access to repository 

46 

47 GitHub limits the number of requets per hour: 

48 `Rate Limiting <https://developer.github.com/v3/#rate-limiting>`_. 

49 """ 

50 url = 'https://api.github.com/repos/{0}/{1}/{2}'.format( 

51 owner, repo, ask.strip('/')) 

52 response = requests.get(url, auth=auth, headers=headers) 

53 if response.status_code != 200: 

54 raise GitHubApiException(response, url) 

55 return response.json()