Adding profile selection methods
All checks were successful
Build python package / Build (push) Successful in 32s
All checks were successful
Build python package / Build (push) Successful in 32s
This commit is contained in:
39
GLPIAPI.py
39
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 :
|
||||
|
||||
Reference in New Issue
Block a user