mirror of
https://github.com/kennethreitz/python-github3.git
synced 2026-06-05 23:10:17 +00:00
proper requests authentication
This commit is contained in:
+14
-18
@@ -27,6 +27,8 @@ class GithubCore(object):
|
||||
|
||||
def __init__(self):
|
||||
self.session = requests.session()
|
||||
self.session.params = {'per_page': 100}
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _resource_serialize(o):
|
||||
@@ -57,12 +59,6 @@ class GithubCore(object):
|
||||
return (settings.base_url + resource)
|
||||
|
||||
|
||||
def _requests_pre_hook(*args, **kwargs):
|
||||
"""Pre-processing for HTTP requests arguments."""
|
||||
|
||||
return args, kwargs
|
||||
|
||||
|
||||
def _requests_post_hook(self, r):
|
||||
"""Post-processing for HTTP response objects."""
|
||||
|
||||
@@ -72,18 +68,18 @@ class GithubCore(object):
|
||||
return r
|
||||
|
||||
|
||||
def _http_resource(self, verb, endpoint, params=None, authed=True, **etc):
|
||||
def _http_resource(self, verb, endpoint, params=None, **etc):
|
||||
|
||||
url = self._generate_url(endpoint)
|
||||
# print self.session.__dict__
|
||||
|
||||
if authed:
|
||||
args, kwargs = self._requests_pre_hook(verb, url, params=params)
|
||||
else:
|
||||
args = (verb, url)
|
||||
kwargs = {'params': params}
|
||||
args = (verb, url)
|
||||
kwargs = {'params': params}
|
||||
|
||||
kwargs.update(etc)
|
||||
|
||||
print self.session.__dict__
|
||||
|
||||
r = self.session.request(*args, **kwargs)
|
||||
r = self._requests_post_hook(r)
|
||||
|
||||
@@ -94,23 +90,23 @@ class GithubCore(object):
|
||||
return r
|
||||
|
||||
|
||||
def _get_resource(self, resource, obj, authed=True, **kwargs):
|
||||
def _get_resource(self, resource, obj, **kwargs):
|
||||
|
||||
r = self._http_resource('GET', resource, params=kwargs, authed=authed)
|
||||
r = self._http_resource('GET', resource, params=kwargs)
|
||||
item = self._resource_deserialize(r.content)
|
||||
|
||||
return obj.new_from_dict(item, gh=self)
|
||||
|
||||
def _patch_resource(self, resource, data, authed=True, **kwargs):
|
||||
r = self._http_resource('PATCH', resource, data=data, params=kwargs, authed=authed)
|
||||
def _patch_resource(self, resource, data, **kwargs):
|
||||
r = self._http_resource('PATCH', resource, data=data, params=kwargs)
|
||||
msg = self._resource_deserialize(r.content)
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
def _get_resources(self, resource, obj, authed=True, **kwargs):
|
||||
def _get_resources(self, resource, obj, **kwargs):
|
||||
|
||||
r = self._http_resource('GET', resource, params=kwargs, authed=authed)
|
||||
r = self._http_resource('GET', resource, params=kwargs)
|
||||
d_items = self._resource_deserialize(r.content)
|
||||
|
||||
items = []
|
||||
|
||||
+1
-5
@@ -29,13 +29,9 @@ def no_auth():
|
||||
def basic_auth(username, password):
|
||||
"""Returns an authenticated Github object, via HTTP Basic."""
|
||||
|
||||
def enable_auth(*args, **kwargs):
|
||||
kwargs['auth'] = (username, password)
|
||||
return args, kwargs
|
||||
|
||||
gh = Github()
|
||||
gh.is_authenticated = True
|
||||
gh._requests_pre_hook = enable_auth
|
||||
gh.session.auth = (username, password)
|
||||
|
||||
return gh
|
||||
|
||||
|
||||
+1
-6
@@ -59,6 +59,7 @@ class BaseResource(object):
|
||||
_gh = gh
|
||||
)
|
||||
|
||||
|
||||
def update(self):
|
||||
deploy = key_diff(self._cache, self.dict(), pack=True)
|
||||
|
||||
@@ -69,12 +70,6 @@ class BaseResource(object):
|
||||
return r
|
||||
|
||||
|
||||
|
||||
def setattr(self, k, v):
|
||||
# TODO: when writable key changed,
|
||||
pass
|
||||
|
||||
|
||||
class Plan(BaseResource):
|
||||
"""Github Plan object model."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user