diff options
| author | Liora Milbaum <liora@lmb.co.il> | 2022-12-04 11:12:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-04 10:12:13 +0100 |
| commit | e88d34e38dd930b00d7bb48f0e1c39420e09fa0f (patch) | |
| tree | b0bccafd1a91f38db668e2c4d8893dc1f144e145 | |
| parent | 50a03017f2ba8ec3252911dd1cf0ed7df42cfe50 (diff) | |
| download | gitlab-e88d34e38dd930b00d7bb48f0e1c39420e09fa0f.tar.gz | |
feat: allow passing kwargs to Gitlab class when instantiating with `from_config` (#2392)
| -rw-r--r-- | docs/api-usage-advanced.rst | 12 | ||||
| -rw-r--r-- | gitlab/client.py | 9 | ||||
| -rw-r--r-- | tests/functional/api/test_gitlab.py | 19 | ||||
| -rw-r--r-- | tests/unit/test_gitlab.py | 23 |
4 files changed, 62 insertions, 1 deletions
diff --git a/docs/api-usage-advanced.rst b/docs/api-usage-advanced.rst index 2382b49..2b069b8 100644 --- a/docs/api-usage-advanced.rst +++ b/docs/api-usage-advanced.rst @@ -181,3 +181,15 @@ on your own, such as for nested API responses and ``Union`` return types. For ex if TYPE_CHECKING: assert isinstance(license["plan"], str) + +Custom session (Bring your own Session) +--------------------------------------- + +You can use configuration files and a custom session to create +``gitlab.Gitlab`` objects: + +.. code-block:: python + + gl = gitlab.Gitlab.from_config('somewhere', ['/tmp/gl.cfg'], session=custom_session) + + diff --git a/gitlab/client.py b/gitlab/client.py index 9814fa2..49882d6 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -232,7 +232,10 @@ class Gitlab: @classmethod def from_config( - cls, gitlab_id: Optional[str] = None, config_files: Optional[List[str]] = None + cls, + gitlab_id: Optional[str] = None, + config_files: Optional[List[str]] = None, + **kwargs: Any, ) -> "Gitlab": """Create a Gitlab connection from configuration files. @@ -240,6 +243,9 @@ class Gitlab: gitlab_id: ID of the configuration section. config_files list[str]: List of paths to configuration files. + kwargs: + session requests.Session: Custom requests Session + Returns: A Gitlab connection. @@ -264,6 +270,7 @@ class Gitlab: order_by=config.order_by, user_agent=config.user_agent, retry_transient_errors=config.retry_transient_errors, + **kwargs, ) @classmethod diff --git a/tests/functional/api/test_gitlab.py b/tests/functional/api/test_gitlab.py index 55e029a..c2aba09 100644 --- a/tests/functional/api/test_gitlab.py +++ b/tests/functional/api/test_gitlab.py @@ -1,4 +1,5 @@ import pytest +import requests import gitlab @@ -23,6 +24,24 @@ def test_auth_from_config(gl, temp_dir): assert isinstance(test_gitlab.user, gitlab.v4.objects.CurrentUser) +def test_no_custom_session(gl, temp_dir): + """Test no custom session""" + custom_session = requests.Session() + test_gitlab = gitlab.Gitlab.from_config( + config_files=[temp_dir / "python-gitlab.cfg"] + ) + assert test_gitlab.session != custom_session + + +def test_custom_session(gl, temp_dir): + """Test custom session""" + custom_session = requests.Session() + test_gitlab = gitlab.Gitlab.from_config( + config_files=[temp_dir / "python-gitlab.cfg"], session=custom_session + ) + assert test_gitlab.session == custom_session + + def test_broadcast_messages(gl, get_all_kwargs): msg = gl.broadcastmessages.create({"message": "this is the message"}) msg.color = "#444444" diff --git a/tests/unit/test_gitlab.py b/tests/unit/test_gitlab.py index a4d558a..6d0a117 100644 --- a/tests/unit/test_gitlab.py +++ b/tests/unit/test_gitlab.py @@ -4,6 +4,7 @@ import pickle from http.client import HTTPConnection import pytest +import requests import responses import gitlab @@ -412,3 +413,25 @@ def test_gitlab_keep_base_url(kwargs, link_header, expected_next_url, show_warni else: obj = gl.http_list("/tests", iterator=True) assert obj._next_url == expected_next_url + + +def test_no_custom_session(default_config): + """Test no custom session""" + + config_path = default_config + custom_session = requests.Session() + test_gitlab = gitlab.Gitlab.from_config("one", [config_path]) + + assert test_gitlab.session != custom_session + + +def test_custom_session(default_config): + """Test custom session""" + + config_path = default_config + custom_session = requests.Session() + test_gitlab = gitlab.Gitlab.from_config( + "one", [config_path], session=custom_session + ) + + assert test_gitlab.session == custom_session |
