From 133e6a0b3c954943788c8f00aafbe4521e5076ed Mon Sep 17 00:00:00 2001 From: Liora Milbaum Date: Tue, 6 Dec 2022 15:17:10 +0200 Subject: [PATCH] refactor: Moving the request call to the backend --- gitlab/client.py | 2 +- gitlab/http_backends/requests_backend.py | 43 +++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/gitlab/client.py b/gitlab/client.py index 5e1db0a0a..98f690578 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -752,7 +752,7 @@ def http_request( cur_retries = 0 while True: try: - result = self.session.request( + result = self.http_backend.http_request( method=verb, url=url, json=json, diff --git a/gitlab/http_backends/requests_backend.py b/gitlab/http_backends/requests_backend.py index eecbfdd19..032597a57 100644 --- a/gitlab/http_backends/requests_backend.py +++ b/gitlab/http_backends/requests_backend.py @@ -1,6 +1,7 @@ -from typing import Optional +from typing import Any, Dict, Optional, Union import requests +from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore class RequestsBackend: @@ -10,3 +11,43 @@ def __init__(self, session: Optional[requests.Session] = None) -> None: @property def client(self) -> requests.Session: return self._client + + def http_request( + self, + method: str, + url: str, + json: Optional[Union[Dict[str, Any], bytes]] = None, + data: Optional[Union[Dict[str, Any], MultipartEncoder]] = None, + params: Optional[Any] = None, + timeout: Optional[float] = None, + verify: Optional[Union[bool, str]] = True, + stream: Optional[bool] = False, + **kwargs: Any + ) -> requests.Response: + """Make HTTP request + + Args: + method: The HTTP method to call ('get', 'post', 'put', 'delete', etc.) + url: The full URL + data: The data to send to the server in the body of the request + json: Data to send in the body in json by default + timeout: The timeout, in seconds, for the request + verify: Whether SSL certificates should be validated. If + the value is a string, it is the path to a CA file used for + certificate validation. + stream: Whether the data should be streamed + + Returns: + A requests Response object. + """ + return self._client.request( + method=method, + url=url, + params=params, + data=data, + timeout=timeout, + stream=stream, + verify=verify, + json=json, + **kwargs + )