summaryrefslogtreecommitdiff
path: root/gitlab/mixins.py
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2017-06-11 13:41:14 +0200
committerGauvain Pocentek <gauvain@pocentek.net>2017-06-11 13:44:05 +0200
commit61fba8431d0471128772429b9a8921d8092fa71b (patch)
tree4d4db2db1a9b6aae4c735bf46a9defe8501bebbd /gitlab/mixins.py
parent197ffd70814ddf577655b3fdb7865f4416201353 (diff)
downloadgitlab-61fba8431d0471128772429b9a8921d8092fa71b.tar.gz
Add laziness to get()
The goal is to create empty objects (no API called) but give access to the managers. Using this users can reduce the number of API calls but still use the same API to access children objects. For example the following will only make one API call but will still get the result right: gl.projects.get(49, lazy=True).issues.get(2, lazy=True).notes.list() This removes the need for more complex managers attributes (e.g. gl.project_issue_notes)
Diffstat (limited to 'gitlab/mixins.py')
-rw-r--r--gitlab/mixins.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index 670f33d..9a84021 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -21,11 +21,14 @@ from gitlab import exceptions
class GetMixin(object):
- def get(self, id, **kwargs):
+ def get(self, id, lazy=False, **kwargs):
"""Retrieve a single object.
Args:
id (int or str): ID of the object to retrieve
+ lazy (bool): If True, don't request the server, but create a
+ shallow object giving access to the managers. This is
+ useful if you want to avoid useless calls to the API.
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
Returns:
@@ -35,6 +38,9 @@ class GetMixin(object):
GitlabGetError: If the server cannot perform the request.
"""
path = '%s/%s' % (self.path, id)
+ if lazy is True:
+ return self._obj_cls(self, {self._obj_cls._id_attr: id})
+
server_data = self.gitlab.http_get(path, **kwargs)
return self._obj_cls(self, server_data)