Skip to content

Commit 62a8f03

Browse files
author
Liora Milbaum
committed
feat: Generation of Gitlab class with custom requests session
1 parent 0ecf3bb commit 62a8f03

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

docs/api-usage.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ You can also use configuration files to create ``gitlab.Gitlab`` objects:
6262
6363
gl = gitlab.Gitlab.from_config('somewhere', ['/tmp/gl.cfg'])
6464
65+
With custom session
66+
67+
.. code-block:: python
68+
69+
gl = gitlab.Gitlab.from_config('somewhere', ['/tmp/gl.cfg'], session=custom_session)
70+
71+
6572
See the :ref:`cli_configuration` section for more information about
6673
configuration files.
6774

gitlab/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,20 @@ def api_version(self) -> str:
232232

233233
@classmethod
234234
def from_config(
235-
cls, gitlab_id: Optional[str] = None, config_files: Optional[List[str]] = None
235+
cls,
236+
gitlab_id: Optional[str] = None,
237+
config_files: Optional[List[str]] = None,
238+
**kwargs: Any,
236239
) -> "Gitlab":
237240
"""Create a Gitlab connection from configuration files.
238241
239242
Args:
240243
gitlab_id: ID of the configuration section.
241244
config_files list[str]: List of paths to configuration files.
242245
246+
kwargs:
247+
session requests.Session: Custom requests Session
248+
243249
Returns:
244250
A Gitlab connection.
245251
@@ -264,6 +270,7 @@ def from_config(
264270
order_by=config.order_by,
265271
user_agent=config.user_agent,
266272
retry_transient_errors=config.retry_transient_errors,
273+
**kwargs,
267274
)
268275

269276
@classmethod

tests/functional/api/test_gitlab.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import requests
23

34
import gitlab
45

@@ -19,10 +20,21 @@ def test_auth_from_config(gl, temp_dir):
1920
test_gitlab = gitlab.Gitlab.from_config(
2021
config_files=[temp_dir / "python-gitlab.cfg"]
2122
)
23+
2224
test_gitlab.auth()
2325
assert isinstance(test_gitlab.user, gitlab.v4.objects.CurrentUser)
2426

2527

28+
def test_custom_session(gl, temp_dir):
29+
"""Test custom session"""
30+
31+
custom_session = requests.Session()
32+
test_gitlab = gitlab.Gitlab.from_config(
33+
config_files=[temp_dir / "python-gitlab.cfg"], session=custom_session
34+
)
35+
assert test_gitlab.session == custom_session
36+
37+
2638
def test_broadcast_messages(gl, get_all_kwargs):
2739
msg = gl.broadcastmessages.create({"message": "this is the message"})
2840
msg.color = "#444444"

0 commit comments

Comments
 (0)