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

Commit7476d56

Browse files
author
Liora Milbaum
committed
refactor: Replacing http_requests return type hint
1 parent1da7c53 commit7476d56

File tree

2 files changed

+89
-35
lines changed

2 files changed

+89
-35
lines changed

‎docs/api-levels.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Lower-lower-level API - HTTP requests
7474
higher-level APIs. To lessen the chances of a change to the interface impacting your code, we
7575
recommend using keyword arguments when calling the interfaces.
7676

77-
At the lowest level, HTTP methods call ``http_request()``, which performs the actual request and takes
77+
At the lowest level, HTTP methods call ``backend_request()``, which performs the actual request and takes
7878
care of details such as timeouts, retries, and handling rate-limits.
7979

8080
This method can be invoked directly to or customize this behavior for a single request, or to call custom
@@ -87,19 +87,19 @@ For example, if for whatever reason you want to fetch allowed methods for an end
8787
8888
>>> gl= gitlab.Gitlab(private_token=private_token)
8989
>>>
90-
>>>response= gl.http_request(verb="OPTIONS",path="/projects")
91-
>>> response.headers["Allow"]
90+
>>>backend_response= gl.backend_request(verb="OPTIONS",path="/projects")
91+
>>>backend_response.response.headers["Allow"]
9292
'OPTIONS, GET, POST, HEAD'
9393
9494
Or get the total number of a user's events with a customized HEAD request:
9595

9696
..code-block::python
9797
98-
>>>response= gl.http_request(
98+
>>>backend_response= gl.backend_request(
9999
verb="HEAD",
100100
path="/events",
101101
query_params={"sudo":"some-user"},
102102
timeout=10
103103
)
104-
>>> response.headers["X-Total"]
104+
>>>backend_response.response.headers["X-Total"]
105105
'123'

‎gitlab/client.py

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def _check_redirects(result: requests.Response) -> None:
639639
)
640640
)
641641

