Skip to content

Commit c264a12

Browse files
author
Liora Milbaum
committed
refactor: Replacing http_requests return type hint
1 parent 14e0f65 commit c264a12

File tree

2 files changed

+94
-37
lines changed

2 files changed

+94
-37
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: 89 additions & 32 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:
@@ -722,7 +722,7 @@ def http_request(
722722
cur_retries = 0
723723
while True:
724724
try:
725-
result = self._backend.http_request(
725+
backend_response = self._backend.http_request(
726726
method=verb,
727727
url=url,
728728
json=send_data.json,
@@ -744,20 +744,26 @@ def http_request(
744744

745745
raise
746746

747-
self._check_redirects(result.response)
747+
self._check_redirects(backend_response.response)
748748

749-
if 200 <= result.status_code < 300:
750-
return result.response
749+
if 200 <= backend_response.status_code < 300:
750+
return backend_response
751751

752752
def should_retry() -> bool:
753-
if result.status_code == 429 and obey_rate_limit:
753+
if backend_response.status_code == 429 and obey_rate_limit:
754754
return True
755755

756756
if not retry_transient_errors:
757757
return False
758-
if result.status_code in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES:
758+
if (
759+
backend_response.status_code
760+
in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES
761+
):
759762
return True
760-
if result.status_code == 409 and "Resource lock" in result.reason:
763+
if (
764+
backend_response.status_code == 409
765+
and "Resource lock" in backend_response.reason
766+
):
761767
return True
762768

763769
return False
@@ -767,36 +773,74 @@ def should_retry() -> bool:
767773
# https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
768774
if max_retries == -1 or cur_retries < max_retries:
769775
wait_time = 2**cur_retries * 0.1
770-
if "Retry-After" in result.headers:
771-
wait_time = int(result.headers["Retry-After"])
772-
elif "RateLimit-Reset" in result.headers:
773-
wait_time = int(result.headers["RateLimit-Reset"]) - time.time()
776+
if "Retry-After" in backend_response.headers:
777+
wait_time = int(backend_response.headers["Retry-After"])
778+
elif "RateLimit-Reset" in backend_response.headers:
779+
wait_time = (
780+
int(backend_response.headers["RateLimit-Reset"])
781+
- time.time()
782+
)
774783
cur_retries += 1
775784
time.sleep(wait_time)
776785
continue
777786

778-
error_message = result.content
787+
error_message = backend_response.content
779788
try:
780-
error_json = result.json()
789+
error_json = backend_response.json()
781790
for k in ("message", "error"):
782791
if k in error_json:
783792
error_message = error_json[k]
784793
except (KeyError, ValueError, TypeError):
785794
pass
786795

787-
if result.status_code == 401:
796+
if backend_response.status_code == 401:
788797
raise gitlab.exceptions.GitlabAuthenticationError(
789-
response_code=result.status_code,
798+
response_code=backend_response.status_code,
790799
error_message=error_message,
791-
response_body=result.content,
800+
response_body=backend_response.content,
792801
)
793802

794803
raise gitlab.exceptions.GitlabHttpError(
795-
response_code=result.status_code,
804+
response_code=backend_response.status_code,
796805
error_message=error_message,
797-
response_body=result.content,
806+
response_body=backend_response.content,
798807
)
799808

809+
def http_request(
810+
self,
811+
verb: str,
812+
path: str,
813+
query_data: Optional[Dict[str, Any]] = None,
814+
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
815+
raw: bool = False,
816+
streamed: bool = False,
817+
files: Optional[Dict[str, Any]] = None,
818+
timeout: Optional[float] = None,
819+
obey_rate_limit: bool = True,
820+
retry_transient_errors: Optional[bool] = None,
821+
max_retries: int = 10,
822+
**kwargs: Any,
823+
) -> requests.Response:
824+
utils.warn(
825+
"`http_request()` is deprecated and will be removed in a future version.\n"
826+
"Please use `backend_request()` instead.",
827+
category=DeprecationWarning,
828+
)
829+
return self.backend_request(
830+
verb,
831+
path,
832+
query_data,
833+
post_data,
834+
raw,
835+
streamed,
836+
files,
837+
timeout,
838+
obey_rate_limit,
839+
retry_transient_errors,
840+
max_retries,
841+
**kwargs,
842+
).response
843+
800844
def http_get(
801845
self,
802846
path: str,
@@ -825,10 +869,13 @@ def http_get(
825869
GitlabParsingError: If the json data could not be parsed
826870
"""
827871
query_data = query_data or {}
828-
result = self.http_request(
872+
backend_response = self.backend_request(
829873
"get", path, query_data=query_data, streamed=streamed, **kwargs
830874
)
831-
content_type = utils.get_content_type(result.headers.get("Content-Type"))
875+
content_type = utils.get_content_type(
876+
backend_response.headers.get("Content-Type")
877+
)
878+
result = backend_response.response
832879

833880
if content_type == "application/json" and not streamed and not raw:
834881
try:
@@ -861,8 +908,10 @@ def http_head(
861908
"""
862909

863910
query_data = query_data or {}
864-
result = self.http_request("head", path, query_data=query_data, **kwargs)
865-
return result.headers
911+
backend_response = self.http_request(
912+
"head", path, query_data=query_data, **kwargs
913+
)
914+
return backend_response.headers
866915

867916
def http_list(
868917
self,
@@ -1018,7 +1067,7 @@ def http_post(
10181067
query_data = query_data or {}
10191068
post_data = post_data or {}
10201069

1021-
result = self.http_request(
1070+
backend_response = self.backend_request(
10221071
"post",
10231072
path,
10241073
query_data=query_data,
@@ -1027,7 +1076,10 @@ def http_post(
10271076
raw=raw,
10281077
**kwargs,
10291078
)
1030-
content_type = utils.get_content_type(result.headers.get("Content-Type"))
1079+
content_type = utils.get_content_type(
1080+
backend_response.headers.get("Content-Type")
1081+
)
1082+
result = backend_response.response
10311083

10321084
try:
10331085
if content_type == "application/json":
@@ -1072,7 +1124,7 @@ def http_put(
10721124
query_data = query_data or {}
10731125
post_data = post_data or {}
10741126

1075-
result = self.http_request(
1127+
backend_response = self.http_request(
10761128
"put",
10771129
path,
10781130
query_data=query_data,
@@ -1082,7 +1134,7 @@ def http_put(
10821134
**kwargs,
10831135
)
10841136
try:
1085-
json_result = result.json()
1137+
json_result = backend_response.json()
10861138
if TYPE_CHECKING:
10871139
assert isinstance(json_result, dict)
10881140
return json_result
@@ -1121,7 +1173,7 @@ def http_patch(
11211173
query_data = query_data or {}
11221174
post_data = post_data or {}
11231175

1124-
result = self.http_request(
1176+
backend_response = self.http_request(
11251177
"patch",
11261178
path,
11271179
query_data=query_data,
@@ -1130,7 +1182,7 @@ def http_patch(
11301182
**kwargs,
11311183
)
11321184
try:
1133-
json_result = result.json()
1185+
json_result = backend_response.json()
11341186
if TYPE_CHECKING:
11351187
assert isinstance(json_result, dict)
11361188
return json_result
@@ -1153,7 +1205,8 @@ def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
11531205
Raises:
11541206
GitlabHttpError: When the return code is not 2xx
11551207
"""
1156-
return self.http_request("delete", path, **kwargs)
1208+
backend_response = self.backend_request("delete", path, **kwargs)
1209+
return backend_response.response
11571210

11581211
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError)
11591212
def search(
@@ -1207,7 +1260,11 @@ def _query(
12071260
self, url: str, query_data: Optional[Dict[str, Any]] = None, **kwargs: Any
12081261
) -> None:
12091262
query_data = query_data or {}
1210-
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+
12111268
try:
12121269
next_url = result.links["next"]["url"]
12131270
except KeyError:

0 commit comments

Comments
 (0)