Skip to content

Commit c9888ef

Browse files
author
Liora Milbaum
committed
feat: Bootstrapping the http_engines concept
1 parent faf842e commit c9888ef

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

gitlab/client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import re
55
import time
6-
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
6+
from typing import Any, cast, Dict, List, Optional, Tuple, Type, TYPE_CHECKING, Union
77
from urllib import parse
88

99
import requests
@@ -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,7 @@
3232

3333

3434
class Gitlab:
35+
3536
"""Represents a GitLab server connection.
3637
3738
Args:
@@ -53,6 +54,10 @@ class Gitlab:
5354
or 52x responses. Defaults to False.
5455
keep_base_url: keep user-provided base URL for pagination if it
5556
differs from response headers
57+
58+
Keyward Args:
59+
requests.Session session: Http Requests Session
60+
RequestsBackend http_backend: Backend that will be used to make http requests
5661
"""
5762

5863
def __init__(
@@ -66,15 +71,14 @@ def __init__(
6671
http_password: Optional[str] = None,
6772
timeout: Optional[float] = None,
6873
api_version: str = "4",
69-
session: Optional[requests.Session] = None,
7074
per_page: Optional[int] = None,
7175
pagination: Optional[str] = None,
7276
order_by: Optional[str] = None,
7377
user_agent: str = gitlab.const.USER_AGENT,
7478
retry_transient_errors: bool = False,
7579
keep_base_url: bool = False,
80+
**kwargs: Any,
7681
) -> None:
77-
7882
self._api_version = str(api_version)
7983
self._server_version: Optional[str] = None
8084
self._server_revision: Optional[str] = None
@@ -98,7 +102,11 @@ def __init__(
98102
self._set_auth_info()
99103

100104
#: Create a session object for requests
101-
self.session = session or requests.Session()
105+
http_backend: Type[http_backends.DefaultBackend] = kwargs.pop(
106+
"http_backend", http_backends.DefaultBackend
107+
)
108+
self.http_backend = http_backend(**kwargs)
109+
self.session = self.http_backend.client
102110

103111
self.per_page = per_page
104112
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Optional
2+
3+
import requests
4+
5+
6+
class RequestsBackend:
7+
def __init__(self, session: Optional[requests.Session] = None) -> None:
8+
self._client: requests.Session = session or requests.Session()
9+
10+
@property
11+
def client(self) -> requests.Session:
12+
return self._client

0 commit comments

Comments
 (0)