diff options
| author | Liora Milbaum <liora@lmb.co.il> | 2022-12-05 12:26:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-05 11:26:44 +0100 |
| commit | 91a665f331c3ffc260db3470ad71fde0d3b56aa2 (patch) | |
| tree | 6b282e8a8f93b164a8a52dd2a4002cf680a6ab8d /gitlab | |
| parent | faf842e97d4858ff5ebd8ae6996e0cb3ca29881c (diff) | |
| download | gitlab-91a665f331c3ffc260db3470ad71fde0d3b56aa2.tar.gz | |
feat(client): bootstrap the http backends concept (#2391)
Diffstat (limited to 'gitlab')
| -rw-r--r-- | gitlab/client.py | 18 | ||||
| -rw-r--r-- | gitlab/http_backends/__init__.py | 7 | ||||
| -rw-r--r-- | gitlab/http_backends/requests_backend.py | 12 |
3 files changed, 32 insertions, 5 deletions
diff --git a/gitlab/client.py b/gitlab/client.py index 49882d6..8514ffd 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -3,7 +3,7 @@ import os import re import time -from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union +from typing import Any, cast, Dict, List, Optional, Tuple, Type, TYPE_CHECKING, Union from urllib import parse import requests @@ -14,7 +14,7 @@ import gitlab import gitlab.config import gitlab.const import gitlab.exceptions -from gitlab import utils +from gitlab import http_backends, utils REDIRECT_MSG = ( "python-gitlab detected a {status_code} ({reason!r}) redirection. You must update " @@ -32,6 +32,7 @@ _PAGINATION_URL = ( class Gitlab: + """Represents a GitLab server connection. Args: @@ -53,6 +54,10 @@ class Gitlab: or 52x responses. Defaults to False. keep_base_url: keep user-provided base URL for pagination if it differs from response headers + + Keyward Args: + requests.Session session: Http Requests Session + RequestsBackend http_backend: Backend that will be used to make http requests """ def __init__( @@ -66,15 +71,14 @@ class Gitlab: http_password: Optional[str] = None, timeout: Optional[float] = None, api_version: str = "4", - session: Optional[requests.Session] = None, per_page: Optional[int] = None, pagination: Optional[str] = None, order_by: Optional[str] = None, user_agent: str = gitlab.const.USER_AGENT, retry_transient_errors: bool = False, keep_base_url: bool = False, + **kwargs: Any, ) -> None: - self._api_version = str(api_version) self._server_version: Optional[str] = None self._server_revision: Optional[str] = None @@ -98,7 +102,11 @@ class Gitlab: self._set_auth_info() #: Create a session object for requests - self.session = session or requests.Session() + http_backend: Type[http_backends.DefaultBackend] = kwargs.pop( + "http_backend", http_backends.DefaultBackend + ) + self.http_backend = http_backend(**kwargs) + self.session = self.http_backend.client self.per_page = per_page self.pagination = pagination diff --git a/gitlab/http_backends/__init__.py b/gitlab/http_backends/__init__.py new file mode 100644 index 0000000..07b7e80 --- /dev/null +++ b/gitlab/http_backends/__init__.py @@ -0,0 +1,7 @@ +""" +Defines http backends for processing http requests +""" + +from .requests_backend import RequestsBackend + +DefaultBackend = RequestsBackend diff --git a/gitlab/http_backends/requests_backend.py b/gitlab/http_backends/requests_backend.py new file mode 100644 index 0000000..eecbfdd --- /dev/null +++ b/gitlab/http_backends/requests_backend.py @@ -0,0 +1,12 @@ +from typing import Optional + +import requests + + +class RequestsBackend: + def __init__(self, session: Optional[requests.Session] = None) -> None: + self._client: requests.Session = session or requests.Session() + + @property + def client(self) -> requests.Session: + return self._client |
