Skip to content

Commit 4dc7f00

Browse files
committed
refactor: move response_content into backend code
1 parent 1da7c53 commit 4dc7f00

File tree

14 files changed

+91
-68
lines changed

14 files changed

+91
-68
lines changed

gitlab/_backends/protocol.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
import sys
3-
from typing import Any, Dict, Optional, Union
3+
from typing import Any, Callable, Dict, Iterator, Optional, Union
44

55
import requests
66
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
@@ -32,3 +32,15 @@ def http_request(
3232
**kwargs: Any,
3333
) -> BackendResponse:
3434
...
35+
36+
@abc.abstractmethod
37+
def response_content(
38+
self,
39+
response: requests.Response,
40+
streamed: bool,
41+
action: Optional[Callable[[bytes], None]],
42+
chunk_size: int,
43+
*,
44+
iterator: bool,
45+
) -> Optional[Union[bytes, Iterator[Any]]]:
46+
...

gitlab/_backends/requests_backend.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING, Union
3+
from typing import Any, Callable, Dict, Iterator, Optional, Tuple, TYPE_CHECKING, Union
44

55
import requests
66
from requests.structures import CaseInsensitiveDict
@@ -9,6 +9,11 @@
99
from . import protocol
1010

1111

12+
class _StdoutStream:
13+
def __call__(self, chunk: Any) -> None:
14+
print(chunk)
15+
16+
1217
class RequestsResponse(protocol.BackendResponse):
1318
def __init__(self, response: requests.Response) -> None:
1419
self._response: requests.Response = response
@@ -117,3 +122,26 @@ def http_request(
117122
**kwargs,
118123
)
119124
return RequestsResponse(response=response)
125+
126+
def response_content(
127+
self,
128+
response: requests.Response,
129+
streamed: bool,
130+
action: Optional[Callable[[bytes], None]],
131+
chunk_size: int,
132+
*,
133+
iterator: bool,
134+
) -> Optional[Union[bytes, Iterator[Any]]]:
135+
if iterator:
136+
return response.iter_content(chunk_size=chunk_size)
137+
138+
if streamed is False:
139+
return response.content
140+
141+
if action is None:
142+
action = _StdoutStream()
143+
144+
for chunk in response.iter_content(chunk_size=chunk_size):
145+
if chunk:
146+
action(chunk)
147+
return None

