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

Commitbaaba22

Browse files
chore: makeGitlab.http_request() a private method
Convert `Gitlab.http_request()` to `Gitlab._http_request()` to signifyit is a private/protected method so that users of the library knowthey should not use the method and we make no API stability promisesfor using it.Add a `Gitlab.http_request()` method which will issue a Deprecationwarning when called. It will pass the call onto`Gitlab._http_request()`Also, in the interest of improving code read-ability, require keywordarg usage when calling `Gitlab._http_request()`
1 parenta1dbe86 commitbaaba22

File tree

2 files changed

+84
-21
lines changed

2 files changed

+84
-21
lines changed

‎gitlab/client.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
importos
2020
importtime
21+
importwarnings
2122
fromtypingimportAny,cast,Dict,List,Optional,Tuple,TYPE_CHECKING,Union
2223

2324
importrequests
@@ -549,7 +550,7 @@ def _build_url(self, path: str) -> str:
549550

550551
def_check_redirects(self,result:requests.Response)->None:
551552
# Check the requests history to detect 301/302 redirections.
552-
# If the initialverb is POST or PUT, the redirected request will use a
553+
# If the initialmethod is POST or PUT, the redirected request will use a
553554
# GET request, leading to unwanted behaviour.
554555
# If we detect a redirection with a POST or a PUT request, we
555556
# raise an exception with a useful error message.
@@ -617,11 +618,45 @@ def http_request(
617618
obey_rate_limit:bool=True,
618619
max_retries:int=10,
619620
**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+
returnself._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,
620655
)->requests.Response:
621656
"""Make an HTTP request to the Gitlab server.
622657
623658
Args:
624-
verb: The HTTP method to call ('get', 'post', 'put', 'delete')
659+
method: The HTTP method to call ('get', 'post', 'put', 'delete')
625660
path: Path or full URL to query ('/projects' or
626661
'http://whatever/v4/api/projecs')
627662
query_data: Data to send as query parameters
@@ -678,7 +713,7 @@ def http_request(
678713
cur_retries=0
679714
whileTrue:
680715
result=self.session.request(
681-
method=verb,
716+
method=method,
682717
url=url,
683718
json=json,
684719
data=data,
@@ -758,8 +793,8 @@ def http_get(
758793
GitlabParsingError: If the json data could not be parsed
759794
"""
760795
query_data=query_dataor {}
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
763798
)
764799

765800
if (
@@ -855,9 +890,9 @@ def http_post(
855890
query_data=query_dataor {}
856891
post_data=post_dataor {}
857892

858-
result=self.http_request(
859-
"post",
860-
path,
893+
result=self._http_request(
894+
method="post",
895+
path=path,
861896
query_data=query_data,
862897
post_data=post_data,
863898
files=files,
@@ -903,9 +938,9 @@ def http_put(
903938
query_data=query_dataor {}
904939
post_data=post_dataor {}
905940

906-
result=self.http_request(
907-
"put",
908-
path,
941+
result=self._http_request(
942+
method="put",
943+
path=path,
909944
query_data=query_data,
910945
post_data=post_data,
911946
files=files,
@@ -933,7 +968,7 @@ def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
933968
Raises:
934969
GitlabHttpError: When the return code is not 2xx
935970
"""
936-
returnself.http_request("delete",path,**kwargs)
971+
returnself._http_request(method="delete",path=path,**kwargs)
937972

938973
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError)
939974
defsearch(
@@ -987,7 +1022,9 @@ def _query(
9871022
self,url:str,query_data:Optional[Dict[str,Any]]=None,**kwargs:Any
9881023
)->None:
9891024
query_data=query_dataor {}
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+
)
9911028
try:
9921029
links=result.links
9931030
iflinks:

‎tests/unit/test_gitlab_http_methods.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
importwarnings
2+
13
importpytest
24
importrequests
35
fromhttmockimportHTTMock,response,urlmatch
@@ -22,11 +24,31 @@ def resp_cont(url, request):
2224
returnresponse(200,content,headers,None,5,request)
2325

2426
withHTTMock(resp_cont):
25-
http_r=gl.http_request("get","/projects")
27+
http_r=gl._http_request(method="get",path="/projects")
2628
http_r.json()
2729
asserthttp_r.status_code==200
2830

2931

32+
deftest_http_request_deprecated(gl):
33+
@urlmatch(scheme="http",netloc="localhost",path="/api/v4/projects",method="get")
34+
defresp_cont(url,request):
35+
headers= {"content-type":"application/json"}
36+
content='[{"name": "project1"}]'
37+
returnresponse(200,content,headers,None,5,request)
38+
39+
withwarnings.catch_warnings(record=True)ascaught_warnings:
40+
withHTTMock(resp_cont):
41+
http_r=gl.http_request(verb="get",path="/projects")
42+
http_r.json()
43+
asserthttp_r.status_code==200
44+
assertlen(caught_warnings)==1
45+
warning=caught_warnings[0]
46+
assertisinstance(warning.message,DeprecationWarning)
47+
message=str(caught_warnings[0].message)
48+
assert"deprecated"inmessage
49+
assert"Gitlab.http_request()"inmessage
50+
51+
3052
deftest_http_request_404(gl):
3153
@urlmatch(scheme="http",netloc="localhost",path="/api/v4/not_there",method="get")
3254
defresp_cont(url,request):
@@ -35,7 +57,7 @@ def resp_cont(url, request):
3557

3658
withHTTMock(resp_cont):
3759
withpytest.raises(GitlabHttpError):
38-
gl.http_request("get","/not_there")
60+
gl._http_request(method="get",path="/not_there")
3961

4062

4163
@pytest.mark.parametrize("status_code", [500,502,503,504])
@@ -50,7 +72,7 @@ def resp_cont(url, request):
5072

5173
withHTTMock(resp_cont):
5274
withpytest.raises(GitlabHttpError):
53-
gl.http_request("get","/projects")
75+
gl._http_request(method="get",path="/projects")
5476

5577
assertcall_count==1
5678

@@ -74,7 +96,9 @@ def resp_cont(url, request):
7496
)
7597

7698
withHTTMock(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+
)
78102

79103
asserthttp_r.status_code==200
80104
assertcall_count==calls_before_success
@@ -99,7 +123,7 @@ def resp_cont(url, request):
99123
)
100124

101125
withHTTMock(resp_cont):
102-
http_r=gl_retry.http_request("get","/projects")
126+
http_r=gl_retry._http_request(method="get",path="/projects")
103127

104128
asserthttp_r.status_code==200
105129
assertcall_count==calls_before_success
@@ -118,7 +142,9 @@ def resp_cont(url, request):
118142

119143
withHTTMock(resp_cont):
120144
withpytest.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+
)
122148

123149
assertcall_count==1
124150

@@ -181,7 +207,7 @@ def resp_cont(
181207
returnresp_obj
182208

183209
withHTTMock(resp_cont):
184-
gl.http_request(verb=method,path=api_path)
210+
gl._http_request(method=method,path=api_path)
185211

186212

187213
deftest_http_request_302_put_raises_redirect_error(gl):
@@ -203,7 +229,7 @@ def resp_cont(
203229

204230
withHTTMock(resp_cont):
205231
withpytest.raises(RedirectError)asexc:
206-
gl.http_request(verb=method,path=api_path)
232+
gl._http_request(method=method,path=api_path)
207233
error_message=exc.value.error_message
208234
assert"Moved Temporarily"inerror_message
209235
assert"http://localhost/api/v4/user/status"inerror_message

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp