From 858352b9a337471401dd2c8d9552c13efab625e6 Mon Sep 17 00:00:00 2001 From: Guyzmo Date: Sat, 2 Apr 2016 16:39:39 +0200 Subject: [PATCH 1/5] Adding a Session instance for all HTTP requests The session instance will make it easier for setting up once headers, including the authentication payload, and it's also making it easier to override HTTP queries handling, when using [betamax](https://github.com/sigmavirus24/betamax) for recording and replaying the test suites. --- gitlab/__init__.py | 75 ++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 6c7519537..f5e42940b 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -122,6 +122,9 @@ def __init__(self, url, private_token=None, #: Whether SSL certificates should be validated self.ssl_verify = ssl_verify + #: Create a session object for requests + self.session = requests.Session() + self.settings = ApplicationSettingsManager(self) self.user_keys = UserKeyManager(self) self.users = UserManager(self) @@ -260,11 +263,11 @@ def _raw_get(self, path, content_type=None, **kwargs): headers = self._create_headers(content_type) try: - return requests.get(url, - params=kwargs, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + return self.session.get(url, + params=kwargs, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -298,10 +301,10 @@ def _raw_post(self, path, data=None, content_type=None, **kwargs): url = '%s%s' % (self._url, path) headers = self._create_headers(content_type) try: - return requests.post(url, params=kwargs, data=data, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + return self.session.post(url, params=kwargs, data=data, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -311,10 +314,10 @@ def _raw_put(self, path, data=None, content_type=None, **kwargs): headers = self._create_headers(content_type) try: - return requests.put(url, data=data, params=kwargs, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + return self.session.put(url, data=data, params=kwargs, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -324,11 +327,11 @@ def _raw_delete(self, path, content_type=None, **kwargs): headers = self._create_headers(content_type) try: - return requests.delete(url, - params=kwargs, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + return self.session.delete(url, + params=kwargs, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -366,9 +369,9 @@ def list(self, obj_class, **kwargs): del params[attribute] try: - r = requests.get(url, params=params, headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + r = self.session.get(url, params=params, headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -434,8 +437,8 @@ def get(self, obj_class, id=None, **kwargs): del params[attribute] try: - r = requests.get(url, params=params, headers=headers, - verify=self.ssl_verify, timeout=self.timeout) + r = self.session.get(url, params=params, headers=headers, + verify=self.ssl_verify, timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -487,11 +490,11 @@ def delete(self, obj, id=None, **kwargs): del params[attribute] try: - r = requests.delete(url, - params=params, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + r = self.session.delete(url, + params=params, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -535,10 +538,10 @@ def create(self, obj, **kwargs): data = obj._data_for_gitlab(extra_parameters=kwargs) try: - r = requests.post(url, data=data, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + r = self.session.post(url, data=data, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) @@ -585,10 +588,10 @@ def update(self, obj, **kwargs): data = obj._data_for_gitlab(extra_parameters=kwargs, update=True) try: - r = requests.put(url, data=data, - headers=headers, - verify=self.ssl_verify, - timeout=self.timeout) + r = self.session.put(url, data=data, + headers=headers, + verify=self.ssl_verify, + timeout=self.timeout) except Exception as e: raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) From 7a8f81b32e47dab6da495f5cefffe48566934744 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 8 May 2016 11:55:04 +0200 Subject: [PATCH 2/5] Add support for Project raw_blob --- gitlab/objects.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gitlab/objects.py b/gitlab/objects.py index 2ceb37f71..a7f35bdbb 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1474,6 +1474,24 @@ def blob(self, sha, filepath, **kwargs): raise_error_from_response(r, GitlabGetError) return r.content + def raw_blob(self, sha, **kwargs): + """Return the raw file contents for a blob by blob SHA. + + Args: + sha(str): ID of the blob + + Returns: + str: The blob content + + Raises: + GitlabConnectionError: If the server cannot be reached. + GitlabGetError: If the server fails to perform the request. + """ + url = "/projects/%s/repository/raw_blobs/%s" % (self.id, sha) + r = self.gitlab._raw_get(url, **kwargs) + raise_error_from_response(r, GitlabGetError) + return r.content + def archive(self, sha=None, **kwargs): """Return a tarball of the repository. From 250f34806b1414b5346b4eaf268eb2537d8017de Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 8 May 2016 12:02:19 +0200 Subject: [PATCH 3/5] Implement project compare Fixes #112 --- gitlab/objects.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index a7f35bdbb..3a44f6ebd 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1475,7 +1475,7 @@ def blob(self, sha, filepath, **kwargs): return r.content def raw_blob(self, sha, **kwargs): - """Return the raw file contents for a blob by blob SHA. + """Returns the raw file contents for a blob by blob SHA. Args: sha(str): ID of the blob @@ -1492,6 +1492,26 @@ def raw_blob(self, sha, **kwargs): raise_error_from_response(r, GitlabGetError) return r.content + def compare(self, from_, to, **kwargs): + """Returns a diff between two branches/commits. + + Args: + from_(str): orig branch/SHA + to(str): dest branch/SHA + + Returns: + str: The diff + + Raises: + GitlabConnectionError: If the server cannot be reached. + GitlabGetError: If the server fails to perform the request. + """ + url = "/projects/%s/repository/compare" % self.id + url = "%s?from=%s&to=%s" % (url, from_, to) + r = self.gitlab._raw_get(url, **kwargs) + raise_error_from_response(r, GitlabGetError) + return r.json() + def archive(self, sha=None, **kwargs): """Return a tarball of the repository. From 1600770e1d01aaaa90dbfd602e073e4e4a578fc1 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 8 May 2016 12:43:11 +0200 Subject: [PATCH 4/5] Implement project contributors --- gitlab/objects.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gitlab/objects.py b/gitlab/objects.py index 3a44f6ebd..9594c2bf7 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1512,6 +1512,21 @@ def compare(self, from_, to, **kwargs): raise_error_from_response(r, GitlabGetError) return r.json() + def contributors(self): + """Returns a list of contributors for the project. + + Returns: + list: The contibutors + + Raises: + GitlabConnectionError: If the server cannot be reached. + GitlabGetError: If the server fails to perform the request. + """ + url = "/projects/%s/repository/contributors" % self.id + r = self.gitlab._raw_get(url) + raise_error_from_response(r, GitlabListError) + return r.json() + def archive(self, sha=None, **kwargs): """Return a tarball of the repository. From 64af39818d02af1b40644d71fd047d6bc3f6e69e Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 8 May 2016 13:07:41 +0200 Subject: [PATCH 5/5] Drop the next_url attribute when listing Fixes #106 --- gitlab/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index f5e42940b..1994d5578 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -368,6 +368,10 @@ def list(self, obj_class, **kwargs): for attribute in obj_class.requiredUrlAttrs: del params[attribute] + # Also remove the next-url attribute that make queries fail + if 'next_url' in params: + del params['next_url'] + try: r = self.session.get(url, params=params, headers=headers, verify=self.ssl_verify,