| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a1dbe86 commit baaba22
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,7 @@ | |||
| 18 | 18 | ||
| 19 | 19 | import os | |
| 20 | 20 | import time | |
| 21 | + import warnings | ||
| 21 | 22 | from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union | |
| 22 | 23 | ||
| 23 | 24 | import requests | |
@@ -549,7 +550,7 @@ def _build_url(self, path: str) -> str: | |||
| 549 | 550 | ||
| 550 | 551 | def _check_redirects(self, result: requests.Response) -> None: | |
| 551 | 552 | # Check the requests history to detect 301/302 redirections. | |
| 552 | - # If the initial verb is POST or PUT, the redirected request will use a | ||
| 553 | + # If the initial method is POST or PUT, the redirected request will use a | ||
| 553 | 554 | # GET request, leading to unwanted behaviour. | |
| 554 | 555 | # If we detect a redirection with a POST or a PUT request, we | |
| 555 | 556 | # raise an exception with a useful error message. | |
@@ -617,11 +618,45 @@ def http_request( | |||
| 617 | 618 | obey_rate_limit: bool = True, | |
| 618 | 619 | max_retries: int = 10, | |
| 619 | 620 | **kwargs: Any, | |
| 621 | + ) -> requests.Response: | ||
| 622 | + warnings.warn( | ||
| 623 | + "The Gitlab.http_request() method is deprecated and will be removed in a " | ||
| 624 | + "future version. This is a private method and should not be used.", | ||
| 625 | + DeprecationWarning, | ||
| 626 | + ) | ||
| 627 | + return self._http_request( | ||
| 628 | + method=verb, | ||
| 629 | + path=path, | ||
| 630 | + query_data=query_data, | ||
| 631 | + post_data=post_data, | ||
| 632 | + raw=raw, | ||
| 633 | + streamed=streamed, | ||
| 634 | + files=files, | ||
| 635 | + timeout=timeout, | ||
| 636 | + obey_rate_limit=obey_rate_limit, | ||
| 637 | + max_retries=max_retries, | ||
| 638 | + **kwargs, | ||
| 639 | + ) | ||
| 640 | + | ||
| 641 | + def _http_request( | ||
| 642 | + self, | ||
| 643 | + *, | ||
| 644 | + method: str, | ||
| 645 | + path: str, | ||
| 646 | + query_data: Optional[Dict[str, Any]] = None, | ||
| 647 | + post_data: Optional[Union[Dict[str, Any], bytes]] = None, | ||
| 648 | + raw: bool = False, | ||
| 649 | + streamed: bool = False, | ||
| 650 | + files: Optional[Dict[str, Any]] = None, | ||
| 651 | + timeout: Optional[float] = None, | ||
| 652 | + obey_rate_limit: bool = True, | ||
| 653 | + max_retries: int = 10, | ||
| 654 | + **kwargs: Any, | ||
| 620 | 655 | ) -> requests.Response: | |
| 621 | 656 | """Make an HTTP request to the Gitlab server. | |
| 622 | 657 | ||
| 623 | 658 | Args: | |
| 624 | - verb: The HTTP method to call ('get', 'post', 'put', 'delete') | ||
| 659 | + method: The HTTP method to call ('get', 'post', 'put', 'delete') | ||
| 625 | 660 | path: Path or full URL to query ('/projects' or | |
| 626 | 661 | 'http://whatever/v4/api/projecs') | |
| 627 | 662 | query_data: Data to send as query parameters | |
@@ -678,7 +713,7 @@ def http_request( | |||
| 678 | 713 | cur_retries = 0 | |
| 679 | 714 | while True: | |
| 680 | 715 | result = self.session.request( | |
| 681 | - method=verb, | ||
| 716 | + method=method, | ||
| 682 | 717 | url=url, | |
| 683 | 718 | json=json, | |
| 684 | 719 | data=data, | |
@@ -758,8 +793,8 @@ def http_get( | |||
| 758 | 793 | GitlabParsingError: If the json data could not be parsed | |
| 759 | 794 | """ | |
| 760 | 795 | query_data = query_data or {} | |
| 761 | - result = self.http_request( | ||
| 762 | - "get", path, query_data=query_data, streamed=streamed, **kwargs | ||
| 796 | + result = self._http_request( | ||
| 797 | + method="get", path=path, query_data=query_data, streamed=streamed, **kwargs | ||
| 763 | 798 | ) | |
| 764 | 799 | ||
| 765 | 800 | if ( | |
@@ -855,9 +890,9 @@ def http_post( | |||
| 855 | 890 | query_data = query_data or {} | |
| 856 | 891 | post_data = post_data or {} | |
| 857 | 892 | ||
| 858 | - result = self.http_request( | ||
| 859 | - "post", | ||
| 860 | - path, | ||
| 893 | + result = self._http_request( | ||
| 894 | + method="post", | ||
| 895 | + path=path, | ||
| 861 | 896 | query_data=query_data, | |
| 862 | 897 | post_data=post_data, | |
| 863 | 898 | files=files, | |
@@ -903,9 +938,9 @@ def http_put( | |||
| 903 | 938 | query_data = query_data or {} | |
| 904 | 939 | post_data = post_data or {} | |
| 905 | 940 | ||
| 906 | - result = self.http_request( | ||
| 907 | - "put", | ||
| 908 | - path, | ||
| 941 | + result = self._http_request( | ||
| 942 | + method="put", | ||
| 943 | + path=path, | ||
| 909 | 944 | query_data=query_data, | |
| 910 | 945 | post_data=post_data, | |
| 911 | 946 | files=files, | |
@@ -933,7 +968,7 @@ def http_delete(self, path: str, **kwargs: Any) -> requests.Response: | |||
| 933 | 968 | Raises: | |
| 934 | 969 | GitlabHttpError: When the return code is not 2xx | |
| 935 | 970 | """ | |
| 936 | - return self.http_request("delete", path, **kwargs) | ||
| 971 | + return self._http_request(method="delete", path=path, **kwargs) | ||
| 937 | 972 | ||
| 938 | 973 | @gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError) | |
| 939 | 974 | def search( | |
@@ -987,7 +1022,9 @@ def _query( | |||
| 987 | 1022 | self, url: str, query_data: Optional[Dict[str, Any]] = None, **kwargs: Any | |
| 988 | 1023 | ) -> None: | |
| 989 | 1024 | query_data = query_data or {} | |
| 990 | - result = self._gl.http_request("get", url, query_data=query_data, **kwargs) | ||
| 1025 | + result = self._gl._http_request( | ||
| 1026 | + method="get", path=url, query_data=query_data, **kwargs | ||
| 1027 | + ) | ||
| 991 | 1028 | try: | |
| 992 | 1029 | links = result.links | |
| 993 | 1030 | if links: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | + import warnings | ||
| 2 | + | ||
| 1 | 3 | import pytest | |
| 2 | 4 | import requests | |
| 3 | 5 | from httmock import HTTMock, response, urlmatch | |
@@ -22,11 +24,31 @@ def resp_cont(url, request): | |||
| 22 | 24 | return response(200, content, headers, None, 5, request) | |
| 23 | 25 | ||
| 24 | 26 | with HTTMock(resp_cont): | |
| 25 | - http_r = gl.http_request("get", "/projects") | ||
| 27 | + http_r = gl._http_request(method="get", path="/projects") | ||
| 26 | 28 | http_r.json() | |
| 27 | 29 | assert http_r.status_code == 200 | |
| 28 | 30 | ||
| 29 | 31 | ||
| 32 | + def test_http_request_deprecated(gl): | ||
| 33 | + @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", method="get") | ||
| 34 | + def resp_cont(url, request): | ||
| 35 | + headers = {"content-type": "application/json"} | ||
| 36 | + content = '[{"name": "project1"}]' | ||
| 37 | + return response(200, content, headers, None, 5, request) | ||
| 38 | + | ||
| 39 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 40 | + with HTTMock(resp_cont): | ||
| 41 | + http_r = gl.http_request(verb="get", path="/projects") | ||
| 42 | + http_r.json() | ||
| 43 | + assert http_r.status_code == 200 | ||
| 44 | + assert len(caught_warnings) == 1 | ||
| 45 | + warning = caught_warnings[0] | ||
| 46 | + assert isinstance(warning.message, DeprecationWarning) | ||
| 47 | + message = str(caught_warnings[0].message) | ||
| 48 | + assert "deprecated" in message | ||
| 49 | + assert "Gitlab.http_request()" in message | ||
| 50 | + | ||
| 51 | + | ||
| 30 | 52 | def test_http_request_404(gl): | |
| 31 | 53 | @urlmatch(scheme="http", netloc="localhost", path="/api/v4/not_there", method="get") | |
| 32 | 54 | def resp_cont(url, request): | |
@@ -35,7 +57,7 @@ def resp_cont(url, request): | |||
| 35 | 57 | ||
| 36 | 58 | with HTTMock(resp_cont): | |
| 37 | 59 | with pytest.raises(GitlabHttpError): | |
| 38 | - gl.http_request("get", "/not_there") | ||
| 60 | + gl._http_request(method="get", path="/not_there") | ||
| 39 | 61 | ||
| 40 | 62 | ||
| 41 | 63 | @pytest.mark.parametrize("status_code", [500, 502, 503, 504]) | |
@@ -50,7 +72,7 @@ def resp_cont(url, request): | |||
| 50 | 72 | ||
| 51 | 73 | with HTTMock(resp_cont): | |
| 52 | 74 | with pytest.raises(GitlabHttpError): | |
| 53 | - gl.http_request("get", "/projects") | ||
| 75 | + gl._http_request(method="get", path="/projects") | ||
| 54 | 76 | ||
| 55 | 77 | assert call_count == 1 | |
| 56 | 78 | ||
@@ -74,7 +96,9 @@ def resp_cont(url, request): | |||
| 74 | 96 | ) | |
| 75 | 97 | ||
| 76 | 98 | with HTTMock(resp_cont): | |
| 77 | - http_r = gl.http_request("get", "/projects", retry_transient_errors=True) | ||
| 99 | + http_r = gl._http_request( | ||
| 100 | + method="get", path="/projects", retry_transient_errors=True | ||
| 101 | + ) | ||
| 78 | 102 | ||
| 79 | 103 | assert http_r.status_code == 200 | |
| 80 | 104 | assert call_count == calls_before_success | |
@@ -99,7 +123,7 @@ def resp_cont(url, request): | |||
| 99 | 123 | ) | |
| 100 | 124 | ||
| 101 | 125 | with HTTMock(resp_cont): | |
| 102 | - http_r = gl_retry.http_request("get", "/projects") | ||
| 126 | + http_r = gl_retry._http_request(method="get", path="/projects") | ||
| 103 | 127 | ||
| 104 | 128 | assert http_r.status_code == 200 | |
| 105 | 129 | assert call_count == calls_before_success | |
@@ -118,7 +142,9 @@ def resp_cont(url, request): | |||
| 118 | 142 | ||
| 119 | 143 | with HTTMock(resp_cont): | |
| 120 | 144 | with pytest.raises(GitlabHttpError): | |
| 121 | - gl_retry.http_request("get", "/projects", retry_transient_errors=False) | ||
| 145 | + gl_retry._http_request( | ||
| 146 | + method="get", path="/projects", retry_transient_errors=False | ||
| 147 | + ) | ||
| 122 | 148 | ||
| 123 | 149 | assert call_count == 1 | |
| 124 | 150 | ||
@@ -181,7 +207,7 @@ def resp_cont( | |||
| 181 | 207 | return resp_obj | |
| 182 | 208 | ||
| 183 | 209 | with HTTMock(resp_cont): | |
| 184 | - gl.http_request(verb=method, path=api_path) | ||
| 210 | + gl._http_request(method=method, path=api_path) | ||
| 185 | 211 | ||
| 186 | 212 | ||
| 187 | 213 | def test_http_request_302_put_raises_redirect_error(gl): | |
@@ -203,7 +229,7 @@ def resp_cont( | |||
| 203 | 229 | ||
| 204 | 230 | with HTTMock(resp_cont): | |
| 205 | 231 | with pytest.raises(RedirectError) as exc: | |
| 206 | - gl.http_request(verb=method, path=api_path) | ||
| 232 | + gl._http_request(method=method, path=api_path) | ||
| 207 | 233 | error_message = exc.value.error_message | |
| 208 | 234 | assert "Moved Temporarily" in error_message | |
| 209 | 235 | assert "http://localhost/api/v4/user/status" in error_message | |
| Back | FazBrowse Home | New Git URL |
0 commit comments