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

Commit16f725f

Browse files
chore: replace usage of utils._url_encode() with utils.EncodedId()
utils.EncodedId() has basically the same functionalityy of usingutils._url_encode(). So remove utils._url_encode() as we don't needit.
1 parenta2e7c38 commit16f725f

File tree

9 files changed

+30
-84
lines changed

9 files changed

+30
-84
lines changed

‎gitlab/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def encoded_id(self) -> Any:
223223
path"""
224224
obj_id=self.get_id()
225225
ifisinstance(obj_id,str):
226-
obj_id=gitlab.utils._url_encode(obj_id)
226+
obj_id=gitlab.utils.EncodedId(obj_id)
227227
returnobj_id
228228

229229
@property

‎gitlab/mixins.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def get(
9999
GitlabAuthenticationError: If authentication is not correct
100100
GitlabGetError: If the server cannot perform the request
101101
"""
102-
id=utils._url_encode(id)
102+
ifisinstance(id,str):
103+
id=utils.EncodedId(id)
103104
path=f"{self.path}/{id}"
104105
ifTYPE_CHECKING:
105106
assertself._obj_clsisnotNone
@@ -390,7 +391,7 @@ def update(
390391
ifidisNone:
391392
path=self.path
392393
else:
393-
path=f"{self.path}/{utils._url_encode(id)}"
394+
path=f"{self.path}/{utils.EncodedId(id)}"
394395

395396
self._check_missing_update_attrs(new_data)
396397
files= {}
@@ -443,7 +444,7 @@ def set(self, key: str, value: str, **kwargs: Any) -> base.RESTObject:
443444
Returns:
444445
The created/updated attribute
445446
"""
446-
path=f"{self.path}/{utils._url_encode(key)}"
447+
path=f"{self.path}/{utils.EncodedId(key)}"
447448
data= {"value":value}
448449
server_data=self.gitlab.http_put(path,post_data=data,**kwargs)
449450
ifTYPE_CHECKING:
@@ -476,7 +477,7 @@ def delete(self, id: Union[str, int], **kwargs: Any) -> None:
476477
ifidisNone:
477478
path=self.path
478479
else:
479-
path=f"{self.path}/{utils._url_encode(id)}"
480+
path=f"{self.path}/{utils.EncodedId(id)}"
480481
self.gitlab.http_delete(path,**kwargs)
481482

482483

‎gitlab/utils.py

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
importurllib.parse
19-
fromtypingimportAny,Callable,Dict,Optional,overload,Union
19+
fromtypingimportAny,Callable,Dict,Optional,Union
2020

2121
importrequests
2222

@@ -76,19 +76,16 @@ class EncodedId(str):
7676
# URL-encoded value each time.
7777
original_str:str
7878

79-
def__new__(cls,value:Union[str,int,"EncodedId"])->"EncodedId":
79+
# mypy complains if return type other than the class type. So we ignore issue.
80+
def__new__(# type: ignore
81+
cls,value:Union[str,int,"EncodedId"]
82+
)->Union[int,"EncodedId"]:
8083
# __new__() gets called before __init__()
81-
ifisinstance(value,int):
82-
value=str(value)
83-
# Make sure isinstance() for `EncodedId` comes before check for `str` as
84-
# `EncodedId` is an instance of `str` and would pass that check.
85-
elifisinstance(value,EncodedId):
86-
# We use the original string value to URL-encode
87-
value=value.original_str
88-
elifisinstance(value,str):
89-
pass
90-
else:
91-
raiseValueError(f"Unsupported type received:{type(value)}")
84+
ifisinstance(value, (int,EncodedId)):
85+
returnvalue
86+
87+
ifnotisinstance(value,str):
88+
raiseTypeError(f"Unsupported type received:{type(value)}")
9289
# Set the value our string will return
9390
value=urllib.parse.quote(value,safe="")
9491
returnsuper().__new__(cls,value)
@@ -100,54 +97,19 @@ def __init__(self, value: Union[int, str]) -> None:
10097
# But `value` contains the original value passed in `EncodedId(value)`. We use
10198
# this to always keep the original string that was received so that no matter
10299
# how many times we recurse we only URL-encode our original string once.
103-
ifisinstance(value,int):
104-
value=str(value)
105100
# Make sure isinstance() for `EncodedId` comes before check for `str` as
106101
# `EncodedId` is an instance of `str` and would pass that check.
107-
elifisinstance(value,EncodedId):
102+
ifisinstance(value,EncodedId):
108103
# This is the key part as we are always keeping the original string even
109104
# through multiple recursions.
110105
value=value.original_str
111106
elifisinstance(value,str):
112107
pass
113108
else:
114-
raiseValueError(f"Unsupported type received:{type(value)}")
109+
raiseTypeError(f"Unsupported type received:{type(value)}")
115110
self.original_str=value
116111
super().__init__()
117112

118113

119-
@overload
120-
def_url_encode(id:int)->int:
121-
...
122-
123-
124-
@overload
125-
def_url_encode(id:Union[str,EncodedId])->EncodedId:
126-
...
127-
128-
129-
def_url_encode(id:Union[int,str,EncodedId])->Union[int,EncodedId]:
130-
"""Encode/quote the characters in the string so that they can be used in a path.
131-
132-
Reference to documentation on why this is necessary.
133-
134-
https://docs.gitlab.com/ee/api/index.html#namespaced-path-encoding
135-
136-
If using namespaced API requests, make sure that the NAMESPACE/PROJECT_PATH is
137-
URL-encoded. For example, / is represented by %2F
138-
139-
https://docs.gitlab.com/ee/api/index.html#path-parameters
140-
141-
Path parameters that are required to be URL-encoded must be followed. If not, it
142-
doesn’t match an API endpoint and responds with a 404. If there’s something in front
143-
of the API (for example, Apache), ensure that it doesn’t decode the URL-encoded path
144-
parameters.
145-
146-
"""
147-
ifisinstance(id, (int,EncodedId)):
148-
returnid
149-
returnEncodedId(id)
150-
151-
152114
defremove_none_from_dict(data:Dict[str,Any])->Dict[str,Any]:
153115
return {k:vfork,vindata.items()ifvisnotNone}

‎gitlab/v4/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _process_from_parent_attrs(self) -> None:
7575
ifkeynotinself.args:
7676
continue
7777

78-
self.parent_args[key]=gitlab.utils._url_encode(self.args[key])
78+
self.parent_args[key]=gitlab.utils.EncodedId(self.args[key])
7979
# If we don't delete it then it will be added to the URL as a query-string
8080
delself.args[key]
8181

‎gitlab/v4/objects/features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def set(
5252
Returns:
5353
The created/updated attribute
5454
"""
55-
name=utils._url_encode(name)
55+
name=utils.EncodedId(name)
5656
path=f"{self.path}/{name}"
5757
data= {
5858
"value":value,

‎gitlab/v4/objects/files.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def save( # type: ignore
5656
"""
5757
self.branch=branch
5858
self.commit_message=commit_message
59-
self.file_path=utils._url_encode(self.file_path)
59+
self.file_path=utils.EncodedId(self.file_path)
6060
super(ProjectFile,self).save(**kwargs)
6161

6262
@exc.on_http_error(exc.GitlabDeleteError)
@@ -144,7 +144,7 @@ def create(
144144
assertdataisnotNone
145145
self._check_missing_create_attrs(data)
146146
new_data=data.copy()
147-
file_path=utils._url_encode(new_data.pop("file_path"))
147+
file_path=utils.EncodedId(new_data.pop("file_path"))
148148
path=f"{self.path}/{file_path}"
149149
server_data=self.gitlab.http_post(path,post_data=new_data,**kwargs)
150150
ifTYPE_CHECKING:
@@ -173,7 +173,7 @@ def update( # type: ignore
173173
"""
174174
new_data=new_dataor {}
175175
data=new_data.copy()
176-
file_path=utils._url_encode(file_path)
176+
file_path=utils.EncodedId(file_path)
177177
data["file_path"]=file_path
178178
path=f"{self.path}/{file_path}"
179179
self._check_missing_update_attrs(data)
@@ -203,7 +203,7 @@ def delete( # type: ignore
203203
GitlabAuthenticationError: If authentication is not correct
204204
GitlabDeleteError: If the server cannot perform the request
205205
"""
206-
file_path=utils._url_encode(file_path)
206+
file_path=utils.EncodedId(file_path)
207207
path=f"{self.path}/{file_path}"
208208
data= {"branch":branch,"commit_message":commit_message}
209209
self.gitlab.http_delete(path,query_data=data,**kwargs)
@@ -239,7 +239,7 @@ def raw(
239239
Returns:
240240
The file content
241241
"""
242-
file_path=utils._url_encode(file_path)
242+
file_path=utils.EncodedId(file_path)
243243
path=f"{self.path}/{file_path}/raw"
244244
query_data= {"ref":ref}
245245
result=self.gitlab.http_get(
@@ -266,7 +266,7 @@ def blame(self, file_path: str, ref: str, **kwargs: Any) -> List[Dict[str, Any]]
266266
Returns:
267267
A list of commits/lines matching the file
268268
"""
269-
file_path=utils._url_encode(file_path)
269+
file_path=utils.EncodedId(file_path)
270270
path=f"{self.path}/{file_path}/blame"
271271
query_data= {"ref":ref}
272272
result=self.gitlab.http_list(path,query_data,**kwargs)

‎gitlab/v4/objects/repositories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def update_submodule(
3939
GitlabPutError: If the submodule could not be updated
4040
"""
4141

42-
submodule=utils._url_encode(submodule)
42+
submodule=utils.EncodedId(submodule)
4343
path=f"/projects/{self.encoded_id}/repository/submodules/{submodule}"
4444
data= {"branch":branch,"commit_sha":commit_sha}
4545
if"commit_message"inkwargs:

‎tests/functional/api/test_lazy_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def lazy_project(gl, project):
1212
deftest_lazy_id(project,lazy_project):
1313
assertisinstance(lazy_project.id,str)
1414
assertisinstance(lazy_project.id,gitlab.utils.EncodedId)
15-
assertlazy_project.id==gitlab.utils._url_encode(project.path_with_namespace)
15+
assertlazy_project.id==gitlab.utils.EncodedId(project.path_with_namespace)
1616

1717

1818
deftest_refresh_after_lazy_get_with_path(project,lazy_project):

‎tests/unit/test_utils.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,10 @@
2020
fromgitlabimportutils
2121

2222

23-
deftest_url_encode():
24-
src="nothing_special"
25-
dest="nothing_special"
26-
assertdest==utils._url_encode(src)
27-
28-
src="foo#bar/baz/"
29-
dest="foo%23bar%2Fbaz%2F"
30-
assertdest==utils._url_encode(src)
31-
32-
src="foo%bar/baz/"
33-
dest="foo%25bar%2Fbaz%2F"
34-
assertdest==utils._url_encode(src)
35-
36-
# periods/dots should not be modified
37-
src="docs/README.md"
38-
dest="docs%2FREADME.md"
39-
assertdest==utils._url_encode(src)
40-
41-
4223
classTestEncodedId:
4324
deftest_init_str(self):
4425
obj=utils.EncodedId("Hello")
26+
assert"Hello"==obj
4527
assert"Hello"==str(obj)
4628
assert"Hello"==f"{obj}"
4729

@@ -51,6 +33,7 @@ def test_init_str(self):
5133

5234
deftest_init_int(self):
5335
obj=utils.EncodedId(23)
36+
assert23==obj
5437
assert"23"==str(obj)
5538
assert"23"==f"{obj}"
5639

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp