Skip to content

Commit c2e3452

Browse files
author
Liora Milbaum
committed
refactor: Replacing http_requests return type hint
1 parent aee73d0 commit c2e3452

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.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.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-
def http_request(
642+
def backend_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
while True:
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-
if 200 <= result.status_code < 300:
752-
return result.response
751+
if 200 <= backend_response.status_code < 300:
752+
return backend_response
753753

754754
def should_retry() -> bool:
755-
if result.status_code == 429 and obey_rate_limit:
755+
if backend_response.status_code == 429 and obey_rate_limit:
756756
return True
757757

758758
if not retry_transient_errors:
759759
return False
760-
if result.status_code in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES:
760+
if (
761+
backend_response.status_code
762+
in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES
763+
):
761764
return True
762-
if result.status_code == 409 and "Resource lock" in result.reason:
765+
if (
766+
backend_response.status_code == 409
767+
and "Resource lock" in backend_response.reason
768+
):
763769
return True
764770

765771
return False
@@ -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
if max_retries == -1 or cur_retries < max_retries:
771777
wait_time = 2**cur_retries * 0.1
772-
if "Retry-After" in result.headers:
773-
wait_time = int(result.headers["Retry-After"])
774-
elif "RateLimit-Reset" in result.headers:
775-
wait_time = int(result.headers["RateLimit-Reset"]) - time.time()
778+
if "Retry-After" in backend_response.headers:
779+
wait_time = int(backend_response.headers["Retry-After"])
780+
elif "RateLimit-Reset" in backend_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
for k in ("message", "error"):
784793
if k in error_json:
785794
error_message = error_json[k]
786795
except (KeyError, ValueError, TypeError):
787796
pass
788797

789-
if result.status_code == 401:
798+
if backend_response.status_code == 401:
790799
raise gitlab.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
raise gitlab.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+
def http_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+
return self.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
def http_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_data or {}
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_data or {}
869-
result = self.http_request("head", path, query_data=query_data, **kwargs)
870-
return result.headers
914+
backend_response = self.http_request(
915+
"head", path, query_data=query_data, **kwargs
916+
)
917+
return backend_response.headers
871918

872919
def http_list(
873920
self,
@@ -1023,7 +1070,7 @@ def http_post(
10231070
query_data = query_data or {}
10241071
post_data = post_data or {}
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
if result.headers.get("Content-Type", None) == "application/json":
10371086
json_result = result.json()
@@ -1075,7 +1124,7 @@ def http_put(
10751124
query_data = query_data or {}
10761125
post_data = post_data or {}
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
if TYPE_CHECKING:
10901139
assert isinstance(json_result, dict)
10911140
return json_result
@@ -1124,7 +1173,7 @@ def http_patch(
11241173
query_data = query_data or {}
11251174
post_data = post_data or {}
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
if TYPE_CHECKING:
11381187
assert isinstance(json_result, dict)
11391188
return json_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-
return self.http_request("delete", path, **kwargs)
1208+
backend_response = self.backend_request("delete", path, **kwargs)
1209+
return backend_response.response
11601210

11611211
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError)
11621212
def search(
@@ -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_data or {}
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
except KeyError:

0 commit comments

Comments
 (0)