Skip to content

Commit 61fba84

Browse files
author
Gauvain Pocentek
committed
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)
1 parent 197ffd7 commit 61fba84

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

gitlab/mixins.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222

2323
class GetMixin(object):
24-
def get(self, id, **kwargs):
24+
def get(self, id, lazy=False, **kwargs):
2525
"""Retrieve a single object.
2626
2727
Args:
2828
id (int or str): ID of the object to retrieve
29+
lazy (bool): If True, don't request the server, but create a
30+
shallow object giving access to the managers. This is
31+
useful if you want to avoid useless calls to the API.
2932
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
3033
3134
Returns:
@@ -35,6 +38,9 @@ def get(self, id, **kwargs):
3538
GitlabGetError: If the server cannot perform the request.
3639
"""
3740
path = '%s/%s' % (self.path, id)
41+
if lazy is True:
42+
return self._obj_cls(self, {self._obj_cls._id_attr: id})
43+
3844
server_data = self.gitlab.http_get(path, **kwargs)
3945
return self._obj_cls(self, server_data)
4046

0 commit comments

Comments
 (0)