summaryrefslogtreecommitdiff
path: root/gitlab/mixins.py
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2018-03-17 16:46:18 +0100
committerGauvain Pocentek <gauvain@pocentek.net>2018-03-17 16:46:18 +0100
commit1940feec3dbb099dc3d671cd14ba756e7d34b071 (patch)
tree50c516a33507c9c92ab88c90189445acf5a65cf7 /gitlab/mixins.py
parent455a8fc8cab12bbcbf35f04053da84ec0ed1c5c6 (diff)
downloadgitlab-1940feec3dbb099dc3d671cd14ba756e7d34b071.tar.gz
Implement attribute types to handle special cases
Some attributes need to be parsed/modified to work with the API (for instance lists). This patch provides two attribute types that will simplify parts of the code, and fix some CLI bugs. Fixes #443
Diffstat (limited to 'gitlab/mixins.py')
-rw-r--r--gitlab/mixins.py39
1 files changed, 37 insertions, 2 deletions
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index ea21e10..28ad04d 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -108,9 +108,21 @@ class ListMixin(object):
GitlabListError: If the server cannot perform the request
"""
+ # Duplicate data to avoid messing with what the user sent us
+ data = kwargs.copy()
+
+ # We get the attributes that need some special transformation
+ types = getattr(self, '_types', {})
+ if types:
+ for attr_name, type_cls in types.items():
+ if attr_name in data.keys():
+ type_obj = type_cls(data[attr_name])
+ data[attr_name] = type_obj.get_for_api()
+
# Allow to overwrite the path, handy for custom listings
- path = kwargs.pop('path', self.path)
- obj = self.gitlab.http_list(path, **kwargs)
+ path = data.pop('path', self.path)
+
+ obj = self.gitlab.http_list(path, **data)
if isinstance(obj, list):
return [self._obj_cls(self, item) for item in obj]
else:
@@ -187,8 +199,22 @@ class CreateMixin(object):
GitlabCreateError: If the server cannot perform the request
"""
self._check_missing_create_attrs(data)
+
+ # special handling of the object if needed
if hasattr(self, '_sanitize_data'):
data = self._sanitize_data(data, 'create')
+
+ # We get the attributes that need some special transformation
+ types = getattr(self, '_types', {})
+
+ if types:
+ # Duplicate data to avoid messing with what the user sent us
+ data = data.copy()
+ for attr_name, type_cls in types.items():
+ if attr_name in data.keys():
+ type_obj = type_cls(data[attr_name])
+ data[attr_name] = type_obj.get_for_api()
+
# Handle specific URL for creation
path = kwargs.pop('path', self.path)
server_data = self.gitlab.http_post(path, post_data=data, **kwargs)
@@ -238,11 +264,20 @@ class UpdateMixin(object):
path = '%s/%s' % (self.path, id)
self._check_missing_update_attrs(new_data)
+
+ # special handling of the object if needed
if hasattr(self, '_sanitize_data'):
data = self._sanitize_data(new_data, 'update')
else:
data = new_data
+ # We get the attributes that need some special transformation
+ types = getattr(self, '_types', {})
+ for attr_name, type_cls in types.items():
+ if attr_name in data.keys():
+ type_obj = type_cls(data[attr_name])
+ data[attr_name] = type_obj.get_for_api()
+
return self.gitlab.http_put(path, post_data=data, **kwargs)