642-
defhttp_request(
642+
defbackend_request(
643643
self,
644644
verb:str,
645645
path:str,
@@ -653,7 +653,7 @@ def http_request(
653653
retry_transient_errors:Optional[bool]=None,
654654
max_retries:int=10,
655655
**kwargs:Any,
656-
)->requests.Response:
656+
)->_backends.DefaultResponse:
657657
"""Make an HTTP request to the Gitlab server.
658658
659659
Args:
@@ -724,7 +724,7 @@ def http_request(
724724
cur_retries=0
725725
whileTrue:
726726
try:
727-
result=self._backend.http_request(
727+
backend_response=self._backend.http_request(
728728
method=verb,
729729
url=url,
730730
json=json,
@@ -746,20 +746,26 @@ def http_request(
746746

747747
raise
748748

749-
self._check_redirects(result.response)
749+
self._check_redirects(backend_response.response)
750750

751-
if200<=result.status_code<300:
752-
returnresult.response
751+
if200<=backend_response.status_code<300:
752+
returnbackend_response
753753

754754
defshould_retry()->bool:
755-
ifresult.status_code==429andobey_rate_limit:
755+
ifbackend_response.status_code==429andobey_rate_limit:
756756
returnTrue
757757

758758
ifnotretry_transient_errors:
759759
returnFalse
760-
ifresult.status_codeingitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES:
760+
if (
761+
backend_response.status_code
762+
ingitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES
763+
):
761764
returnTrue
762-
ifresult.status_code==409and"Resource lock"inresult.reason:
765+
if (
766+
backend_response.status_code==409
767+
and"Resource lock"inbackend_response.reason
768+
):
763769
returnTrue
764770

765771
returnFalse
@@ -769,36 +775,74 @@ def should_retry() -> bool:
769775
# https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
770776
ifmax_retries==-1orcur_retries<max_retries:
771777
wait_time=2**cur_retries*0.1
772-
if"Retry-After"inresult.headers:
773-
wait_time=int(result.headers["Retry-After"])
774-
elif"RateLimit-Reset"inresult.headers:
775-
wait_time=int(result.headers["RateLimit-Reset"])-time.time()
778+
if"Retry-After"inbackend_response.headers:
779+
wait_time=int(backend_response.headers["Retry-After"])
780+
elif"RateLimit-Reset"inbackend_response.headers:
781+
wait_time= (
782+
int(backend_response.headers["RateLimit-Reset"])
783+
-time.time()
784+
)
776785
cur_retries+=1
777786
time.sleep(wait_time)
778787
continue
779788

780-
error_message=result.content
789+
error_message=backend_response.content
781790
try:
782-
error_json=result.json()
791+
error_json=backend_response.json()
783792
forkin ("message","error"):
784793
ifkinerror_json:
785794
error_message=error_json[k]
786795
except (KeyError,ValueError,TypeError):
787796
pass
788797

789-
ifresult.status_code==401:
798+
ifbackend_response.status_code==401:
790799
raisegitlab.exceptions.GitlabAuthenticationError(
791-
response_code=result.status_code,
800+
response_code=backend_response.status_code,
792801
error_message=error_message,
793-
response_body=result.content,
802+
response_body=backend_response.content,
794803
)
795804

796805
raisegitlab.exceptions.GitlabHttpError(
797-
response_code=result.status_code,
806+
response_code=backend_response.status_code,
798807
error_message=error_message,
799-
response_body=result.content,
808+
response_body=backend_response.content,
800809
)
801810

811+
defhttp_request(
812+
self,
813+
verb:str,
814+
path:str,
815+
query_data:Optional[Dict[str,Any]]=None,
816+
post_data:Optional[Union[Dict[str,Any],bytes]]=None,
817+
raw:bool=False,
818+
streamed:bool=False,
819+
files:Optional[Dict[str,Any]]=None,
820+
timeout:Optional[float]=None,
821+
obey_rate_limit:bool=True,
822+
retry_transient_errors:Optional[bool]=None,
823+
max_retries:int=10,
824+
**kwargs:Any,
825+
)->requests.Response:
826+
utils.warn(
827+
"`http_request()` is deprecated and will be removed in a future version.\n"
828+
"Please use `backend_request()` instead.",
829+
category=DeprecationWarning,
830+
)
831+
returnself.backend_request(
832+
verb,
833+
path,
834+
query_data,
835+
post_data,
836+
raw,
837+
streamed,
838+
files,
839+
timeout,
840+
obey_rate_limit,
841+
retry_transient_errors,
842+
max_retries,
843+
**kwargs,
844+
).response
845+
802846
defhttp_get(
803847
self,
804848
path:str,
@@ -827,9 +871,10 @@ def http_get(
827871
GitlabParsingError: If the json data could not be parsed
828872
"""
829873
query_data=query_dataor {}
830-
result=self.http_request(
874+
backend_response=self.backend_request(
831875
"get",path,query_data=query_data,streamed=streamed,**kwargs
832876
)
877+
result=backend_response.response
833878

834879
if (
835880
result.headers["Content-Type"]=="application/json"
@@ -866,8 +911,10 @@ def http_head(
866911
"""
867912

868913
query_data=query_dataor {}
869-
result=self.http_request("head",path,query_data=query_data,**kwargs)
870-
returnresult.headers
914+
backend_response=self.http_request(
915+
"head",path,query_data=query_data,**kwargs
916+
)
917+
returnbackend_response.headers
871918

872919
defhttp_list(
873920
self,
@@ -1023,7 +1070,7 @@ def http_post(
10231070
query_data=query_dataor {}
10241071
post_data=post_dataor {}
10251072

1026-
result=self.http_request(
1073+
backend_response=self.backend_request(
10271074
"post",
10281075
path,
10291076
query_data=query_data,
@@ -1032,6 +1079,8 @@ def http_post(
10321079
raw=raw,
10331080
**kwargs,
10341081
)
1082+
result=backend_response.response
1083+
10351084
try:
10361085
ifresult.headers.get("Content-Type",None)=="application/json":
10371086
json_result=result.json()
@@ -1075,7 +1124,7 @@ def http_put(
10751124
query_data=query_dataor {}
10761125
post_data=post_dataor {}
10771126

1078-
result=self.http_request(
1127+
backend_response=self.http_request(
10791128
"put",
10801129
path,
10811130
query_data=query_data,
@@ -1085,7 +1134,7 @@ def http_put(
10851134
**kwargs,
10861135
)
10871136
try:
1088-
json_result=result.json()
1137+
json_result=backend_response.json()
10891138
ifTYPE_CHECKING:
10901139
assertisinstance(json_result,dict)
10911140
returnjson_result
@@ -1124,7 +1173,7 @@ def http_patch(
11241173
query_data=query_dataor {}
11251174
post_data=post_dataor {}
11261175

1127-
result=self.http_request(
1176+
backend_response=self.http_request(
11281177
"patch",
11291178
path,
11301179
query_data=query_data,
@@ -1133,7 +1182,7 @@ def http_patch(
11331182
**kwargs,
11341183
)
11351184
try:
1136-
json_result=result.json()
1185+
json_result=backend_response.json()
11371186
ifTYPE_CHECKING:
11381187
assertisinstance(json_result,dict)
11391188
returnjson_result
@@ -1156,7 +1205,8 @@ def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
11561205
Raises:
11571206
GitlabHttpError: When the return code is not 2xx
11581207
"""
1159-
returnself.http_request("delete",path,**kwargs)
1208+
backend_response=self.backend_request("delete",path,**kwargs)
1209+
returnbackend_response.response
11601210

11611211
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError)
11621212
defsearch(
@@ -1210,7 +1260,11 @@ def _query(
12101260
self,url:str,query_data:Optional[Dict[str,Any]]=None,**kwargs:Any
12111261
)->None:
12121262
query_data=query_dataor {}
1213-
result=self._gl.http_request("get",url,query_data=query_data,**kwargs)
1263+
backend_response=self._gl.backend_request(
1264+
"get",url,query_data=query_data,**kwargs
1265+
)
1266+
result=backend_response.response
1267+
12141268
try:
12151269
next_url=result.links["next"]["url"]
12161270
exceptKeyError:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp