Skip to content

Commit 791de22

Browse files
committed
feat: replace requests with httpx
Make all functions async Use httpx instead of httpx Make decorators async to correctly wrap async methods
1 parent 19242c3 commit 791de22

File tree

8 files changed

+389
-384
lines changed

8 files changed

+389
-384
lines changed

gitlab/__init__.py

Lines changed: 84 additions & 90 deletions
Large diffs are not rendered by default.

gitlab/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
def register_custom_action(cls_names, mandatory=tuple(), optional=tuple()):
4040
def wrap(f):
4141
@functools.wraps(f)
42-
def wrapped_f(*args, **kwargs):
43-
return f(*args, **kwargs)
42+
async def wrapped_f(*args, **kwargs):
43+
return await f(*args, **kwargs)
4444

4545
# in_obj defines whether the method belongs to the obj or the manager
4646
in_obj = True

gitlab/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ def on_http_error(error):
262262

263263
def wrap(f):
264264
@functools.wraps(f)
265-
def wrapped_f(*args, **kwargs):
265+
async def wrapped_f(*args, **kwargs):
266266
try:
267-
return f(*args, **kwargs)
267+
return await f(*args, **kwargs)
268268
except GitlabHttpError as e:
269269
raise error(e.error_message, e.response_code, e.response_body)
270270

gitlab/mixins.py

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
import gitlab
19-
from gitlab import base
20-
from gitlab import cli
19+
from gitlab import base, cli
2120
from gitlab import exceptions as exc
2221
from gitlab import types as g_types
2322
from gitlab import utils
2423

2524

2625
class GetMixin(object):
2726
@exc.on_http_error(exc.GitlabGetError)
28-
def get(self, id, lazy=False, **kwargs):
27+
async def get(self, id, lazy=False, **kwargs):
2928
"""Retrieve a single object.
3029
3130
Args:
@@ -48,12 +47,12 @@ def get(self, id, lazy=False, **kwargs):
4847
if lazy is True:
4948
return self._obj_cls(self, {self._obj_cls._id_attr: id})
5049
server_data = self.gitlab.http_get(path, **kwargs)
51-
return self._obj_cls(self, server_data)
50+
return await self._obj_cls(self, server_data)
5251

5352

5453
class GetWithoutIdMixin(object):
5554
@exc.on_http_error(exc.GitlabGetError)
56-
def get(self, id=None, **kwargs):
55+
async def get(self, id=None, **kwargs):
5756
"""Retrieve a single object.
5857
5958
Args:
@@ -69,12 +68,12 @@ def get(self, id=None, **kwargs):
6968
server_data = self.gitlab.http_get(self.path, **kwargs)
7069
if server_data is None:
7170
return None
72-
return self._obj_cls(self, server_data)
71+
return await self._obj_cls(self, server_data)
7372

7473

7574
class RefreshMixin(object):
7675
@exc.on_http_error(exc.GitlabGetError)
77-
def refresh(self, **kwargs):
76+
async def refresh(self, **kwargs):
7877
"""Refresh a single object from server.
7978
8079
Args:
@@ -90,13 +89,13 @@ def refresh(self, **kwargs):
9089
path = "%s/%s" % (self.manager.path, self.id)
9190
else:
9291
path = self.manager.path
93-
server_data = self.manager.gitlab.http_get(path, **kwargs)
92+
server_data = await self.manager.gitlab.http_get(path, **kwargs)
9493
self._update_attrs(server_data)
9594

9695

9796
class ListMixin(object):
9897
@exc.on_http_error(exc.GitlabListError)
99-
def list(self, **kwargs):
98+
async def list(self, **kwargs):
10099
"""Retrieve a list of objects.
101100
102101
Args:
@@ -138,7 +137,7 @@ def list(self, **kwargs):
138137
# Allow to overwrite the path, handy for custom listings
139138
path = data.pop("path", self.path)
140139

141-
obj = self.gitlab.http_list(path, **data)
140+
obj = await self.gitlab.http_list(path, **data)
142141
if isinstance(obj, list):
143142
return [self._obj_cls(self, item) for item in obj]
144143
else:
@@ -170,7 +169,7 @@ def get_create_attrs(self):
170169
return getattr(self, "_create_attrs", (tuple(), tuple()))
171170

