|
23 | 23 | import itertools |
24 | 24 | import json |
25 | 25 | import re |
| 26 | +import time |
26 | 27 | import warnings |
27 | 28 |
|
28 | 29 | import requests |
@@ -698,24 +699,35 @@ def copy_dict(dest, src): |
698 | 699 | prepped.url = sanitized_url(prepped.url) |
699 | 700 | settings = self.session.merge_environment_settings( |
700 | 701 | prepped.url, {}, streamed, verify, None) |
701 | | - result = self.session.send(prepped, timeout=timeout, **settings) |
702 | 702 |
|
703 | | - if 200 <= result.status_code < 300: |
704 | | - return result |
| 703 | + # obey the rate limit by default |
| 704 | + obey_rate_limit = kwargs.get("obey_rate_limit", True) |
705 | 705 |
|
706 | | - try: |
707 | | - error_message = result.json()['message'] |
708 | | - except (KeyError, ValueError, TypeError): |
709 | | - error_message = result.content |
710 | | - |
711 | | - if result.status_code == 401: |
712 | | - raise GitlabAuthenticationError(response_code=result.status_code, |
713 | | - error_message=error_message, |
714 | | - response_body=result.content) |
715 | | - |
716 | | - raise GitlabHttpError(response_code=result.status_code, |
717 | | - error_message=error_message, |
718 | | - response_body=result.content) |
| 706 | + while True: |
| 707 | + result = self.session.send(prepped, timeout=timeout, **settings) |
| 708 | + |
| 709 | + if 200 <= result.status_code < 300: |
| 710 | + return result |
| 711 | + |
| 712 | + if 429 == result.status_code and obey_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 | + if result.status_code == 401: |
| 723 | + raise GitlabAuthenticationError( |
| 724 | + response_code=result.status_code, |
| 725 | + error_message=error_message, |
| 726 | + response_body=result.content) |
| 727 | + |
| 728 | + raise GitlabHttpError(response_code=result.status_code, |
| 729 | + error_message=error_message, |
| 730 | + response_body=result.content) |
719 | 731 |
|
720 | 732 | def http_get(self, path, query_data={}, streamed=False, **kwargs): |
721 | 733 | """Make a GET request to the Gitlab server. |
|
0 commit comments