diff --git a/GLPIAPI.py b/GLPIAPI.py index 2cee9c5..bfad6eb 100644 --- a/GLPIAPI.py +++ b/GLPIAPI.py @@ -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 : diff --git a/build/pyproject.toml b/build/pyproject.toml index f7dd8eb..b112bfe 100644 --- a/build/pyproject.toml +++ b/build/pyproject.toml @@ -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 = [