Skip to content

Commit 7358573

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

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

docs/api-levels.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.http_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.http_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: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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,34 +775,37 @@ 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

802811
def http_get(
@@ -827,9 +836,10 @@ def http_get(
827836
GitlabParsingError: If the json data could not be parsed
828837
"""
829838
query_data = query_data or {}
830-
result = self.http_request(
839+
backend_response = self.http_request(
831840
"get", path, query_data=query_data, streamed=streamed, **kwargs
832841
)
842+
result = backend_response.response
833843

834844
if (
835845
result.headers["Content-Type"] == "application/json"
@@ -866,8 +876,10 @@ def http_head(
866876
"""
867877

868878
query_data = query_data or {}
869-
result = self.http_request("head", path, query_data=query_data, **kwargs)
870-
return result.headers
879+
backend_response = self.http_request(
880+
"head", path, query_data=query_data, **kwargs
881+
)
882+
return backend_response.headers
871883

872884
def http_list(
873885
self,
@@ -1023,7 +1035,7 @@ def http_post(
10231035
query_data = query_data or {}
10241036
post_data = post_data or {}
10251037

1026-
result = self.http_request(
1038+
backend_response = self.http_request(
10271039
"post",
10281040
path,
10291041
query_data=query_data,
@@ -1032,6 +1044,8 @@ def http_post(
10321044
raw=raw,
10331045
**kwargs,
10341046
)
1047+
result = backend_response.response
1048+
10351049
try:
10361050
if result.headers.get("Content-Type", None) == "application/json":
10371051
json_result = result.json()
@@ -1075,7 +1089,7 @@ def http_put(
10751089
query_data = query_data or {}
10761090
post_data = post_data or {}
10771091

1078-
result = self.http_request(
1092+
backend_response = self.http_request(
10791093
"put",
10801094
path,
10811095
query_data=query_data,
@@ -1085,7 +1099,7 @@ def http_put(
10851099
**kwargs,
10861100
)
10871101
try:
1088-
json_result = result.json()
1102+
json_result = backend_response.json()
10891103
if TYPE_CHECKING:
10901104
assert isinstance(json_result, dict)
10911105
return json_result
@@ -1124,7 +1138,7 @@ def http_patch(
11241138
query_data = query_data or {}
11251139
post_data = post_data or {}
11261140

1127-
result = self.http_request(
1141+
backend_response = self.http_request(
11281142
"patch",
11291143
path,
11301144
query_data=query_data,
@@ -1133,7 +1147,7 @@ def http_patch(
11331147
**kwargs,
11341148
)
11351149
try:
1136-
json_result = result.json()
1150+
json_result = backend_response.json()
11371151
if TYPE_CHECKING:
11381152
assert isinstance(json_result, dict)
11391153
return json_result
@@ -1156,7 +1170,8 @@ def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
11561170
Raises:
11571171
GitlabHttpError: When the return code is not 2xx
11581172
"""
1159-
return self.http_request("delete", path, **kwargs)
1173+
backend_response = self.http_request("delete", path, **kwargs)
1174+
return backend_response.response
11601175

11611176
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError)
11621177
def search(
@@ -1210,7 +1225,11 @@ def _query(
12101225
self, url: str, query_data: Optional[Dict[str, Any]] = None, **kwargs: Any
12111226
) -> None:
12121227
query_data = query_data or {}
1213-
result = self._gl.http_request("get", url, query_data=query_data, **kwargs)
1228+
backend_response = self._gl.http_request(
1229+
"get", url, query_data=query_data, **kwargs
1230+
)
1231+
result = backend_response.response
1232+
12141233
try:
12151234
next_url = result.links["next"]["url"]
12161235
except KeyError:

0 commit comments

Comments
 (0)