Skip to content

Commit

Permalink
Merge branch 'features/personal_snippets' of https://github.com/guyzm…
Browse files Browse the repository at this point in the history
…o/python-gitlab into guyzmo-features/personal_snippets
  • Loading branch information
Gauvain Pocentek committed Dec 25, 2016
2 parents b05c0b6 + 6022dfe commit 26c8a0f
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gitlab/__init__.py
Expand Up @@ -103,6 +103,7 @@ def __init__(self, url, private_token=None, email=None, password=None,
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)
Expand Down Expand Up @@ -469,7 +470,8 @@ def delete(self, obj, id=None, **kwargs):
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):
Expand Down
61 changes: 61 additions & 0 deletions gitlab/objects.py
Expand Up @@ -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'
Expand Down
34 changes: 34 additions & 0 deletions gitlab/tests/test_gitlabobject.py
Expand Up @@ -455,3 +455,37 @@ def test_content(self):
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)

0 comments on commit 26c8a0f

Please sign in to comment.