Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86a8251

Browse files
author
Gauvain Pocentek
authored
Merge pull request#488 from siemens/feat/rate-limit
feat: obey the rate limit
2 parents25ed8e7 +e216f06 commit86a8251

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

‎docs/api-usage.rst‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,26 @@ The following sample illustrates how to use a client-side certificate:
326326
327327
Reference:
328328
http://docs.python-requests.org/en/master/user/advanced/#client-side-certificates
329+
330+
Rate limits
331+
-----------
332+
333+
python-gitlab will obey the rate limit of the GitLab server by default.
334+
On receiving a 429 response (Too Many Requests), python-gitlab will sleep for the amount of time
335+
in the Retry-After header, that GitLab sends back.
336+
337+
If you don't want to wait, you can disable the rate-limiting feature, by supplying the
338+
``obey_rate_limit`` argument.
339+
340+
..code-block::python
341+
342+
import gitlab
343+
import requests
344+
345+
gl= gitlab.gitlab(url, token,api_version=4)
346+
gl.projects.list(all=True,obey_rate_limit=False)
347+
348+
349+
..warning::
350+
351+
You will get an Exception, if you then go over the rate limit of your GitLab instance.

‎gitlab/__init__.py‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
importitertools
2424
importjson
2525
importre
26+
importtime
2627
importwarnings
2728

2829
importrequests
@@ -698,24 +699,35 @@ def copy_dict(dest, src):
698699
prepped.url=sanitized_url(prepped.url)
699700
settings=self.session.merge_environment_settings(
700701
prepped.url, {},streamed,verify,None)
701-
result=self.session.send(prepped,timeout=timeout,**settings)
702702

703-
if200<=result.status_code<300:
704-
returnresult
703+
# obey the rate limit by default
704+
obey_rate_limit=kwargs.get("obey_rate_limit",True)
705705

706-
try:
707-
error_message=result.json()['message']
708-
except (KeyError,ValueError,TypeError):
709-
error_message=result.content
710-
711-
ifresult.status_code==401:
712-
raiseGitlabAuthenticationError(response_code=result.status_code,
713-
error_message=error_message,
714-
response_body=result.content)
715-
716-
raiseGitlabHttpError(response_code=result.status_code,
717-
error_message=error_message,
718-
response_body=result.content)
706+
whileTrue:
707+
result=self.session.send(prepped,timeout=timeout,**settings)
708+
709+
if200<=result.status_code<300:
710+
returnresult
711+
712+
if429==result.status_codeandobey_rate_limit:
713+
wait_time=int(result.headers["Retry-After"])
714+
time.sleep(wait_time)
715+
continue
716+
717+
try:
718+
error_message=result.json()['message']
719+
except (KeyError,ValueError,TypeError):
720+
error_message=result.content
721+
722+
ifresult.status_code==401:
723+
raiseGitlabAuthenticationError(
724+
response_code=result.status_code,
725+
error_message=error_message,
726+
response_body=result.content)
727+
728+
raiseGitlabHttpError(response_code=result.status_code,
729+
error_message=error_message,
730+
response_body=result.content)
719731

720732
defhttp_get(self,path,query_data={},streamed=False,**kwargs):
721733
"""Make a GET request to the Gitlab server.

‎tools/python_test_v4.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,28 @@
646646

647647
# events
648648
gl.events.list()
649+
650+
# rate limit
651+
settings=gl.settings.get()
652+
settings.throttle_authenticated_api_enabled=True
653+
settings.throttle_authenticated_api_requests_per_period=1
654+
settings.throttle_authenticated_api_period_in_seconds=3
655+
settings.save()
656+
projects=list()
657+
foriinrange(0,20):
658+
projects.append(gl.projects.create(
659+
{'name':str(i)+"ok"}))
660+
661+
error_message=None
662+
foriinrange(20,40):
663+
try:
664+
projects.append(
665+
gl.projects.create(
666+
{'name':str(i)+'shouldfail'},obey_rate_limit=False))
667+
exceptgitlab.GitlabCreateErrorase:
668+
error_message=e.error_message
669+
break
670+
assert'Retry later'inerror_message
671+
[current_project.delete()forcurrent_projectinprojects]
672+
settings.throttle_authenticated_api_enabled=False
673+
settings.save()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp