diff options
author | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2015-12-30 19:13:10 +0100 |
---|---|---|
committer | Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> | 2015-12-30 19:13:10 +0100 |
commit | 74dc2acc788fb6e2fdced0561d8959e2a9d0572f (patch) | |
tree | ecfce463bab9855dd52be4d9d9f8a665f65a8204 | |
parent | c580b1e69868e038ef61080aa6c6b92f112b4891 (diff) | |
download | gitlab-74dc2acc788fb6e2fdced0561d8959e2a9d0572f.tar.gz |
Add a get method for GitlabObject
This change provides a way to implement GET for objects that don't
support it, but support LIST.
It is also a first step to a cleaner API.
-rw-r--r-- | gitlab/__init__.py | 24 | ||||
-rw-r--r-- | gitlab/cli.py | 4 |
2 files changed, 20 insertions, 8 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 39fea25..638c5eb 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -696,11 +696,23 @@ class GitlabObject(object): return gl.list(cls, **kwargs) @classmethod + def get(cls, gl, id, **kwargs): + if cls.canGet is True: + return cls(gl, id, **kwargs) + elif cls.canGet == 'from_list': + for obj in cls.list(gl, **kwargs): + obj_id = getattr(obj, obj.idAttr) + if str(obj_id) == str(id): + return obj + + raise GitlabGetError("Object not found") + + @classmethod def _get_list_or_object(cls, gl, id, **kwargs): if id is None and cls.getListWhenNoId: return cls.list(gl, **kwargs) else: - return cls(gl, id, **kwargs) + return cls.get(gl, id, **kwargs) def _get_object(self, k, v): if self._constructorTypes and k in self._constructorTypes: @@ -834,7 +846,7 @@ class GitlabObject(object): class UserKey(GitlabObject): _url = '/users/%(user_id)s/keys' - canGet = False + canGet = 'from_list' canUpdate = False requiredUrlAttrs = ['user_id'] requiredCreateAttrs = ['title', 'key'] @@ -882,7 +894,7 @@ class CurrentUser(GitlabObject): class GroupMember(GitlabObject): _url = '/groups/%(group_id)s/members' - canGet = False + canGet = 'from_list' requiredUrlAttrs = ['group_id'] requiredCreateAttrs = ['access_level', 'user_id'] requiredUpdateAttrs = ['access_level'] @@ -928,7 +940,7 @@ class Issue(GitlabObject): _url = '/issues' _constructorTypes = {'author': 'User', 'assignee': 'User', 'milestone': 'ProjectMilestone'} - canGet = False + canGet = 'from_list' canDelete = False canUpdate = False canCreate = False @@ -997,7 +1009,7 @@ class ProjectKey(GitlabObject): class ProjectEvent(GitlabObject): _url = '/projects/%(project_id)s/events' - canGet = False + canGet = 'from_list' canDelete = False canUpdate = False canCreate = False @@ -1073,7 +1085,7 @@ class ProjectNote(GitlabObject): class ProjectTag(GitlabObject): _url = '/projects/%(project_id)s/repository/tags' idAttr = 'name' - canGet = False + canGet = 'from_list' canDelete = False canUpdate = False requiredUrlAttrs = ['project_id'] diff --git a/gitlab/cli.py b/gitlab/cli.py index 1f82498..648da2d 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -170,7 +170,7 @@ def do_list(cls, gl, what, args): def do_get(cls, gl, what, args): - if not cls.canGet: + if cls.canGet is False: die("%s objects can't be retrieved" % what) id = None @@ -178,7 +178,7 @@ def do_get(cls, gl, what, args): id = get_id(cls, args) try: - o = cls(gl, id, **args) + o = cls.get(gl, id, **args) except Exception as e: die("Impossible to get object (%s)" % str(e)) |