172171
@exc.on_http_error(exc.GitlabCreateError)
173-
def create(self, data, **kwargs):
172+
async def create(self, data, **kwargs):
174173
"""Create a new object.
175174
176175
Args:
@@ -208,7 +207,9 @@ def create(self, data, **kwargs):
208207

209208
# Handle specific URL for creation
210209
path = kwargs.pop("path", self.path)
211-
server_data = self.gitlab.http_post(path, post_data=data, files=files, **kwargs)
210+
server_data = await self.gitlab.http_post(
211+
path, post_data=data, files=files, **kwargs
212+
)
212213
return self._obj_cls(self, server_data)
213214

214215

@@ -247,7 +248,7 @@ def _get_update_method(self):
247248
return http_method
248249

249250
@exc.on_http_error(exc.GitlabUpdateError)
250-
def update(self, id=None, new_data=None, **kwargs):
251+
async def update(self, id=None, new_data=None, **kwargs):
251252
"""Update an object on the server.
252253
253254
Args:
@@ -290,12 +291,12 @@ def update(self, id=None, new_data=None, **kwargs):
290291
new_data[attr_name] = type_obj.get_for_api()
291292

292293
http_method = self._get_update_method()
293-
return http_method(path, post_data=new_data, files=files, **kwargs)
294+
return await http_method(path, post_data=new_data, files=files, **kwargs)
294295

295296

296297
class SetMixin(object):
297298
@exc.on_http_error(exc.GitlabSetError)
298-
def set(self, key, value, **kwargs):
299+
async def set(self, key, value, **kwargs):
299300
"""Create or update the object.
300301
301302
Args:
@@ -312,13 +313,13 @@ def set(self, key, value, **kwargs):
312313
"""
313314
path = "%s/%s" % (self.path, utils.clean_str_id(key))
314315
data = {"value": value}
315-
server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
316+
server_data = await self.gitlab.http_put(path, post_data=data, **kwargs)
316317
return self._obj_cls(self, server_data)
317318

318319

319320
class DeleteMixin(object):
320321
@exc.on_http_error(exc.GitlabDeleteError)
321-
def delete(self, id, **kwargs):
322+
async def delete(self, id, **kwargs):
322323
"""Delete an object on the server.
323324
324325
Args:
@@ -335,7 +336,7 @@ def delete(self, id, **kwargs):
335336
if not isinstance(id, int):
336337
id = utils.clean_str_id(id)
337338
path = "%s/%s" % (self.path, id)
338-
self.gitlab.http_delete(path, **kwargs)
339+
await self.gitlab.http_delete(path, **kwargs)
339340

340341

341342
class CRUDMixin(GetMixin, ListMixin, CreateMixin, UpdateMixin, DeleteMixin):
@@ -360,7 +361,7 @@ def _get_updated_data(self):
360361

361362
return updated_data
362363

363-
def save(self, **kwargs):
364+
async def save(self, **kwargs):
364365
"""Save the changes made to the object to the server.
365366
366367
The object is updated to match what the server returns.
@@ -379,15 +380,15 @@ def save(self, **kwargs):
379380

380381
# call the manager
381382
obj_id = self.get_id()
382-
server_data = self.manager.update(obj_id, updated_data, **kwargs)
383+
server_data = await self.manager.update(obj_id, updated_data, **kwargs)
383384
if server_data is not None:
384385
self._update_attrs(server_data)
385386

386387

387388
class ObjectDeleteMixin(object):
388389
"""Mixin for RESTObject's that can be deleted."""
389390

390-
def delete(self, **kwargs):
391+
async def delete(self, **kwargs):
391392
"""Delete the object from the server.
392393
393394
Args:
@@ -397,13 +398,13 @@ def delete(self, **kwargs):
397398
GitlabAuthenticationError: If authentication is not correct
398399
GitlabDeleteError: If the server cannot perform the request
399400
"""
400-
self.manager.delete(self.get_id())
401+
await self.manager.delete(self.get_id())
401402

402403

