Description
The search() API helper currently passes criteria, metacriteria, and forcedisplay directly to the HTTP client as query parameters.
Example:
search = api.computer.search(
criteria=[
{
"field": 126,
"searchtype": "equals",
"value": "X.X.X.X",
}
],
forcedisplay=[1, 5, 126],
range="0-1999",
)
The request completes successfully, but GLPI returns no results because the generated query string is not encoded in the format expected by the GLPI Search API.
Instead of generating keys such as:
criteria[0][field]=126
criteria[0][searchtype]=equals
criteria[0][value]=X.X.X.X
forcedisplay[0]=1
forcedisplay[1]=5
forcedisplay[2]=126
the underlying HTTP client receives Python lists/dicts and serializes them incorrectly.
This makes complex searches silently fail, especially when using criteria, metacriteria, or forcedisplay.
Proposed solution
Flatten nested search parameters before passing them to requests / aiohttp.
Example implementation:
def _flatten_search_params(params: dict) -> dict:
flat = {}
for key in ("criteria", "metacriteria"):
items = params.pop(key, None)
if not items:
continue
for i, crit in enumerate(items):
for sub_key, value in crit.items():
flat[f"{key}[{i}][{sub_key}]"] = value
forcedisplay = params.pop("forcedisplay", None)
if forcedisplay:
for i, field in enumerate(forcedisplay):
flat[f"forcedisplay[{i}]"] = field
flat.update(params)
return flat
and then:
params = _boolify_params(_flatten_search_params(kwargs))
inside GlpiAPI.search().
Impact
This change allows the Python API to generate search requests that match the format documented by GLPI and enables criteria-based searches such as searching computers by IP address, serial number, inventory number, etc.
Notes
I have a working local patch implementing this approach. I have not opened a pull request yet, but I wanted to report the issue and share the proposed fix.
Additional informations
Name: glpi-utils
Version: 1.4.4
Summary: Python library for working with the GLPI REST API (9.1+, 10.x, 11.x)
Home-page: https://github.com/giovanny07/python-glpi-utils
Author: Giovanny Rodriguez
Author-email: giovanny@imagunet.com
License: UNKNOWN
Location: /home/adamo/libtest/python-glpi-utils
Requires: requests
Required-by:
Description
The search() API helper currently passes criteria, metacriteria, and forcedisplay directly to the HTTP client as query parameters.
Example:
The request completes successfully, but GLPI returns no results because the generated query string is not encoded in the format expected by the GLPI Search API.
Instead of generating keys such as:
the underlying HTTP client receives Python lists/dicts and serializes them incorrectly.
This makes complex searches silently fail, especially when using criteria, metacriteria, or forcedisplay.
Proposed solution
Flatten nested search parameters before passing them to requests / aiohttp.
Example implementation:
and then:
params = _boolify_params(_flatten_search_params(kwargs))inside
GlpiAPI.search().Impact
This change allows the Python API to generate search requests that match the format documented by GLPI and enables criteria-based searches such as searching computers by IP address, serial number, inventory number, etc.
Notes
I have a working local patch implementing this approach. I have not opened a pull request yet, but I wanted to report the issue and share the proposed fix.
Additional informations
Python version :
Python 3.10.12GLPI version :
GLPI 10.0.19Lib version :