canonical / python-libmaas

Unofficial python client library for MAAS
https://maas.io
Other
62 stars 70 forks source link

CallAPI breaks when string value passed to the payload during a GET method #277

Closed canozyurt closed 2 years ago

canozyurt commented 2 years ago
>>> session.MAAS.get_config(name="ntp_servers")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/maas/client/utils/maas_async.py", line 43, in wrapper
    result = eventloop.run_until_complete(result)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.8/dist-packages/maas/client/bones/__init__.py", line 307, in __call__
    response = await self.bind(**params).call(**data)
  File "/usr/local/lib/python3.8/dist-packages/maas/client/bones/__init__.py", line 479, in dispatch
    raise CallError(request, response, content, self)
maas.client.bones.CallError: GET http://192.168.61.8:5240/MAAS/api/2.0/maas/?op=get_config&name=n&name=t&name=p&name=_&name=s&name=e&name=r&name=v&name=e&name=r&name=s -> HTTP 400 Bad Request ({"s": ["s is not a valid config setting (valid se…)
>>>

The call above doesn't work simply because the code below iterates through the given string value and expands it.

        def expand(data):
            for name, value in data.items():
                if isinstance(value, Iterable):
                    for value in value:
                        yield name, value
                else:
                    yield name, value

        # `data` must be an iterable yielding 2-tuples.
        if self.action.method in ("GET", "DELETE"):
            # MAAS does not expect an entity-body for GET or DELETE.
            data = expand(data)
        else:
            # MAAS expects and entity-body for PUT and POST.
            data = data.items()

MAAS does not expect an entity-body for GET or DELETE.

Although changing the request method to POST may fix it, I don't think backport to APIv2 is possible.