From 61fba8431d0471128772429b9a8921d8092fa71b Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Sun, 11 Jun 2017 13:41:14 +0200 Subject: 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) --- gitlab/mixins.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'gitlab/mixins.py') 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) -- cgit v1.2.1