403404
class UserAgentDetailMixin(object):
404405
@cli.register_custom_action(("Snippet", "ProjectSnippet", "ProjectIssue"))
405406
@exc.on_http_error(exc.GitlabGetError)
406-
def user_agent_detail(self, **kwargs):
407+
async def user_agent_detail(self, **kwargs):
407408
"""Get the user agent detail.
408409
409410
Args:
@@ -414,15 +415,15 @@ def user_agent_detail(self, **kwargs):
414415
GitlabGetError: If the server cannot perform the request
415416
"""
416417
path = "%s/%s/user_agent_detail" % (self.manager.path, self.get_id())
417-
return self.manager.gitlab.http_get(path, **kwargs)
418+
return await self.manager.gitlab.http_get(path, **kwargs)
418419

419420

420421
class AccessRequestMixin(object):
421422
@cli.register_custom_action(
422423
("ProjectAccessRequest", "GroupAccessRequest"), tuple(), ("access_level",)
423424
)
424425
@exc.on_http_error(exc.GitlabUpdateError)
425-
def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
426+
async def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
426427
"""Approve an access request.
427428
428429
Args:
@@ -436,7 +437,7 @@ def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
436437

437438
path = "%s/%s/approve" % (self.manager.path, self.id)
438439
data = {"access_level": access_level}
439-
server_data = self.manager.gitlab.http_put(path, post_data=data, **kwargs)
440+
server_data = await self.manager.gitlab.http_put(path, post_data=data, **kwargs)
440441
self._update_attrs(server_data)
441442

442443

@@ -445,7 +446,7 @@ class SubscribableMixin(object):
445446
("ProjectIssue", "ProjectMergeRequest", "ProjectLabel", "GroupLabel")
446447
)
447448
@exc.on_http_error(exc.GitlabSubscribeError)
448-
def subscribe(self, **kwargs):
449+
async def subscribe(self, **kwargs):
449450
"""Subscribe to the object notifications.
450451
451452
Args:
@@ -456,14 +457,14 @@ def subscribe(self, **kwargs):
456457
GitlabSubscribeError: If the subscription cannot be done
457458
"""
458459
path = "%s/%s/subscribe" % (self.manager.path, self.get_id())
459-
server_data = self.manager.gitlab.http_post(path, **kwargs)
460+
server_data = await self.manager.gitlab.http_post(path, **kwargs)
460461
self._update_attrs(server_data)
461462

462463
@cli.register_custom_action(
463464
("ProjectIssue", "ProjectMergeRequest", "ProjectLabel", "GroupLabel")
464465
)
465466
@exc.on_http_error(exc.GitlabUnsubscribeError)
466-
def unsubscribe(self, **kwargs):
467+
async def unsubscribe(self, **kwargs):
467468
"""Unsubscribe from the object notifications.
468469
469470
Args:
@@ -474,14 +475,14 @@ def unsubscribe(self, **kwargs):
474475
GitlabUnsubscribeError: If the unsubscription cannot be done
475476
"""
476477
path = "%s/%s/unsubscribe" % (self.manager.path, self.get_id())
477-
server_data = self.manager.gitlab.http_post(path, **kwargs)
478+
server_data = await self.manager.gitlab.http_post(path, **kwargs)
478479
self._update_attrs(server_data)
479480

480481

481482
class TodoMixin(object):
482483
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"))
483484
@exc.on_http_error(exc.GitlabTodoError)
484-
def todo(self, **kwargs):
485+
async def todo(self, **kwargs):
485486
"""Create a todo associated to the object.
486487
487488
Args:
@@ -492,13 +493,13 @@ def todo(self, **kwargs):
492493
GitlabTodoError: If the todo cannot be set
493494
"""
494495
path = "%s/%s/todo" % (self.manager.path, self.get_id())
495-
self.manager.gitlab.http_post(path, **kwargs)
496+
await self.manager.gitlab.http_post(path, **kwargs)
496497

497498

498499
class TimeTrackingMixin(object):
499500
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"))
500501
@exc.on_http_error(exc.GitlabTimeTrackingError)
501-
def time_stats(self, **kwargs):
502+
async def time_stats(self, **kwargs):
502503
"""Get time stats for the object.
503504
504505
Args:
@@ -514,11 +515,11 @@ def time_stats(self, **kwargs):
514515
return self.attributes["time_stats"]
515516

516517
path = "%s/%s/time_stats" % (self.manager.path, self.get_id())
517-
return self.manager.gitlab.http_get(path, **kwargs)
518+
return await self.manager.gitlab.http_get(path, **kwargs)
518519

