3 Commits
1.0.4 ... 1.0.5

Author SHA1 Message Date
Jason SECULA
5a464316f1 fixed misplace of +1 for fieldstodisplay
All checks were successful
Build python package / Build (push) Successful in 33s
2026-04-23 16:39:26 +02:00
Jason SECULA
8dec92b43a Adding profile selection methods
All checks were successful
Build python package / Build (push) Successful in 32s
2026-04-23 16:26:03 +02:00
Jason SECULA
9ac724ed4c Fixed error with id not shown in GetItems() 2026-04-23 16:07:31 +02:00
2 changed files with 44 additions and 5 deletions

View File

@@ -3,7 +3,7 @@ import json
from datetime import datetime
class GLPIAPI:
def __init__(self, Server, AppToken, UserToken, UserAgent="GLPI API"):
def __init__(self, Server, AppToken, UserToken, UserAgent="GLPI API", Profile=None):
self.Server = Server
self.AppToken = AppToken
self.UserToken = UserToken
@@ -11,7 +11,14 @@ class GLPIAPI:
self.SessionToken = None
self.StatusCode = None
self.Headers = None
self.ActiveProfile = None
self.InitConnection()
if(Profile == None):
self.ActiveProfile = self.GetActiveProfile()
else:
self.ActiveProfile = Profile
profileId = self.GetItems('Profile', 'Profile.name', Profile)[1]['2']
self.SetActiveProfile(profileId)
def InitConnection(self):
initURI = '/apirest.php/initSession/'
@@ -32,6 +39,9 @@ class GLPIAPI:
"Session-Token": self.SessionToken,
"App-Token": self.AppToken
}
if(self.GetActiveProfile() != self.ActiveProfile and self.ActiveProfile != None):
profileId = self.GetItems('Profile', 'Profile.name', self.ActiveProfile)[1]['2']
self.SetActiveProfile(profileId)
else:
raise Exception(f"{result.status_code} - {result.json()[0]}")
@@ -42,6 +52,33 @@ class GLPIAPI:
self.InitConnection()
return
def GetUserProfiles(self):
self.CheckConnection()
uri = f"{self.Server}/apirest.php/getMyProfiles"
req = requests.get(uri, headers=self.Headers)
if(req.status_code == 200):
return req.json()
else:
return req.status_code
def GetActiveProfile(self):
self.CheckConnection()
uri = f"{self.Server}/apirest.php/getActiveProfile"
req = requests.get(uri, headers=self.Headers)
if(req.status_code == 200):
return req.json()['active_profile']['name']
else:
return req.status_code
def SetActiveProfile(self, profileId):
self.CheckConnection()
body = {
"profiles_id" : profileId
}
uri = f"{self.Server}/apirest.php/changeActiveProfile"
return requests.post(uri, headers=self.Headers, json=body)
def GetComputers(self, deviceName=None, serialNumber=None, user=None, imei=None, airwatchDevice=None, fieldsToDisplay=[], trashbin=0):
'''
Search for computer items in GLPI based on one of the possibles parameters :
@@ -146,9 +183,12 @@ class GLPIAPI:
else:
searchUri = f"{self.Server}/apirest.php/search/{itemType}?{search_parameter}"
idFieldNumber = list(self.GetSearchOptions(itemType, f'{itemType}.id'))[0]
searchUri += f"&forcedisplay[0]={idFieldNumber}"
if(fieldsToDisplay != []):
for i in range(len(fieldsToDisplay)):
searchUri += f"&forcedisplay[{i}]={fieldsToDisplay[i]}"
for i in range(1, len(fieldsToDisplay)+1):
searchUri += f"&forcedisplay[{i}]={fieldsToDisplay[i-1]}"
search = requests.get(searchUri, headers=self.Headers)
@@ -161,7 +201,6 @@ class GLPIAPI:
return itemID, data, search["totalcount"]
elif(search["totalcount"] > 1):
if(searchAll):
idFieldNumber = list(self.GetSearchOptions(itemType, f'{itemType}.id'))[0]
itemID = [i[idFieldNumber] for i in search["data"]]
else:
itemID = list(search["data"].keys())

View File

@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "GLPIAPI"
version = "1.0.4"
version = "1.0.5"
description = "A module python to make it easier to use GLPI API"
readme = "README.md"
dependencies = [