Skip to content

Commit ba2ee6c

Browse files
author
Liora Milbaum
committed
feat: Bootstrapping the http_engines concept
1 parent 0ecf3bb commit ba2ee6c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

gitlab/client.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import gitlab.config
1515
import gitlab.const
1616
import gitlab.exceptions
17-
from gitlab import utils
17+
from gitlab import http_backends, utils
1818

1919
REDIRECT_MSG = (
2020
"python-gitlab detected a {status_code} ({reason!r}) redirection. You must update "
@@ -32,6 +32,13 @@
3232

3333

3434
class Gitlab:
35+
36+
# The session object is defined here as long as the RequestsBackend is not
37+
# fully implemented. The long-term goal is to wrap all the http
38+
# requests/responses by the RequestsBackend class
39+
40+
session: requests.Session
41+
3542
"""Represents a GitLab server connection.
3643
3744
Args:
@@ -53,6 +60,10 @@ class Gitlab:
5360
or 52x responses. Defaults to False.
5461
keep_base_url: keep user-provided base URL for pagination if it
5562
differs from response headers
63+
64+
Keyward Args:
65+
requests.Session session: Http Requests Session
66+
RequestsBackend http_backend: Backend that will be used to make http requests
5667
"""
5768

5869
def __init__(
@@ -66,15 +77,14 @@ def __init__(
6677
http_password: Optional[str] = None,
6778
timeout: Optional[float] = None,
6879
api_version: str = "4",
69-
session: Optional[requests.Session] = None,
7080
per_page: Optional[int] = None,
7181
pagination: Optional[str] = None,
7282
order_by: Optional[str] = None,
7383
user_agent: str = gitlab.const.USER_AGENT,
7484
retry_transient_errors: bool = False,
7585
keep_base_url: bool = False,
86+
**kwargs: Any,
7687
) -> None:
77-
7888
self._api_version = str(api_version)
7989
self._server_version: Optional[str] = None
8090
self._server_revision: Optional[str] = None
@@ -98,7 +108,9 @@ def __init__(
98108
self._set_auth_info()
99109

100110
#: Create a session object for requests
101-
self.session = session or requests.Session()
111+
http_backend = kwargs.pop("http_backend", http_backends.DefaultBackend)
112+
self.http_backend = http_backend(**kwargs)
113+
self.session = self.http_backend.client
102114

103115
self.per_page = per_page
104116
self.pagination = pagination

gitlab/http_backends/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Defines http backends for processing http requests
3+
"""
4+
5+
from .requests_backend import RequestsBackend
6+
7+
DefaultBackend = RequestsBackend
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Optional
2+
3+
import requests
4+
5+
6+
class RequestsBackend:
7+
8+
_client: requests.Session
9+
10+
def __init__(self, session: Optional[requests.Session] = None) -> None:
11+
self._client = session or requests.Session()
12+
13+
@property
14+
def client(self) -> requests.Session:
15+
return self._client

0 commit comments

Comments
 (0)