| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 15d3362 commit 6022dfe
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -103,6 +103,7 @@ def __init__(self, url, private_token=None, email=None, password=None, | |||
| 103 | 103 | self.runners = RunnerManager(self) | |
| 104 | 104 | self.settings = ApplicationSettingsManager(self) | |
| 105 | 105 | self.sidekiq = SidekiqManager(self) | |
| 106 | + self.snippets = SnippetManager(self) | ||
| 106 | 107 | self.users = UserManager(self) | |
| 107 | 108 | self.teams = TeamManager(self) | |
| 108 | 109 | self.todos = TodoManager(self) | |
@@ -469,7 +470,8 @@ def delete(self, obj, id=None, **kwargs): | |||
| 469 | 470 | params.pop(obj.idAttr) | |
| 470 | 471 | ||
| 471 | 472 | r = self._raw_delete(url, **params) | |
| 472 | - raise_error_from_response(r, GitlabDeleteError) | ||
| 473 | + raise_error_from_response(r, GitlabDeleteError, | ||
| 474 | + expected_code=[200, 204]) | ||
| 473 | 475 | return True | |
| 474 | 476 | ||
| 475 | 477 | def create(self, obj, **kwargs): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1018,6 +1018,67 @@ class LicenseManager(BaseManager): | |||
| 1018 | 1018 | obj_cls = License | |
| 1019 | 1019 | ||
| 1020 | 1020 | ||
| 1021 | + class Snippet(GitlabObject): | ||
| 1022 | + _url = '/snippets' | ||
| 1023 | + _constructorTypes = {'author': 'User'} | ||
| 1024 | + requiredCreateAttrs = ['title', 'file_name', 'content'] | ||
| 1025 | + optionalCreateAttrs = ['lifetime', 'visibility_level'] | ||
| 1026 | + optionalUpdateAttrs = ['title', 'file_name', 'content', 'visibility_level'] | ||
| 1027 | + shortPrintAttr = 'title' | ||
| 1028 | + | ||
| 1029 | + def content(self, streamed=False, action=None, chunk_size=1024, **kwargs): | ||
| 1030 | + """Return the raw content of a snippet. | ||
| 1031 | + | ||
| 1032 | + Args: | ||
| 1033 | + streamed (bool): If True the data will be processed by chunks of | ||
| 1034 | + `chunk_size` and each chunk is passed to `action` for | ||
| 1035 | + treatment. | ||
| 1036 | + action (callable): Callable responsible of dealing with chunk of | ||
| 1037 | + data. | ||
| 1038 | + chunk_size (int): Size of each chunk. | ||
| 1039 | + | ||
| 1040 | + Returns: | ||
| 1041 | + str: The snippet content | ||
| 1042 | + | ||
| 1043 | + Raises: | ||
| 1044 | + GitlabConnectionError: If the server cannot be reached. | ||
| 1045 | + GitlabGetError: If the server fails to perform the request. | ||
| 1046 | + """ | ||
| 1047 | + url = ("/snippets/%(snippet_id)s/raw" % | ||
| 1048 | + {'snippet_id': self.id}) | ||
| 1049 | + r = self.gitlab._raw_get(url, **kwargs) | ||
| 1050 | + raise_error_from_response(r, GitlabGetError) | ||
| 1051 | + return utils.response_content(r, streamed, action, chunk_size) | ||
| 1052 | + | ||
| 1053 | + | ||
| 1054 | + class SnippetManager(BaseManager): | ||
| 1055 | + obj_cls = Snippet | ||
| 1056 | + | ||
| 1057 | + def all(self, **kwargs): | ||
| 1058 | + """List all the snippets | ||
| 1059 | + | ||
| 1060 | + Args: | ||
| 1061 | + all (bool): If True, return all the items, without pagination | ||
| 1062 | + **kwargs: Additional arguments to send to GitLab. | ||
| 1063 | + | ||
| 1064 | + Returns: | ||
| 1065 | + list(gitlab.Gitlab.Snippet): The list of snippets. | ||
| 1066 | + """ | ||
| 1067 | + return self.gitlab._raw_list("/snippets/public", Snippet, **kwargs) | ||
| 1068 | + | ||
| 1069 | + def owned(self, **kwargs): | ||
| 1070 | + """List owned snippets. | ||
| 1071 | + | ||
| 1072 | + Args: | ||
| 1073 | + all (bool): If True, return all the items, without pagination | ||
| 1074 | + **kwargs: Additional arguments to send to GitLab. | ||
| 1075 | + | ||
| 1076 | + Returns: | ||
| 1077 | + list(gitlab.Gitlab.Snippet): The list of owned snippets. | ||
| 1078 | + """ | ||
| 1079 | + return self.gitlab._raw_list("/snippets", Snippet, **kwargs) | ||
| 1080 | + | ||
| 1081 | + | ||
| 1021 | 1082 | class Namespace(GitlabObject): | |
| 1022 | 1083 | _url = '/namespaces' | |
| 1023 | 1084 | canGet = 'from_list' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -455,3 +455,37 @@ def test_content(self): | |||
| 455 | 455 | def test_blob_fail(self): | |
| 456 | 456 | with HTTMock(self.resp_content_fail): | |
| 457 | 457 | self.assertRaises(GitlabGetError, self.obj.content) | |
| 458 | + | ||
| 459 | + | ||
| 460 | + class TestSnippet(unittest.TestCase): | ||
| 461 | + def setUp(self): | ||
| 462 | + self.gl = Gitlab("http://localhost", private_token="private_token", | ||
| 463 | + email="testuser@test.com", password="testpassword", | ||
| 464 | + ssl_verify=True) | ||
| 465 | + self.obj = Snippet(self.gl, data={"id": 3}) | ||
| 466 | + | ||
| 467 | + @urlmatch(scheme="http", netloc="localhost", | ||
| 468 | + path="/api/v3/snippets/3/raw", | ||
| 469 | + method="get") | ||
| 470 | + def resp_content(self, url, request): | ||
| 471 | + headers = {'content-type': 'application/json'} | ||
| 472 | + content = 'content'.encode("utf-8") | ||
| 473 | + return response(200, content, headers, None, 5, request) | ||
| 474 | + | ||
| 475 | + @urlmatch(scheme="http", netloc="localhost", | ||
| 476 | + path="/api/v3/snippets/3/raw", | ||
| 477 | + method="get") | ||
| 478 | + def resp_content_fail(self, url, request): | ||
| 479 | + headers = {'content-type': 'application/json'} | ||
| 480 | + content = '{"message": "messagecontent" }'.encode("utf-8") | ||
| 481 | + return response(400, content, headers, None, 5, request) | ||
| 482 | + | ||
| 483 | + def test_content(self): | ||
| 484 | + with HTTMock(self.resp_content): | ||
| 485 | + data = b'content' | ||
| 486 | + content = self.obj.content() | ||
| 487 | + self.assertEqual(content, data) | ||
| 488 | + | ||
| 489 | + def test_blob_fail(self): | ||
| 490 | + with HTTMock(self.resp_content_fail): | ||
| 491 | + self.assertRaises(GitlabGetError, self.obj.content) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments