Skip to content

Commit 1696ad0

Browse files
author
Liora Milbaum
committed
refactor: Migrate MultipartEncoder to RequestsBackend
1 parent a0ecac2 commit 1696ad0

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

gitlab/client.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import requests
1010
import requests.utils
11-
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
1211

1312
import gitlab
1413
import gitlab.config
@@ -637,38 +636,6 @@ def _check_redirects(result: requests.Response) -> None:
637636
)
638637
)
639638

640-
@staticmethod
641-
def _prepare_send_data(
642-
files: Optional[Dict[str, Any]] = None,
643-
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
644-
raw: bool = False,
645-
) -> Tuple[
646-
Optional[Union[Dict[str, Any], bytes]],
647-
Optional[Union[Dict[str, Any], MultipartEncoder]],
648-
str,
649-
]:
650-
if files:
651-
if post_data is None:
652-
post_data = {}
653-
else:
654-
# booleans does not exists for data (neither for MultipartEncoder):
655-
# cast to string int to avoid: 'bool' object has no attribute 'encode'
656-
if TYPE_CHECKING:
657-
assert isinstance(post_data, dict)
658-
for k, v in post_data.items():
659-
if isinstance(v, bool):
660-
post_data[k] = str(int(v))
661-
post_data["file"] = files.get("file")
662-
post_data["avatar"] = files.get("avatar")
663-
664-
data = MultipartEncoder(post_data)
665-
return (None, data, data.content_type)
666-
667-
if raw and post_data:
668-
return (None, post_data, "application/octet-stream")
669-
670-
return (post_data, None, "application/json")
671-
672639
def http_request(
673640
self,
674641
verb: str,
@@ -746,7 +713,9 @@ def http_request(
746713
retry_transient_errors = self.retry_transient_errors
747714

748715
# We need to deal with json vs. data when uploading files
749-
json, data, content_type = self._prepare_send_data(files, post_data, raw)
716+
json, data, content_type = self.http_backend.prepare_send_data(
717+
files, post_data, raw
718+
)
750719
opts["headers"]["Content-type"] = content_type
751720

752721
cur_retries = 0

gitlab/http_backends/requests_backend.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional, Union
1+
from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING, Union
22

33
import requests
44
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
@@ -36,6 +36,38 @@ def __init__(self, session: Optional[requests.Session] = None) -> None:
3636
def client(self) -> requests.Session:
3737
return self._client
3838

39+
@staticmethod
40+
def prepare_send_data(
41+
files: Optional[Dict[str, Any]] = None,
42+
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
43+
raw: bool = False,
44+
) -> Tuple[
45+
Optional[Union[Dict[str, Any], bytes]],
46+
Optional[Union[Dict[str, Any], MultipartEncoder]],
47+
str,
48+
]:
49+
if files:
50+
if post_data is None:
51+
post_data = {}
52+
else:
53+
# booleans does not exists for data (neither for MultipartEncoder):
54+
# cast to string int to avoid: 'bool' object has no attribute 'encode'
55+
if TYPE_CHECKING:
56+
assert isinstance(post_data, dict)
57+
for k, v in post_data.items():
58+
if isinstance(v, bool):
59+
post_data[k] = str(int(v))
60+
post_data["file"] = files.get("file")
61+
post_data["avatar"] = files.get("avatar")
62+
63+
data = MultipartEncoder(post_data)
64+
return (None, data, data.content_type)
65+
66+
if raw and post_data:
67+
return (None, post_data, "application/octet-stream")
68+
69+
return (post_data, None, "application/json")
70+
3971
def http_request(
4072
self,
4173
method: str,

0 commit comments

Comments
 (0)