519520
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"), ("duration",))
520521
@exc.on_http_error(exc.GitlabTimeTrackingError)
521-
def time_estimate(self, duration, **kwargs):
522+
async def time_estimate(self, duration, **kwargs):
522523
"""Set an estimated time of work for the object.
523524
524525
Args:
@@ -531,11 +532,11 @@ def time_estimate(self, duration, **kwargs):
531532
"""
532533
path = "%s/%s/time_estimate" % (self.manager.path, self.get_id())
533534
data = {"duration": duration}
534-
return self.manager.gitlab.http_post(path, post_data=data, **kwargs)
535+
return await self.manager.gitlab.http_post(path, post_data=data, **kwargs)
535536

536537
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"))
537538
@exc.on_http_error(exc.GitlabTimeTrackingError)
538-
def reset_time_estimate(self, **kwargs):
539+
async def reset_time_estimate(self, **kwargs):
539540
"""Resets estimated time for the object to 0 seconds.
540541
541542
Args:
@@ -546,11 +547,11 @@ def reset_time_estimate(self, **kwargs):
546547
GitlabTimeTrackingError: If the time tracking update cannot be done
547548
"""
548549
path = "%s/%s/reset_time_estimate" % (self.manager.path, self.get_id())
549-
return self.manager.gitlab.http_post(path, **kwargs)
550+
return await self.manager.gitlab.http_post(path, **kwargs)
550551

551552
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"), ("duration",))
552553
@exc.on_http_error(exc.GitlabTimeTrackingError)
553-
def add_spent_time(self, duration, **kwargs):
554+
async def add_spent_time(self, duration, **kwargs):
554555
"""Add time spent working on the object.
555556
556557
Args:
@@ -563,11 +564,11 @@ def add_spent_time(self, duration, **kwargs):
563564
"""
564565
path = "%s/%s/add_spent_time" % (self.manager.path, self.get_id())
565566
data = {"duration": duration}
566-
return self.manager.gitlab.http_post(path, post_data=data, **kwargs)
567+
return await self.manager.gitlab.http_post(path, post_data=data, **kwargs)
567568

568569
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"))
569570
@exc.on_http_error(exc.GitlabTimeTrackingError)
570-
def reset_spent_time(self, **kwargs):
571+
async def reset_spent_time(self, **kwargs):
571572
"""Resets the time spent working on the object.
572573
573574
Args:
@@ -578,13 +579,13 @@ def reset_spent_time(self, **kwargs):
578579
GitlabTimeTrackingError: If the time tracking update cannot be done
579580
"""
580581
path = "%s/%s/reset_spent_time" % (self.manager.path, self.get_id())
581-
return self.manager.gitlab.http_post(path, **kwargs)
582+
return await self.manager.gitlab.http_post(path, **kwargs)
582583

583584

584585
class ParticipantsMixin(object):
585586
@cli.register_custom_action(("ProjectMergeRequest", "ProjectIssue"))
586587
@exc.on_http_error(exc.GitlabListError)
587-
def participants(self, **kwargs):
588+
async def participants(self, **kwargs):
588589
"""List the participants.
589590
590591
Args:
@@ -604,15 +605,15 @@ def participants(self, **kwargs):
604605
"""
605606

606607
path = "%s/%s/participants" % (self.manager.path, self.get_id())
607-
return self.manager.gitlab.http_get(path, **kwargs)
608+
return await self.manager.gitlab.http_get(path, **kwargs)
608609

609610

610611
class BadgeRenderMixin(object):
611612
@cli.register_custom_action(
612613
("GroupBadgeManager", "ProjectBadgeManager"), ("link_url", "image_url")
613614
)
614615
@exc.on_http_error(exc.GitlabRenderError)
615-
def render(self, link_url, image_url, **kwargs):
616+
async def render(self, link_url, image_url, **kwargs):
616617
"""Preview link_url and image_url after interpolation.
617618
618619
Args:
@@ -629,4 +630,4 @@ def render(self, link_url, image_url, **kwargs):
629630
"""
630631
path = "%s/render" % self.path
631632
data = {"link_url": link_url, "image_url": image_url}
632-
return self.gitlab.http_get(path, data, **kwargs)
633+
return await self.gitlab.http_get(path, data, **kwargs)

0 commit comments

Comments
 (0)