summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/__init__.py4
-rw-r--r--gitlab/objects.py61
-rw-r--r--gitlab/tests/test_gitlabobject.py34
3 files changed, 98 insertions, 1 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 82a2414..679b023 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -103,6 +103,7 @@ class Gitlab(object):
self.runners = RunnerManager(self)
self.settings = ApplicationSettingsManager(self)
self.sidekiq = SidekiqManager(self)
+ self.snippets = SnippetManager(self)
self.users = UserManager(self)
self.teams = TeamManager(self)
self.todos = TodoManager(self)
@@ -469,7 +470,8 @@ class Gitlab(object):
params.pop(obj.idAttr)
r = self._raw_delete(url, **params)
- raise_error_from_response(r, GitlabDeleteError)
+ raise_error_from_response(r, GitlabDeleteError,
+ expected_code=[200, 204])
return True
def create(self, obj, **kwargs):
diff --git a/gitlab/objects.py b/gitlab/objects.py
index 4d1e7b8..dcf5d5c 100644
--- a/gitlab/objects.py
+++ b/gitlab/objects.py
@@ -1018,6 +1018,67 @@ class LicenseManager(BaseManager):
obj_cls = License
+class Snippet(GitlabObject):
+ _url = '/snippets'
+ _constructorTypes = {'author': 'User'}
+ requiredCreateAttrs = ['title', 'file_name', 'content']
+ optionalCreateAttrs = ['lifetime', 'visibility_level']
+ optionalUpdateAttrs = ['title', 'file_name', 'content', 'visibility_level']
+ shortPrintAttr = 'title'
+
+ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
+ """Return the raw content of a snippet.
+
+ Args:
+ streamed (bool): If True the data will be processed by chunks of
+ `chunk_size` and each chunk is passed to `action` for
+ treatment.
+ action (callable): Callable responsible of dealing with chunk of
+ data.
+ chunk_size (int): Size of each chunk.
+
+ Returns:
+ str: The snippet content
+
+ Raises:
+ GitlabConnectionError: If the server cannot be reached.
+ GitlabGetError: If the server fails to perform the request.
+ """
+ url = ("/snippets/%(snippet_id)s/raw" %
+ {'snippet_id': self.id})
+ r = self.gitlab._raw_get(url, **kwargs)
+ raise_error_from_response(r, GitlabGetError)
+ return utils.response_content(r, streamed, action, chunk_size)
+
+
+class SnippetManager(BaseManager):
+ obj_cls = Snippet
+
+ def all(self, **kwargs):
+ """List all the snippets
+
+ Args:
+ all (bool): If True, return all the items, without pagination
+ **kwargs: Additional arguments to send to GitLab.
+
+ Returns:
+ list(gitlab.Gitlab.Snippet): The list of snippets.
+ """
+ return self.gitlab._raw_list("/snippets/public", Snippet, **kwargs)
+
+ def owned(self, **kwargs):
+ """List owned snippets.
+
+ Args:
+ all (bool): If True, return all the items, without pagination
+ **kwargs: Additional arguments to send to GitLab.
+
+ Returns:
+ list(gitlab.Gitlab.Snippet): The list of owned snippets.
+ """
+ return self.gitlab._raw_list("/snippets", Snippet, **kwargs)
+
+
class Namespace(GitlabObject):
_url = '/namespaces'
canGet = 'from_list'
diff --git a/gitlab/tests/test_gitlabobject.py b/gitlab/tests/test_gitlabobject.py
index cf06a2a..d191c0f 100644
--- a/gitlab/tests/test_gitlabobject.py
+++ b/gitlab/tests/test_gitlabobject.py
@@ -455,3 +455,37 @@ class TestProjectSnippet(unittest.TestCase):
def test_blob_fail(self):
with HTTMock(self.resp_content_fail):
self.assertRaises(GitlabGetError, self.obj.content)
+
+
+class TestSnippet(unittest.TestCase):
+ def setUp(self):
+ self.gl = Gitlab("http://localhost", private_token="private_token",
+ email="testuser@test.com", password="testpassword",
+ ssl_verify=True)
+ self.obj = Snippet(self.gl, data={"id": 3})
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/snippets/3/raw",
+ method="get")
+ def resp_content(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = 'content'.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ @urlmatch(scheme="http", netloc="localhost",
+ path="/api/v3/snippets/3/raw",
+ method="get")
+ def resp_content_fail(self, url, request):
+ headers = {'content-type': 'application/json'}
+ content = '{"message": "messagecontent" }'.encode("utf-8")
+ return response(400, content, headers, None, 5, request)
+
+ def test_content(self):
+ with HTTMock(self.resp_content):
+ data = b'content'
+ content = self.obj.content()
+ self.assertEqual(content, data)
+
+ def test_blob_fail(self):
+ with HTTMock(self.resp_content_fail):
+ self.assertRaises(GitlabGetError, self.obj.content)