Compare commits
3 Commits
1.0.4
...
5a464316f1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a464316f1 | ||
|
|
8dec92b43a | ||
|
|
9ac724ed4c |
47
GLPIAPI.py
47
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 :
|
||||
@@ -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())
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
Reference in New Issue
Block a user