|
16 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 |
|
18 | 18 | import urllib.parse
|
19 |
| -from typing import Any, Callable, Dict, Optional, overload, Union |
| 19 | +from typing import Any, Callable, Dict, Optional, Union |
20 | 20 |
|
21 | 21 | import requests
|
22 | 22 |
|
@@ -71,83 +71,18 @@ class EncodedId(str):
|
71 | 71 | https://docs.gitlab.com/ee/api/index.html#path-parameters
|
72 | 72 | """
|
73 | 73 |
|
74 |
| - # `original_str` will contain the original string value that was used to create the |
75 |
| - # first instance of EncodedId. We will use this original value to generate the |
76 |
| - # URL-encoded value each time. |
77 |
| - original_str: str |
78 |
| - |
79 |
| - def __new__(cls, value: Union[str, int, "EncodedId"]) -> "EncodedId": |
80 |
| - # __new__() gets called before __init__() |
81 |
| - if isinstance(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 |
| - elif isinstance(value, EncodedId): |
86 |
| - # We use the original string value to URL-encode |
87 |
| - value = value.original_str |
88 |
| - elif isinstance(value, str): |
89 |
| - pass |
90 |
| - else: |
91 |
| - raise ValueError(f"Unsupported type received: {type(value)}") |
92 |
| - # Set the value our string will return |
| 74 | + # mypy complains if return type other than the class type. So we ignore issue. |
| 75 | + def __new__( # type: ignore |
| 76 | + cls, value: Union[str, int, "EncodedId"] |
| 77 | + ) -> Union[int, "EncodedId"]: |
| 78 | + if isinstance(value, (int, EncodedId)): |
| 79 | + return value |
| 80 | + |
| 81 | + if not isinstance(value, str): |
| 82 | + raise TypeError(f"Unsupported type received: {type(value)}") |
93 | 83 | value = urllib.parse.quote(value, safe="")
|
94 | 84 | return super().__new__(cls, value)
|
95 | 85 |
|
96 |
| - def __init__(self, value: Union[int, str]) -> None: |
97 |
| - # At this point `super().__str__()` returns the URL-encoded value. Which means |
98 |
| - # when using this as a `str` it will return the URL-encoded value. |
99 |
| - # |
100 |
| - # But `value` contains the original value passed in `EncodedId(value)`. We use |
101 |
| - # this to always keep the original string that was received so that no matter |
102 |
| - # how many times we recurse we only URL-encode our original string once. |
103 |
| - if isinstance(value, int): |
104 |
| - value = str(value) |
105 |
| - # Make sure isinstance() for `EncodedId` comes before check for `str` as |
106 |
| - # `EncodedId` is an instance of `str` and would pass that check. |
107 |
| - elif isinstance(value, EncodedId): |
108 |
| - # This is the key part as we are always keeping the original string even |
109 |
| - # through multiple recursions. |
110 |
| - value = value.original_str |
111 |
| - elif isinstance(value, str): |
112 |
| - pass |
113 |
| - else: |
114 |
| - raise ValueError(f"Unsupported type received: {type(value)}") |
115 |
| - self.original_str = value |
116 |
| - super().__init__() |
117 |
| - |
118 |
| - |
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 |
| - if isinstance(id, (int, EncodedId)): |
148 |
| - return id |
149 |
| - return EncodedId(id) |
150 |
| - |
151 | 86 |
|
152 | 87 | def remove_none_from_dict(data: Dict[str, Any]) -> Dict[str, Any]:
|
153 | 88 | return {k: v for k, v in data.items() if v is not None}
|
0 commit comments