Skip to content

Commit 0c4d320

Browse files
committed
feat(async): make utils.response_content sync agnostic
1 parent 1aa8c21 commit 0c4d320

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

gitlab/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import asyncio
1819
from urllib.parse import urlparse
1920

2021

@@ -23,7 +24,7 @@ def __call__(self, chunk):
2324
print(chunk)
2425

2526

26-
async def response_content(response, streamed, action):
27+
async def aresponse_content(response, streamed, action):
2728
if streamed is False:
2829
return response.content
2930

@@ -35,6 +36,21 @@ async def response_content(response, streamed, action):
3536
action(chunk)
3637

3738

39+
def response_content(response, streamed, action):
40+
if asyncio.iscoroutine(response):
41+
return aresponse_content(response, streamed, action)
42+
43+
if streamed is False:
44+
return response.content
45+
46+
if action is None:
47+
action = _StdoutStream()
48+
49+
for chunk in response.iter_bytes():
50+
if chunk:
51+
action(chunk)
52+
53+
3854
def copy_dict(dest, src):
3955
for k, v in src.items():
4056
if isinstance(v, dict):

gitlab/v4/objects.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
15701570
result = self.manager.gitlab.http_get(
15711571
path, streamed=streamed, raw=True, **kwargs
15721572
)
1573-
return utils.response_content(result, streamed, action) # TODO:???
1573+
return utils.response_content(result, streamed, action)
15741574

15751575

15761576
class SnippetManager(CRUDMixin, RESTManager):
@@ -1918,7 +1918,7 @@ def artifacts(self, streamed=False, action=None, chunk_size=1024, **kwargs):
19181918
result = self.manager.gitlab.http_get(
19191919
path, streamed=streamed, raw=True, **kwargs
19201920
)
1921-
return utils.response_content(result, streamed, action) # TODO: ???
1921+
return utils.response_content(result, streamed, action)
19221922

19231923
@cli.register_custom_action("ProjectJob")
19241924
@exc.on_http_error(exc.GitlabGetError)
@@ -1946,7 +1946,7 @@ def artifact(self, path, streamed=False, action=None, chunk_size=1024, **kwargs)
19461946
result = self.manager.gitlab.http_get(
19471947
path, streamed=streamed, raw=True, **kwargs
19481948
)
1949-
return utils.response_content(result, streamed, action) # TODO: ???
1949+
return utils.response_content(result, streamed, action)
19501950

19511951
@cli.register_custom_action("ProjectJob")
19521952
@exc.on_http_error(exc.GitlabGetError)
@@ -1973,7 +1973,7 @@ def trace(self, streamed=False, action=None, chunk_size=1024, **kwargs):
19731973
result = self.manager.gitlab.http_get(
19741974
path, streamed=streamed, raw=True, **kwargs
19751975
)
1976-
return utils.response_content(result, streamed, action) # TODO: ???
1976+
return utils.response_content(result, streamed, action)
19771977

19781978

19791979
class ProjectJobManager(RetrieveMixin, RESTManager):
@@ -3458,7 +3458,7 @@ def raw(
34583458
result = self.gitlab.http_get(
34593459
path, query_data=query_data, streamed=streamed, raw=True, **kwargs
34603460
)
3461-
return utils.response_content(result, streamed, action) # TODO: ???
3461+
return utils.response_content(result, streamed, action)
34623462

34633463
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref"))
34643464
@exc.on_http_error(exc.GitlabListError)
@@ -3768,7 +3768,7 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
37683768
result = self.manager.gitlab.http_get(
37693769
path, streamed=streamed, raw=True, **kwargs
37703770
)
3771-
return utils.response_content(result, streamed, action) # TODO: ???
3771+
return utils.response_content(result, streamed, action)
37723772

37733773

37743774
class ProjectSnippetManager(CRUDMixin, RESTManager):
@@ -4088,7 +4088,7 @@ def download(self, streamed=False, action=None, chunk_size=1024, **kwargs):
40884088
result = self.manager.gitlab.http_get(
40894089
path, streamed=streamed, raw=True, **kwargs
40904090
)
4091-
return utils.response_content(result, streamed, action) # TODO: ???
4091+
return utils.response_content(result, streamed, action)
40924092

40934093

40944094
class ProjectExportManager(GetWithoutIdMixin, CreateMixin, RESTManager):
@@ -4279,7 +4279,7 @@ def repository_raw_blob(
42794279
result = self.manager.gitlab.http_get(
42804280
path, streamed=streamed, raw=True, **kwargs
42814281
)
4282-
return utils.response_content(result, streamed, action) # TODO: ???
4282+
return utils.response_content(result, streamed, action)
42834283

42844284
@cli.register_custom_action("Project", ("from_", "to"))
42854285
@exc.on_http_error(exc.GitlabGetError)
@@ -4356,7 +4356,7 @@ def repository_archive(
43564356
result = self.manager.gitlab.http_get(
43574357
path, query_data=query_data, raw=True, streamed=streamed, **kwargs
43584358
)
4359-
return utils.response_content(result, streamed, action) # TODO: ???
4359+
return utils.response_content(result, streamed, action)
43604360

43614361
@cli.register_custom_action("Project", ("forked_from_id",))
43624362
@exc.on_http_error(exc.GitlabCreateError)
@@ -4636,7 +4636,7 @@ def snapshot(
46364636
result = self.manager.gitlab.http_get(
46374637
path, streamed=streamed, raw=True, **kwargs
46384638
)
4639-
return utils.response_content(result, streamed, action) # TODO:???
4639+
return utils.response_content(result, streamed, action)
46404640

46414641
@cli.register_custom_action("Project", ("scope", "search"))
46424642
@exc.on_http_error(exc.GitlabSearchError)
@@ -4729,7 +4729,7 @@ def artifact(
47294729
result = self.manager.gitlab.http_get(
47304730
path, streamed=streamed, raw=True, **kwargs
47314731
)
4732-
return utils.response_content(result, streamed, action) # TODO:???
4732+
return utils.response_content(result, streamed, action)
47334733

47344734

47354735
class ProjectManager(CRUDMixin, RESTManager):

0 commit comments

Comments
 (0)