diff options
author | John L. Villalovos <john@sodarock.com> | 2022-01-22 10:47:43 -0800 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-01-22 10:47:43 -0800 |
commit | 1905548ec251c88bb6a2b6677940dac9c1ab7167 (patch) | |
tree | 749471d43b6a8297727c8209330c95a7d3ae0d6a /gitlab/client.py | |
parent | 8dfed0c362af2c5e936011fd0b488b8b05e8a8a0 (diff) | |
download | gitlab-jlvillal/arrays.tar.gz |
fix: use the [] after key names for array variablesjlvillal/arrays
1. Create a new CommaSeparatedListAttribute class. This is to indicate
types which are sent to the GitLab server as
comma-separated-strings (CSV) but we have been allowing users to
use a list-of-strings. These values are NOT array values, so
adding [] to the key name breaks them.
2. Rename ListAttribute to ArrayAttribute.
3. If a value is of type ArrayAttribute then append '[]' to the name
of the value.
4. Move processing of most GitlabAttributes into the
client.py:http_request() method. Now we convert our params into a
list of tuples so that we can have multiple identical keys but with
different values.
Fixes: #1698
Diffstat (limited to 'gitlab/client.py')
-rw-r--r-- | gitlab/client.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/gitlab/client.py b/gitlab/client.py index b791c8f..a3e0cc4 100644 --- a/gitlab/client.py +++ b/gitlab/client.py @@ -16,6 +16,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. """Wrapper for the GitLab API.""" +import copy import os import time from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union @@ -27,6 +28,7 @@ from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore import gitlab.config import gitlab.const import gitlab.exceptions +from gitlab import types as gl_types from gitlab import utils REDIRECT_MSG = ( @@ -604,6 +606,28 @@ class Gitlab(object): return (post_data, None, "application/json") + @staticmethod + def _prepare_dict_for_api(*, in_dict: Dict[str, Any]) -> Dict[str, Any]: + result: Dict[str, Any] = {} + for key, value in in_dict.items(): + if isinstance(value, gl_types.GitlabAttribute): + result[key] = value.get_for_api() + else: + result[key] = copy.deepcopy(in_dict[key]) + return result + + @staticmethod + def _param_dict_to_param_tuples(*, params: Dict[str, Any]) -> List[Tuple[str, Any]]: + """Convert a dict to a list of key/values. This will be used to pass + values to requests""" + result: List[Tuple[str, Any]] = [] + for key, value in params.items(): + if isinstance(value, gl_types.GitlabAttribute): + result.extend(value.get_as_tuple_list(key=key)) + else: + result.append((key, value)) + return result + def http_request( self, verb: str, @@ -663,6 +687,10 @@ class Gitlab(object): else: utils.copy_dict(params, kwargs) + tuple_params = self._param_dict_to_param_tuples(params=params) + if isinstance(post_data, dict): + post_data = self._prepare_dict_for_api(in_dict=post_data) + opts = self._get_session_opts() verify = opts.pop("verify") @@ -682,7 +710,7 @@ class Gitlab(object): url=url, json=json, data=data, - params=params, + params=tuple_params, timeout=timeout, verify=verify, stream=streamed, |