|
8 | 8 |
|
9 | 9 | importrequests
|
10 | 10 | importrequests.utils
|
11 |
| -fromrequests_toolbelt.multipart.encoderimportMultipartEncoder# type: ignore |
12 | 11 |
|
13 | 12 | importgitlab
|
14 | 13 | importgitlab.config
|
@@ -637,38 +636,6 @@ def _check_redirects(result: requests.Response) -> None:
|
637 | 636 | )
|
638 | 637 | )
|
639 | 638 |
|
640 |
| -@staticmethod |
641 |
| -def_prepare_send_data( |
642 |
| -files:Optional[Dict[str,Any]]=None, |
643 |
| -post_data:Optional[Union[Dict[str,Any],bytes]]=None, |
644 |
| -raw:bool=False, |
645 |
| - )->Tuple[ |
646 |
| -Optional[Union[Dict[str,Any],bytes]], |
647 |
| -Optional[Union[Dict[str,Any],MultipartEncoder]], |
648 |
| -str, |
649 |
| - ]: |
650 |
| -iffiles: |
651 |
| -ifpost_dataisNone: |
652 |
| -post_data= {} |
653 |
| -else: |
654 |
| -# booleans does not exists for data (neither for MultipartEncoder): |
655 |
| -# cast to string int to avoid: 'bool' object has no attribute 'encode' |
656 |
| -ifTYPE_CHECKING: |
657 |
| -assertisinstance(post_data,dict) |
658 |
| -fork,vinpost_data.items(): |
659 |
| -ifisinstance(v,bool): |
660 |
| -post_data[k]=str(int(v)) |
661 |
| -post_data["file"]=files.get("file") |
662 |
| -post_data["avatar"]=files.get("avatar") |
663 |
| - |
664 |
| -data=MultipartEncoder(post_data) |
665 |
| -return (None,data,data.content_type) |
666 |
| - |
667 |
| -ifrawandpost_data: |
668 |
| -return (None,post_data,"application/octet-stream") |
669 |
| - |
670 |
| -return (post_data,None,"application/json") |
671 |
| - |
672 | 639 | defhttp_request(
|
673 | 640 | self,
|
674 | 641 | verb:str,
|
@@ -746,7 +713,9 @@ def http_request(
|
746 | 713 | retry_transient_errors=self.retry_transient_errors
|
747 | 714 |
|
748 | 715 | # We need to deal with json vs. data when uploading files
|
749 |
| -json,data,content_type=self._prepare_send_data(files,post_data,raw) |
| 716 | +json,data,content_type=self.http_backend.prepare_send_data( |
| 717 | +files,post_data,raw |
| 718 | + ) |
750 | 719 | opts["headers"]["Content-type"]=content_type
|
751 | 720 |
|
752 | 721 | cur_retries=0
|
@@ -779,46 +748,42 @@ def http_request(
|
779 | 748 | if200<=result.status_code<300:
|
780 | 749 | returnresult.response
|
781 | 750 |
|
782 |
| -if (429==result.response.status_codeandobey_rate_limit)or ( |
783 |
| -result.response.status_code |
784 |
| -ingitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES |
| 751 | +if (429==result.status_codeandobey_rate_limit)or ( |
| 752 | +result.status_codeingitlab.const.RETRYABLE_TRANSIENT_ERROR_CODES |
785 | 753 | andretry_transient_errors
|
786 | 754 | ):
|
787 | 755 | # Response headers documentation:
|
788 | 756 | # https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
|
789 | 757 | ifmax_retries==-1orcur_retries<max_retries:
|
790 | 758 | wait_time=2**cur_retries*0.1
|
791 |
| -if"Retry-After"inresult.response.headers: |
792 |
| -wait_time=int(result.response.headers["Retry-After"]) |
793 |
| -elif"RateLimit-Reset"inresult.response.headers: |
794 |
| -wait_time= ( |
795 |
| -int(result.response.headers["RateLimit-Reset"]) |
796 |
| --time.time() |
797 |
| - ) |
| 759 | +if"Retry-After"inresult.headers: |
| 760 | +wait_time=int(result.headers["Retry-After"]) |
| 761 | +elif"RateLimit-Reset"inresult.headers: |
| 762 | +wait_time=int(result.headers["RateLimit-Reset"])-time.time() |
798 | 763 | cur_retries+=1
|
799 | 764 | time.sleep(wait_time)
|
800 | 765 | continue
|
801 | 766 |
|
802 |
| -error_message=result.response.content |
| 767 | +error_message=result.content |
803 | 768 | try:
|
804 |
| -error_json=result.response.json() |
| 769 | +error_json=result.json() |
805 | 770 | forkin ("message","error"):
|
806 | 771 | ifkinerror_json:
|
807 | 772 | error_message=error_json[k]
|
808 | 773 | except (KeyError,ValueError,TypeError):
|
809 | 774 | pass
|
810 | 775 |
|
811 |
| -ifresult.response.status_code==401: |
| 776 | +ifresult.status_code==401: |
812 | 777 | raisegitlab.exceptions.GitlabAuthenticationError(
|
813 |
| -response_code=result.response.status_code, |
| 778 | +response_code=result.status_code, |
814 | 779 | error_message=error_message,
|
815 |
| -response_body=result.response.content, |
| 780 | +response_body=result.content, |
816 | 781 | )
|
817 | 782 |
|
818 | 783 | raisegitlab.exceptions.GitlabHttpError(
|
819 |
| -response_code=result.response.status_code, |
| 784 | +response_code=result.status_code, |
820 | 785 | error_message=error_message,
|
821 |
| -response_body=result.response.content, |
| 786 | +response_body=result.content, |
822 | 787 | )
|
823 | 788 |
|
824 | 789 | defhttp_get(
|
|