Skip to content

refactor: move Response object to backends #2420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,10 @@ def http_request(

raise

self._check_redirects(result)
self._check_redirects(result.response)

if 200 <= result.status_code < 300:
return result
return result.response

if (429 == result.status_code and obey_rate_limit) or (
result.status_code in gitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES
Expand Down
3 changes: 2 additions & 1 deletion gitlab/http_backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Defines http backends for processing http requests
"""

from .requests_backend import RequestsBackend
from .requests_backend import RequestsBackend, RequestsResponse

DefaultBackend = RequestsBackend
DefaultResponse = RequestsResponse
36 changes: 32 additions & 4 deletions gitlab/http_backends/requests_backend.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
from __future__ import annotations

from typing import Any, Dict, Optional, Union

import requests
from requests.structures import CaseInsensitiveDict
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore


class RequestsResponse:
def __init__(self, response: requests.Response) -> None:
self._response: requests.Response = response

@property
def response(self) -> requests.Response:
return self._response

@property
def status_code(self) -> int:
return self._response.status_code

@property
def headers(self) -> CaseInsensitiveDict[str]:
return self._response.headers

@property
def content(self) -> bytes:
return self._response.content

def json(self) -> Any:
return self._response.json()


class RequestsBackend:
def __init__(self, session: Optional[requests.Session] = None) -> None:
self._client: requests.Session = session or requests.Session()
Expand All @@ -22,8 +49,8 @@ def http_request(
timeout: Optional[float] = None,
verify: Optional[Union[bool, str]] = True,
stream: Optional[bool] = False,
**kwargs: Any
) -> requests.Response:
**kwargs: Any,
) -> RequestsResponse:
"""Make HTTP request

Args:
Expand All @@ -40,7 +67,7 @@ def http_request(
Returns:
A requests Response object.
"""
return self._client.request(
response: requests.Response = self._client.request(
method=method,
url=url,
params=params,
Expand All @@ -49,5 +76,6 @@ def http_request(
stream=stream,
verify=verify,
json=json,
**kwargs
**kwargs,
)
return RequestsResponse(response=response)