Skip to content

Commit

Permalink
feat: allow passing kwargs to Gitlab class when instantiating with `f…
Browse files Browse the repository at this point in the history
…rom_config` (#2392)
  • Loading branch information
lmilbaum committed Dec 4, 2022
1 parent 50a0301 commit e88d34e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/api-usage-advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 8 additions & 1 deletion gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,20 @@ def api_version(self) -> str:

@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.
Args:
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.
Expand All @@ -264,6 +270,7 @@ def from_config(
order_by=config.order_by,
user_agent=config.user_agent,
retry_transient_errors=config.retry_transient_errors,
**kwargs,
)

@classmethod
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/api/test_gitlab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import requests

import gitlab

Expand All @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from http.client import HTTPConnection

import pytest
import requests
import responses

import gitlab
Expand Down Expand Up @@ -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

0 comments on commit e88d34e

Please sign in to comment.