Skip to content

Commit 24ba442

Browse files
committed
refactor: move response_content into backend code
1 parent 7d779c8 commit 24ba442

File tree

14 files changed

+88
-68
lines changed

14 files changed

+88
-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
@@ -18,6 +18,18 @@ def __init__(self, response: requests.Response) -> None:
1818

1919

2020
class Backend(Protocol):
21+
@staticmethod
22+
@abc.abstractmethod
23+
def response_content(
24+
response: requests.Response,
25+
streamed: bool,
26+
action: Optional[Callable[[bytes], None]],
27+
chunk_size: int,
28+
*,
29+
iterator: bool,
30+
) -> Optional[Union[bytes, Iterator[Any]]]:
31+
...
32+
2133
@abc.abstractmethod
2234
def http_request(
2335
self,

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
@@ -77,6 +82,29 @@ def prepare_send_data(
7782

7883
return (post_data, None, "application/json")
7984

85+
@staticmethod
86+
def response_content(
87+
response: requests.Response,
88+
streamed: bool,
89+
action: Optional[Callable[[bytes], None]],
90+
chunk_size: int,
91+
*,
92+
iterator: bool,
93+
) -> Optional[Union[bytes, Iterator[Any]]]:
94+
if iterator:
95+
return response.iter_content(chunk_size=chunk_size)
96+
97+
if streamed is False:
98+
return response.content
99+
100+
if action is None:
101+
action = _StdoutStream()
102+
103+
for chunk in response.iter_content(chunk_size=chunk_size):
104+
if chunk:
105+
action(chunk)
106+
return None
107+
80108
def http_request(
81109
self,
82110
method: str,

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: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,21 @@
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+
return DefaultBackend.response_content(*args, **kwargs)
3820

3921

4022
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)