22
22
import time
23
23
import warnings
24
24
25
- import requests
25
+ import httpx
26
26
27
27
import gitlab .config
28
28
from gitlab .const import * # noqa
29
- from gitlab .exceptions import * # noqa
29
+ from gitlab .exceptions import (on_http_error , GitlabVerifyError ,
30
+ GitlabMarkdownError , GitlabLicenseError , RedirectError ,
31
+ GitlabHttpError , GitlabAuthenticationError ,
32
+ GitlabParsingError , GitlabSearchError )
30
33
from gitlab import utils # noqa
31
34
32
35
__title__ = "python-gitlab"
@@ -82,7 +85,7 @@ def __init__(
82
85
http_password = None ,
83
86
timeout = None ,
84
87
api_version = "4" ,
85
- session = None ,
88
+ client = None ,
86
89
per_page = None ,
87
90
):
88
91
@@ -105,8 +108,8 @@ def __init__(
105
108
self .job_token = job_token
106
109
self ._set_auth_info ()
107
110
108
- #: Create a session object for requests
109
- self .session = session or requests . Session ()
111
+ #: Create a client object for requests
112
+ self .client = client or httpx . AsyncClient ()
110
113
111
114
self .per_page = per_page
112
115
@@ -143,8 +146,8 @@ def __init__(
143
146
def __enter__ (self ):
144
147
return self
145
148
146
- def __exit__ (self , * args ):
147
- self .session . close ()
149
+ async def __exit__ (self , * args ):
150
+ await self .client . aclose ()
148
151
149
152
def __getstate__ (self ):
150
153
state = self .__dict__ .copy ()
@@ -211,7 +214,7 @@ def auth(self):
211
214
"""
212
215
self .user = self ._objects .CurrentUserManager (self ).get ()
213
216
214
- def version (self ):
217
+ async def version (self ):
215
218
"""Returns the version and revision of the gitlab server.
216
219
217
220
Note that self.version and self.revision will be set on the gitlab
@@ -224,7 +227,7 @@ def version(self):
224
227
"""
225
228
if self ._server_version is None :
226
229
try :
227
- data = self .http_get ("/version" )
230
+ data = await self .http_get ("/version" )
228
231
self ._server_version = data ["version" ]
229
232
self ._server_revision = data ["revision" ]
230
233
except Exception :
@@ -233,7 +236,7 @@ def version(self):
233
236
return self ._server_version , self ._server_revision
234
237
235
238
@on_http_error (GitlabVerifyError )
236
- def lint (self , content , ** kwargs ):
239
+ async def lint (self , content , ** kwargs ):
237
240
"""Validate a gitlab CI configuration.
238
241
239
242
Args:
@@ -249,11 +252,11 @@ def lint(self, content, **kwargs):
249
252
otherwise
250
253
"""
251
254
post_data = {"content" : content }
252
- data = self .http_post ("/ci/lint" , post_data = post_data , ** kwargs )
255
+ data = await self .http_post ("/ci/lint" , post_data = post_data , ** kwargs )
253
256
return (data ["status" ] == "valid" , data ["errors" ])
254
257
255
258
@on_http_error (GitlabMarkdownError )
256
- def markdown (self , text , gfm = False , project = None , ** kwargs ):
259
+ async def markdown (self , text , gfm = False , project = None , ** kwargs ):
257
260
"""Render an arbitrary Markdown document.
258
261
259
262
Args:
@@ -274,11 +277,11 @@ def markdown(self, text, gfm=False, project=None, **kwargs):
274
277
post_data = {"text" : text , "gfm" : gfm }
275
278
if project is not None :
276
279
post_data ["project" ] = project
277
- data = self .http_post ("/markdown" , post_data = post_data , ** kwargs )
280
+ data = await self .http_post ("/markdown" , post_data = post_data , ** kwargs )
278
281
return data ["html" ]
279
282
280
283
@on_http_error (GitlabLicenseError )
281
- def get_license (self , ** kwargs ):
284
+ async def get_license (self , ** kwargs ):
282
285
"""Retrieve information about the current license.
283
286
284
287
Args:
@@ -291,10 +294,10 @@ def get_license(self, **kwargs):
291
294
Returns:
292
295
dict: The current license information
293
296
"""
294
- return self .http_get ("/license" , ** kwargs )
297
+ return await self .http_get ("/license" , ** kwargs )
295
298
296
299
@on_http_error (GitlabLicenseError )
297
- def set_license (self , license , ** kwargs ):
300
+ async def set_license (self , license , ** kwargs ):
298
301
"""Add a new license.
299
302
300
303
Args:
@@ -309,7 +312,7 @@ def set_license(self, license, **kwargs):
309
312
dict: The new license information
310
313
"""
311
314
data = {"license" : license }
312
- return self .http_post ("/license" , post_data = data , ** kwargs )
315
+ return await self .http_post ("/license" , post_data = data , ** kwargs )
313
316
314
317
def _construct_url (self , id_ , obj , parameters , action = None ):
315
318
if "next_url" in parameters :
@@ -369,7 +372,7 @@ def _set_auth_info(self):
369
372
self .headers ["JOB-TOKEN" ] = self .job_token
370
373
371
374
if self .http_username :
372
- self ._http_auth = requests .auth .HTTPBasicAuth (
375
+ self ._http_auth = httpx .auth .HTTPBasicAuth (
373
376
self .http_username , self .http_password
374
377
)
375
378
@@ -436,7 +439,7 @@ def _check_redirects(self, result):
436
439
if location and location .startswith ("https://" ):
437
440
raise RedirectError (REDIRECT_MSG )
438
441
439
- def http_request (
442
+ async def http_request (
440
443
self ,
441
444
verb ,
442
445
path ,
@@ -508,12 +511,12 @@ def http_request(
508
511
# The Requests behavior is right but it seems that web servers don't
509
512
# always agree with this decision (this is the case with a default
510
513
# gitlab installation)
511
- req = requests .Request (
514
+ req = httpx .Request (
512
515
verb , url , json = json , data = data , params = params , files = files , ** opts
513
516
)
514
- prepped = self .session .prepare_request (req )
517
+ prepped = self .client .prepare_request (req )
515
518
prepped .url = utils .sanitized_url (prepped .url )
516
- settings = self .session .merge_environment_settings (
519
+ settings = self .client .merge_environment_settings (
517
520
prepped .url , {}, streamed , verify , None
518
521
)
519
522
@@ -527,7 +530,7 @@ def http_request(
527
530
cur_retries = 0
528
531
529
532
while True :
530
- result = self .session .send (prepped , timeout = timeout , ** settings )
533
+ result = await self .client .send (prepped , timeout = timeout , ** settings )
531
534
532
535
self ._check_redirects (result )
533
536
@@ -567,7 +570,7 @@ def http_request(
567
570
response_body = result .content ,
568
571
)
569
572
570
- def http_get (self , path , query_data = None , streamed = False , raw = False , ** kwargs ):
573
+ async def http_get (self , path , query_data = None , streamed = False , raw = False , ** kwargs ):
571
574
"""Make a GET request to the Gitlab server.
572
575
573
576
Args:
@@ -588,7 +591,7 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
588
591
GitlabParsingError: If the json data could not be parsed
589
592
"""
590
593
query_data = query_data or {}
591
- result = self .http_request (
594
+ result = await self .http_request (
592
595
"get" , path , query_data = query_data , streamed = streamed , ** kwargs
593
596
)
594
597
@@ -606,7 +609,7 @@ def http_get(self, path, query_data=None, streamed=False, raw=False, **kwargs):
606
609
else :
607
610
return result
608
611
609
- def http_list (self , path , query_data = None , as_list = None , ** kwargs ):
612
+ async def http_list (self , path , query_data = None , as_list = None , ** kwargs ):
610
613
"""Make a GET request to the Gitlab server for list-oriented queries.
611
614
612
615
Args:
@@ -645,7 +648,8 @@ def http_list(self, path, query_data=None, as_list=None, **kwargs):
645
648
# No pagination, generator requested
646
649
return GitlabList (self , url , query_data , ** kwargs )
647
650
648
- def http_post (self , path , query_data = None , post_data = None , files = None , ** kwargs ):
651
+ async def http_post (self , path , query_data = None ,
652
+ post_data = None , files = None , ** kwargs ):
649
653
"""Make a POST request to the Gitlab server.
650
654
651
655
Args:
@@ -668,7 +672,7 @@ def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs)
668
672
query_data = query_data or {}
669
673
post_data = post_data or {}
670
674
671
- result = self .http_request (
675
+ result = await self .http_request (
672
676
"post" ,
673
677
path ,
674
678
query_data = query_data ,
@@ -683,7 +687,8 @@ def http_post(self, path, query_data=None, post_data=None, files=None, **kwargs)
683
687
raise GitlabParsingError (error_message = "Failed to parse the server message" )
684
688
return result
685
689
686
- def http_put (self , path , query_data = None , post_data = None , files = None , ** kwargs ):
690
+ async def http_put (self , path , query_data = None ,
691
+ post_data = None , files = None , ** kwargs ):
687
692
"""Make a PUT request to the Gitlab server.
688
693
689
694
Args:
@@ -705,7 +710,7 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
705
710
query_data = query_data or {}
706
711
post_data = post_data or {}
707
712
708
- result = self .http_request (
713
+ result = await self .http_request (
709
714
"put" ,
710
715
path ,
711
716
query_data = query_data ,
@@ -718,7 +723,7 @@ def http_put(self, path, query_data=None, post_data=None, files=None, **kwargs):
718
723
except Exception :
719
724
raise GitlabParsingError (error_message = "Failed to parse the server message" )
720
725
721
- def http_delete (self , path , ** kwargs ):
726
+ async def http_delete (self , path , ** kwargs ):
722
727
"""Make a PUT request to the Gitlab server.
723
728
724
729
Args:
@@ -732,10 +737,10 @@ def http_delete(self, path, **kwargs):
732
737
Raises:
733
738
GitlabHttpError: When the return code is not 2xx
734
739
"""
735
- return self .http_request ("delete" , path , ** kwargs )
740
+ return await self .http_request ("delete" , path , ** kwargs )
736
741
737
742
@on_http_error (GitlabSearchError )
738
- def search (self , scope , search , ** kwargs ):
743
+ async def search (self , scope , search , ** kwargs ):
739
744
"""Search GitLab resources matching the provided string.'
740
745
741
746
Args:
@@ -761,14 +766,17 @@ class GitlabList(object):
761
766
the API again when needed.
762
767
"""
763
768
764
- def __init__ (self , gl , url , query_data , get_next = True , ** kwargs ):
769
+ async def __init__ (self , gl , url , query_data , get_next = True , ** kwargs ):
765
770
self ._gl = gl
766
- self ._query (url , query_data , ** kwargs )
771
+ await self ._query (url , query_data , ** kwargs )
767
772
self ._get_next = get_next
768
773
769
- def _query (self , url , query_data = None , ** kwargs ):
774
+ async def _query (self , url , query_data = None , ** kwargs ):
770
775
query_data = query_data or {}
771
- result = self ._gl .http_request ("get" , url , query_data = query_data , ** kwargs )
776
+ result = await self ._gl .http_request ("get" ,
777
+ url ,
778
+ query_data = query_data ,
779
+ ** kwargs )
772
780
try :
773
781
self ._next_url = result .links ["next" ]["url" ]
774
782
except KeyError :
@@ -829,10 +837,10 @@ def __iter__(self):
829
837
def __len__ (self ):
830
838
return int (self ._total )
831
839
832
- def __next__ (self ):
840
+ async def __next__ (self ):
833
841
return self .next ()
834
842
835
- def next (self ):
843
+ async def next (self ):
836
844
try :
837
845
item = self ._data [self ._current ]
838
846
self ._current += 1
@@ -841,7 +849,7 @@ def next(self):
841
849
pass
842
850
843
851
if self ._next_url and self ._get_next is True :
844
- self ._query (self ._next_url )
852
+ await self ._query (self ._next_url )
845
853
return self .next ()
846
854
847
855
raise StopIteration
0 commit comments