diff options
author | John L. Villalovos <john@sodarock.com> | 2022-01-13 11:17:40 -0800 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-01-13 11:17:40 -0800 |
commit | b07eece0a35dbc48076c9ec79f65f1e3fa17a872 (patch) | |
tree | 7a3109087cee26308c4359d4cdcdada8999d5b22 /gitlab/utils.py | |
parent | a2e7c383e10509b6eb0fa8760727036feb0807c8 (diff) | |
download | gitlab-b07eece0a35dbc48076c9ec79f65f1e3fa17a872.tar.gz |
chore: replace usage of utils._url_encode() with utils.EncodedId()jlvillal/encoded_id
utils.EncodedId() has basically the same functionalityy of using
utils._url_encode(). So remove utils._url_encode() as we don't need
it.
Diffstat (limited to 'gitlab/utils.py')
-rw-r--r-- | gitlab/utils.py | 85 |
1 files changed, 10 insertions, 75 deletions
diff --git a/gitlab/utils.py b/gitlab/utils.py index 61e98f3..8b3054c 100644 --- a/gitlab/utils.py +++ b/gitlab/utils.py @@ -16,7 +16,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import urllib.parse -from typing import Any, Callable, Dict, Optional, overload, Union +from typing import Any, Callable, Dict, Optional, Union import requests @@ -71,83 +71,18 @@ class EncodedId(str): https://docs.gitlab.com/ee/api/index.html#path-parameters """ - # `original_str` will contain the original string value that was used to create the - # first instance of EncodedId. We will use this original value to generate the - # URL-encoded value each time. - original_str: str - - def __new__(cls, value: Union[str, int, "EncodedId"]) -> "EncodedId": - # __new__() gets called before __init__() - if isinstance(value, int): - value = str(value) - # Make sure isinstance() for `EncodedId` comes before check for `str` as - # `EncodedId` is an instance of `str` and would pass that check. - elif isinstance(value, EncodedId): - # We use the original string value to URL-encode - value = value.original_str - elif isinstance(value, str): - pass - else: - raise ValueError(f"Unsupported type received: {type(value)}") - # Set the value our string will return + # mypy complains if return type other than the class type. So we ignore issue. + def __new__( # type: ignore + cls, value: Union[str, int, "EncodedId"] + ) -> Union[int, "EncodedId"]: + if isinstance(value, (int, EncodedId)): + return value + + if not isinstance(value, str): + raise TypeError(f"Unsupported type received: {type(value)}") value = urllib.parse.quote(value, safe="") return super().__new__(cls, value) - def __init__(self, value: Union[int, str]) -> None: - # At this point `super().__str__()` returns the URL-encoded value. Which means - # when using this as a `str` it will return the URL-encoded value. - # - # But `value` contains the original value passed in `EncodedId(value)`. We use - # this to always keep the original string that was received so that no matter - # how many times we recurse we only URL-encode our original string once. - if isinstance(value, int): - value = str(value) - # Make sure isinstance() for `EncodedId` comes before check for `str` as - # `EncodedId` is an instance of `str` and would pass that check. - elif isinstance(value, EncodedId): - # This is the key part as we are always keeping the original string even - # through multiple recursions. - value = value.original_str - elif isinstance(value, str): - pass - else: - raise ValueError(f"Unsupported type received: {type(value)}") - self.original_str = value - super().__init__() - - -@overload -def _url_encode(id: int) -> int: - ... - - -@overload -def _url_encode(id: Union[str, EncodedId]) -> EncodedId: - ... - - -def _url_encode(id: Union[int, str, EncodedId]) -> Union[int, EncodedId]: - """Encode/quote the characters in the string so that they can be used in a path. - - Reference to documentation on why this is necessary. - - https://docs.gitlab.com/ee/api/index.html#namespaced-path-encoding - - If using namespaced API requests, make sure that the NAMESPACE/PROJECT_PATH is - URL-encoded. For example, / is represented by %2F - - https://docs.gitlab.com/ee/api/index.html#path-parameters - - Path parameters that are required to be URL-encoded must be followed. If not, it - doesn’t match an API endpoint and responds with a 404. If there’s something in front - of the API (for example, Apache), ensure that it doesn’t decode the URL-encoded path - parameters. - - """ - if isinstance(id, (int, EncodedId)): - return id - return EncodedId(id) - def remove_none_from_dict(data: Dict[str, Any]) -> Dict[str, Any]: return {k: v for k, v in data.items() if v is not None} |