1
1
"""Wrapper for the GitLab API."""
2
+ from __future__ import annotations
2
3
3
4
import os
4
5
import re
6
+ import sys
5
7
import time
6
8
from typing import Any , cast , Dict , List , Optional , Tuple , TYPE_CHECKING , Union
7
9
from urllib import parse
8
10
11
+ import httpx
9
12
import requests
10
13
import requests .utils
11
14
from requests_toolbelt .multipart .encoder import MultipartEncoder # type: ignore
16
19
import gitlab .exceptions
17
20
from gitlab import utils
18
21
22
+ from .clients ._httpxclient import _HttpxClient
23
+ from .clients ._requestsclient import _RequestsClient
24
+
25
+ if sys .version_info >= (3 , 8 ):
26
+ from typing import Literal
27
+ else :
28
+ from typing_extensions import Literal
29
+
19
30
REDIRECT_MSG = (
20
31
"python-gitlab detected a {status_code} ({reason!r}) redirection. You must update "
21
32
"your GitLab URL to the correct URL to avoid issues. The redirection was from: "
@@ -56,7 +67,7 @@ class Gitlab:
56
67
"""
57
68
58
69
def __init__ (
59
- self ,
70
+ self : Gitlab ,
60
71
url : Optional [str ] = None ,
61
72
private_token : Optional [str ] = None ,
62
73
oauth_token : Optional [str ] = None ,
@@ -66,7 +77,9 @@ def __init__(
66
77
http_password : Optional [str ] = None ,
67
78
timeout : Optional [float ] = None ,
68
79
api_version : str = "4" ,
80
+ http : Optional [Literal ["requests" , "httpx" ]] = "requests" ,
69
81
session : Optional [requests .Session ] = None ,
82
+ client : Optional [httpx .Client ] = None ,
70
83
per_page : Optional [int ] = None ,
71
84
pagination : Optional [str ] = None ,
72
85
order_by : Optional [str ] = None ,
@@ -75,7 +88,21 @@ def __init__(
75
88
keep_base_url : bool = False ,
76
89
) -> None :
77
90
91
+ # We only support v4 API at this time
92
+ if api_version not in ("4" ,):
93
+ raise ModuleNotFoundError (f"gitlab.v{ api_version } .objects" )
94
+ # NOTE: We must delay import of gitlab.v4.objects until now or
95
+ # otherwise it will cause circular import errors
96
+ from gitlab .v4 import objects
97
+
78
98
self ._api_version = str (api_version )
99
+
100
+ if http == "requests" :
101
+ self ._requests_client = _RequestsClient (session = session )
102
+ self .session = self ._requests_client .get_session
103
+ elif http == "httpx" :
104
+ self ._httpx_client = _HttpxClient (client = client )
105
+
79
106
self ._server_version : Optional [str ] = None
80
107
self ._server_revision : Optional [str ] = None
81
108
self ._base_url = self ._get_base_url (url )
@@ -97,20 +124,10 @@ def __init__(
97
124
self .job_token = job_token
98
125
self ._set_auth_info ()
99
126
100
- #: Create a session object for requests
101
- self .session = session or requests .Session ()
102
-
103
127
self .per_page = per_page
104
128
self .pagination = pagination
105
129
self .order_by = order_by
106
130
107
- # We only support v4 API at this time
108
- if self ._api_version not in ("4" ,):
109
- raise ModuleNotFoundError (f"gitlab.v{ self ._api_version } .objects" )
110
- # NOTE: We must delay import of gitlab.v4.objects until now or
111
- # otherwise it will cause circular import errors
112
- from gitlab .v4 import objects
113
-
114
131
self ._objects = objects
115
132
self .user : Optional [objects .CurrentUser ] = None
116
133
@@ -189,11 +206,14 @@ def __init__(
189
206
self .topics = objects .TopicManager (self )
190
207
"""See :class:`~gitlab.v4.objects.TopicManager`"""
191
208
192
- def __enter__ (self ) -> " Gitlab" :
209
+ def __enter__ (self : Gitlab ) -> Gitlab :
193
210
return self
194
211
195
- def __exit__ (self , * args : Any ) -> None :
196
- self .session .close ()
212
+ def __exit__ (self : Gitlab , * args : Any ) -> None :
213
+ if self ._requests_client :
214
+ self ._requests_client .close ()
215
+ elif self ._httpx_client :
216
+ self ._httpx_client .close ()
197
217
198
218
def __getstate__ (self ) -> Dict [str , Any ]:
199
219
state = self .__dict__ .copy ()
0 commit comments