Skip to content

Commit

Permalink
Merge pull request #3 from guyzmo/feature/requests_session
Browse files Browse the repository at this point in the history
switched direct requests call to use of session instance
  • Loading branch information
ethantkoenig authored Dec 23, 2016
2 parents 4bbc50e + c88f317 commit da455d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 8 additions & 7 deletions gogs_client/_implementation/http_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class RelativeHttpRequestor(object):
A thin wrapper around the requests module that allows for endpoint paths
to be given relative to a fixed base URL
"""
def __init__(self, base_url):
def __init__(self, base_url, session=None):
self.base_url = base_url
self.session = session or requests.Session()

def absolute_url(self, relative_path):
"""
Expand All @@ -26,22 +27,22 @@ def absolute_url(self, relative_path):
# except that they expect relative paths

def delete(self, relative_path, **kwargs):
return requests.delete(self.absolute_url(relative_path), **kwargs)
return self.session.delete(self.absolute_url(relative_path), **kwargs)

def get(self, relative_path, params=None, **kwargs):
return requests.get(self.absolute_url(relative_path), params=params, **kwargs)
return self.session.get(self.absolute_url(relative_path), params=params, **kwargs)

def options(self, relative_path, params=None, **kwargs):
return requests.options(self.absolute_url(relative_path), params=params, **kwargs)
return self.session.options(self.absolute_url(relative_path), params=params, **kwargs)

def patch(self, relative_path, data=None, **kwargs):
return requests.patch(self.absolute_url(relative_path), data=data, **kwargs)
return self.session.patch(self.absolute_url(relative_path), data=data, **kwargs)

def post(self, relative_path, data=None, **kwargs):
return requests.post(self.absolute_url(relative_path), data=data, **kwargs)
return self.session.post(self.absolute_url(relative_path), data=data, **kwargs)

def put(self, relative_path, params=None, data=None, **kwargs):
return requests.put(self.absolute_url(relative_path), params=params, data=data, **kwargs)
return self.session.put(self.absolute_url(relative_path), params=params, data=data, **kwargs)


def append_url(base_url, path):
Expand Down
5 changes: 3 additions & 2 deletions gogs_client/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class GogsApi(object):
A Gogs client, serving as a wrapper around the Gogs HTTP API.
"""

def __init__(self, base_url):
def __init__(self, base_url, session=None):
"""
:param str base_url: the URL of the Gogs server to communicate with. Should be given
with the https protocol
:param str session: a requests session instance
"""
api_base = append_url(base_url, "/api/v1/")
self._requestor = RelativeHttpRequestor(api_base)
self._requestor = RelativeHttpRequestor(api_base, session=session)

def valid_authentication(self, auth):
"""
Expand Down

0 comments on commit da455d1

Please sign in to comment.