diff --git a/GLPIAPI.py b/GLPIAPI.py index 4e380d2..e53294c 100644 --- a/GLPIAPI.py +++ b/GLPIAPI.py @@ -103,10 +103,19 @@ class GLPIAPI: return None, None, 0 - def GetItems(self, itemType, fieldId=None, fieldValue=None): + def GetItems(self, itemType, fieldName=None, fieldValue=None): + ''' + Search for items of a specific item type in GLPI. A filter can be set using fieldName and fieldValue : + fieldName: must be the name of the field as visible in GLPI + fieldValue: a string + If only itemType is set, it will search for all items + Return a tuple with item id (in a list if there are more than 1), item data (in a list of dict items) and item count (int). + If the query does not succeed it will return a tuple with the http status code instead of item data (e.g. None, 400, 0). + ''' searchAll = False - if(fieldId != None and fieldValue != None): + if(fieldName != None and fieldValue != None): + fieldId = list(self.GetSearchOptions(itemType, fieldName))[0] # Recherche en fonction de l'utilisateur de l'appareil search_parameter = f'is_deleted=0&criteria[0][field]={fieldId}&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{fieldValue}$' else: @@ -128,13 +137,13 @@ class GLPIAPI: return itemID, data, search["totalcount"] elif(search["totalcount"] > 1): if(searchAll): - # return None since id can be in a different key based on itemtype - itemID = None + idFieldNumber = list(self.GetSearchOptions(itemType, 'id'))[0] + itemID = [i[idFieldNumber] for i in search["data"]] else: itemID = list(search["data"].keys()) return itemID, search["data"], search["totalcount"] - return None, None, 0 + return None, search.status, 0 def GetUsers(self, username=None, email=None): '''