gitlab/mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def download(
637637
)
638638
if TYPE_CHECKING:
639639
assert isinstance(result, requests.Response)
640-
return utils.response_content(
640+
return self.manager.gitlab._backend.response_content(
641641
result, streamed, action, chunk_size, iterator=iterator
642642
)
643643

gitlab/utils.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,22 @@
22
import traceback
33
import urllib.parse
44
import warnings
5-
from typing import Any, Callable, Dict, Iterator, Optional, Tuple, Type, Union
6-
7-
import requests
5+
from typing import Any, Dict, Iterator, Optional, Tuple, Type, Union
86

97
from gitlab import types
10-
11-
12-
class _StdoutStream:
13-
def __call__(self, chunk: Any) -> None:
14-
print(chunk)
8+
from gitlab._backends import DefaultBackend
159

1610

1711
def response_content(
18-
response: requests.Response,
19-
streamed: bool,
20-
action: Optional[Callable[[bytes], None]],
21-
chunk_size: int,
22-
*,
23-
iterator: bool,
12+
*args: Any, **kwargs: Any
2413
) -> Optional[Union[bytes, Iterator[Any]]]:
25-
if iterator:
26-
return response.iter_content(chunk_size=chunk_size)
27-
28-
if streamed is False:
29-
return response.content
30-
31-
if action is None:
32-
action = _StdoutStream()
33-
34-
for chunk in response.iter_content(chunk_size=chunk_size):
35-
if chunk:
36-
action(chunk)
37-
return None
14+
warn(
15+
"`utils.response_content()` is deprecated and will be removed in a future"
16+
"version.\nUse the current backend's `response_content()` method instead.",
17+
category=DeprecationWarning,
18+
)
19+
backend = DefaultBackend()
20+
return backend.response_content(*args, **kwargs)
3821

3922

4023
def _transform_types(

gitlab/v4/objects/artifacts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def download(
111111
)
112112
if TYPE_CHECKING:
113113
assert isinstance(result, requests.Response)
114-
return utils.response_content(
114+
return self.gitlab._backend.response_content(
115115
result, streamed, action, chunk_size, iterator=iterator
116116
)
117117

@@ -162,6 +162,6 @@ def raw(
162162
)
163163
if TYPE_CHECKING:
164164
assert isinstance(result, requests.Response)
165-
return utils.response_content(
165+
return self.gitlab._backend.response_content(
166166
result, streamed, action, chunk_size, iterator=iterator
167167
)

gitlab/v4/objects/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def raw(
266266
)
267267
if TYPE_CHECKING:
268268
assert isinstance(result, requests.Response)
269-
return utils.response_content(
269+
return self.gitlab._backend.response_content(
270270
result, streamed, action, chunk_size, iterator=iterator
271271
)
272272

gitlab/v4/objects/jobs.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from gitlab import cli
66
from gitlab import exceptions as exc
7-
from gitlab import utils
87
from gitlab.base import RESTManager, RESTObject
98
from gitlab.mixins import RefreshMixin, RetrieveMixin
109
from gitlab.types import ArrayAttribute
@@ -149,7 +148,7 @@ def artifacts(
149148
)
150149
if TYPE_CHECKING:
151150
assert isinstance(result, requests.Response)
152-
return utils.response_content(
151+
return self.manager.gitlab._backend.response_content(
153152
result, streamed, action, chunk_size, iterator=iterator
154153
)
155154

@@ -192,7 +191,7 @@ def artifact(
192191
)
193192
if TYPE_CHECKING:
194193
assert isinstance(result, requests.Response)
195-
return utils.response_content(
194+
return self.manager.gitlab._backend.response_content(
196195
result, streamed, action, chunk_size, iterator=iterator
197196
)
198197

@@ -233,7 +232,7 @@ def trace(
233232
)
234233
if TYPE_CHECKING:
235234
assert isinstance(result, requests.Response)
236-
return_value = utils.response_content(
235+
return_value = self.manager.gitlab._backend.response_content(
237236
result, streamed, action, chunk_size, iterator=iterator
238237
)
239238
if TYPE_CHECKING:

gitlab/v4/objects/packages.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from gitlab import cli
1313
from gitlab import exceptions as exc
14-
from gitlab import utils
1514
from gitlab.base import RESTManager, RESTObject
1615
from gitlab.mixins import DeleteMixin, GetMixin, ListMixin, ObjectDeleteMixin
1716

@@ -136,7 +135,7 @@ def download(
136135
result = self.gitlab.http_get(path, streamed=streamed, raw=True, **kwargs)
137136
if TYPE_CHECKING:
138137
assert isinstance(result, requests.Response)
139-
return utils.response_content(
138+
return self.gitlab._backend.response_content(
140139
result, streamed, action, chunk_size, iterator=iterator
141140
)
142141

gitlab/v4/objects/projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def snapshot(
541541
)
542542
if TYPE_CHECKING:
543543
assert isinstance(result, requests.Response)
544-
return utils.response_content(
544+
return self.manager.gitlab._backend.response_content(
545545
result, streamed, action, chunk_size, iterator=iterator
546546
)
547547

gitlab/v4/objects/repositories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def repository_raw_blob(
140140
)
141141
if TYPE_CHECKING:
142142
assert isinstance(result, requests.Response)
143-
return utils.response_content(
143+
return self.manager.gitlab._backend.response_content(
144144
result, streamed, action, chunk_size, iterator=iterator
145145
)
146146

@@ -242,7 +242,7 @@ def repository_archive(
242242
)
243243
if TYPE_CHECKING:
244244
assert isinstance(result, requests.Response)
245-
return utils.response_content(
245+
return self.manager.gitlab._backend.response_content(
246246
result, streamed, action, chunk_size, iterator=iterator
247247
)
248248

0 commit comments

Comments
 (0)