Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb375a1a

Browse files
author
Liora Milbaum
committed
refactor: Migrate MultipartEncoder to RequestsBackend
1 parent18f4148 commitb375a1a

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

‎gitlab/client.py

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

99
importrequests
1010
importrequests.utils
11-
fromrequests_toolbelt.multipart.encoderimportMultipartEncoder# type: ignore
1211

1312
importgitlab
1413
importgitlab.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-
iffiles:
651-
ifpost_dataisNone:
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-
ifTYPE_CHECKING:
657-
assertisinstance(post_data,dict)
658-
fork,vinpost_data.items():
659-
ifisinstance(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-
ifrawandpost_data:
668-
return (None,post_data,"application/octet-stream")
669-
670-
return (post_data,None,"application/json")
671-
672639
defhttp_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
@@ -779,46 +748,42 @@ def http_request(
779748
if200<=result.status_code<300:
780749
returnresult.response
781750

782-
if (429==result.response.status_codeandobey_rate_limit)or (
783-
result.response.status_code
784-
ingitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES
751+
if (429==result.status_codeandobey_rate_limit)or (
752+
result.status_codeingitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES
785753
andretry_transient_errors
786754
):
787755
# Response headers documentation:
788756
# https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
789757
ifmax_retries==-1orcur_retries<max_retries:
790758
wait_time=2**cur_retries*0.1
791-
if"Retry-After"inresult.response.headers:
792-
wait_time=int(result.response.headers["Retry-After"])
793-
elif"RateLimit-Reset"inresult.response.headers:
794-
wait_time= (
795-
int(result.response.headers["RateLimit-Reset"])
796-
-time.time()
797-
)
759+
if"Retry-After"inresult.headers:
760+
wait_time=int(result.headers["Retry-After"])
761+
elif"RateLimit-Reset"inresult.headers:
762+
wait_time=int(result.headers["RateLimit-Reset"])-time.time()
798763
cur_retries+=1
799764
time.sleep(wait_time)
800765
continue
801766

802-
error_message=result.response.content
767+
error_message=result.content
803768
try:
804-
error_json=result.response.json()
769+
error_json=result.json()
805770
forkin ("message","error"):
806771
ifkinerror_json:
807772
error_message=error_json[k]
808773
except (KeyError,ValueError,TypeError):
809774
pass
810775

811-
ifresult.response.status_code==401:
776+
ifresult.status_code==401:
812777
raisegitlab.exceptions.GitlabAuthenticationError(
813-
response_code=result.response.status_code,
778+
response_code=result.status_code,
814779
error_message=error_message,
815-
response_body=result.response.content,
780+
response_body=result.content,
816781
)
817782

818783
raisegitlab.exceptions.GitlabHttpError(
819-
response_code=result.response.status_code,
784+
response_code=result.status_code,
820785
error_message=error_message,
821-
response_body=result.response.content,
786+
response_body=result.content,
822787
)
823788

824789
defhttp_get(

‎gitlab/http_backends/requests_backend.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__importannotations
22

3-
fromtypingimportAny,Dict,Optional,Union
3+
fromtypingimportAny,Dict,Optional,Tuple,TYPE_CHECKING,Union
44

55
importrequests
66
fromrequests.structuresimportCaseInsensitiveDict
@@ -39,6 +39,38 @@ def __init__(self, session: Optional[requests.Session] = None) -> None:
3939
defclient(self)->requests.Session:
4040
returnself._client
4141

42+
@staticmethod
43+
defprepare_send_data(
44+
files:Optional[Dict[str,Any]]=None,
45+
post_data:Optional[Union[Dict[str,Any],bytes]]=None,
46+
raw:bool=False,
47+
)->Tuple[
48+
Optional[Union[Dict[str,Any],bytes]],
49+
Optional[Union[Dict[str,Any],MultipartEncoder]],
50+
str,
51+
]:
52+
iffiles:
53+
ifpost_dataisNone:
54+
post_data= {}
55+
else:
56+
# booleans does not exists for data (neither for MultipartEncoder):
57+
# cast to string int to avoid: 'bool' object has no attribute 'encode'
58+
ifTYPE_CHECKING:
59+
assertisinstance(post_data,dict)
60+
fork,vinpost_data.items():
61+
ifisinstance(v,bool):
62+
post_data[k]=str(int(v))
63+
post_data["file"]=files.get("file")
64+
post_data["avatar"]=files.get("avatar")
65+
66+
data=MultipartEncoder(post_data)
67+
return (None,data,data.content_type)
68+
69+
ifrawandpost_data:
70+
return (None,post_data,"application/octet-stream")
71+
72+
return (post_data,None,"application/json")
73+
4274
defhttp_request(
4375
self,
4476
method:str,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp