mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 19:01:15 +00:00
Add a customizable User-Agent (issue #109)
This commit is contained in:
+2
-2
@@ -32,8 +32,8 @@ DEFAULT_TIMEOUT = 10
|
||||
|
||||
|
||||
class Github(object):
|
||||
def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None):
|
||||
self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret)
|
||||
def __init__(self, login_or_token=None, password=None, base_url=DEFAULT_BASE_URL, timeout=DEFAULT_TIMEOUT, client_id=None, client_secret=None, user_agent=None):
|
||||
self.__requester = Requester(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent)
|
||||
|
||||
def get_FIX_REPO_GET_GIT_REF(self):
|
||||
return self.__requester.FIX_REPO_GET_GIT_REF
|
||||
|
||||
+4
-1
@@ -39,7 +39,7 @@ class Requester:
|
||||
cls.__httpConnectionClass = httpConnectionClass
|
||||
cls.__httpsConnectionClass = httpsConnectionClass
|
||||
|
||||
def __init__(self, login_or_token, password, base_url, timeout, client_id=None, client_secret=None):
|
||||
def __init__(self, login_or_token, password, base_url, timeout, client_id, client_secret, user_agent):
|
||||
if password is not None:
|
||||
login = login_or_token
|
||||
self.__authorizationHeader = "Basic " + base64.b64encode(login + ":" + password).replace('\n', '')
|
||||
@@ -67,6 +67,7 @@ class Requester:
|
||||
|
||||
self.__clientId = client_id
|
||||
self.__clientSecret = client_secret
|
||||
self.__userAgent = user_agent
|
||||
|
||||
def requestAndCheck(self, verb, url, parameters, input):
|
||||
status, headers, output = self.requestRaw(verb, url, parameters, input)
|
||||
@@ -88,6 +89,8 @@ class Requester:
|
||||
|
||||
requestHeaders = dict()
|
||||
self.__authenticate(requestHeaders, parameters)
|
||||
if self.__userAgent is not None:
|
||||
requestHeaders["User-Agent"] = self.__userAgent
|
||||
|
||||
url = self.__makeAbsoluteUrl(url)
|
||||
url = self.__addParametersToUrl(url, parameters)
|
||||
|
||||
@@ -34,3 +34,7 @@ class Authentication(Framework.BasicTestCase):
|
||||
def testSecretKeyAuthentication(self):
|
||||
g = github.Github(client_id=self.client_id,client_secret=self.client_secret)
|
||||
self.assertListKeyEqual(g.get_organization("BeaverSoftware").get_repos("public"), lambda r: r.name, ["FatherBeaver", "PyGithub"])
|
||||
|
||||
def testUserAgent(self):
|
||||
g = github.Github(user_agent="PyGithubTester")
|
||||
self.assertEqual(g.get_user("jacquev6").name, "Vincent Jacques")
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
https GET api.github.com None /users/jacquev6 {'User-Agent': 'PyGithubTester'} null
|
||||
200
|
||||
[('status', '200 OK'), ('content-length', '1250'), ('x-github-media-type', 'github.beta; format=json'), ('x-content-type-options', 'nosniff'), ('vary', 'Accept'), ('x-ratelimit-remaining', '57'), ('server', 'nginx'), ('last-modified', 'Mon, 19 Nov 2012 19:05:48 GMT'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '60'), ('etag', '"20bb1bc354d1c62d7c5e8b918cdbe6a1"'), ('cache-control', 'public, s-maxage=60, max-age=60'), ('date', 'Mon, 19 Nov 2012 20:14:08 GMT'), ('content-type', 'application/json; charset=utf-8')]
|
||||
{"public_repos":19,"type":"User","followers_url":"https://api.github.com/users/jacquev6/followers","url":"https://api.github.com/users/jacquev6","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png","received_events_url":"https://api.github.com/users/jacquev6/received_events","following_url":"https://api.github.com/users/jacquev6/following","login":"jacquev6","blog":"http://vincent-jacques.net","following":37,"html_url":"https://github.com/jacquev6","created_at":"2010-07-09T06:10:06Z","subscriptions_url":"https://api.github.com/users/jacquev6/subscriptions","hireable":false,"gravatar_id":"b68de5ae38616c296fa345d2b9df2225","starred_url":"https://api.github.com/users/jacquev6/starred{/owner}{/repo}","gists_url":"https://api.github.com/users/jacquev6/gists{/gist_id}","bio":"","name":"Vincent Jacques","email":"vincent@vincent-jacques.net","repos_url":"https://api.github.com/users/jacquev6/repos","public_gists":2,"followers":18,"company":"Criteo","location":"Paris, France","id":327146,"events_url":"https://api.github.com/users/jacquev6/events{/privacy}","organizations_url":"https://api.github.com/users/jacquev6/orgs"}
|
||||
|
||||
Reference in New Issue
Block a user