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