Added some verification to renew session if session token expires
All checks were successful
Build python package / Build (push) Successful in 36s

This commit is contained in:
2026-03-28 21:26:07 +01:00
parent b9fd987c5e
commit 906564a44a

View File

@@ -32,6 +32,15 @@ class GLPIAPI:
"Session-Token": self.SessionToken, "Session-Token": self.SessionToken,
"App-Token": self.AppToken "App-Token": self.AppToken
} }
else:
raise Exception(f"{result.status_code} - {result.json()[0]}")
def CheckConnection(self):
sessionUri = f"{self.Server}/apirest.php/getFullSession/"
result = requests.get(sessionUri, headers=self.Headers)
if(result != 200 and result.json()[0] == 'ERROR_SESSION_TOKEN_INVALID'):
self.InitConnection()
return
def GetComputers(self, deviceName=None, serialNumber=None, user=None, imei=None, airwatchDevice=None): def GetComputers(self, deviceName=None, serialNumber=None, user=None, imei=None, airwatchDevice=None):
''' '''
@@ -45,6 +54,7 @@ class GLPIAPI:
Return a tuple with item id, item data and item count. Return a tuple with item id, item data and item count.
''' '''
self.CheckConnection()
searchAll = False searchAll = False
if(deviceName != None): if(deviceName != None):
# Recherche en fonction du nom de l'appareil # Recherche en fonction du nom de l'appareil
@@ -115,6 +125,7 @@ class GLPIAPI:
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). 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). 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).
''' '''
self.CheckConnection()
searchAll = False searchAll = False
if(fieldName != None and fieldValue != None): if(fieldName != None and fieldValue != None):
fieldId = list(self.GetSearchOptions(itemType, fieldName))[0] fieldId = list(self.GetSearchOptions(itemType, fieldName))[0]
@@ -170,6 +181,7 @@ class GLPIAPI:
return self.GetItems("User", fieldName, fieldValue) return self.GetItems("User", fieldName, fieldValue)
def GetSearchOptions(self, itemType, fieldName=None): def GetSearchOptions(self, itemType, fieldName=None):
self.CheckConnection()
queryUri = f"{self.Server}/apirest.php/listSearchOptions/{itemType}" queryUri = f"{self.Server}/apirest.php/listSearchOptions/{itemType}"
searchOptions = requests.get(queryUri, headers=self.Headers) searchOptions = requests.get(queryUri, headers=self.Headers)
if(searchOptions.status_code == 200): if(searchOptions.status_code == 200):
@@ -182,6 +194,7 @@ class GLPIAPI:
return searchOptions.status_code return searchOptions.status_code
def UploadFile(self, file, path): def UploadFile(self, file, path):
self.CheckConnection()
manifest = { manifest = {
"input": { "input": {
"name": file, "name": file,
@@ -200,6 +213,7 @@ class GLPIAPI:
return requests.post(self.Server+"/apirest.php/Document/", headers=headers, data=data, files=files) return requests.post(self.Server+"/apirest.php/Document/", headers=headers, data=data, files=files)
def SetDocumentToItem(self, itemID, itemType, documentID): def SetDocumentToItem(self, itemID, itemType, documentID):
self.CheckConnection()
body = { body = {
"input": { "input": {
"documents_id": documentID, "documents_id": documentID,
@@ -210,6 +224,7 @@ class GLPIAPI:
return requests.post(self.Server+"/apirest.php/Document/"+str(documentID)+"/Document_Item", headers=self.Headers, json=body) return requests.post(self.Server+"/apirest.php/Document/"+str(documentID)+"/Document_Item", headers=self.Headers, json=body)
def UpdateInventory(self, inventory): def UpdateInventory(self, inventory):
self.CheckConnection()
headers = { headers = {
"Content-Type":"Application/x-compress", "Content-Type":"Application/x-compress",
"user-agent":self.UserAgent "user-agent":self.UserAgent
@@ -225,6 +240,7 @@ class GLPIAPI:
... ...
} }
''' '''
self.CheckConnection()
body = { body = {
"input" : { "input" : {
"id" : itemId, "id" : itemId,
@@ -236,7 +252,7 @@ class GLPIAPI:
return requests.put(uri, headers=self.Headers, json=body) return requests.put(uri, headers=self.Headers, json=body)
def UpdateSerialNumber(self, deviceid, serialnumber): def UpdateSerialNumber(self, deviceid, serialnumber):
self.CheckConnection()
body = { body = {
"input" : { "input" : {
"id" : deviceid, "id" : deviceid,
@@ -247,7 +263,7 @@ class GLPIAPI:
return requests.put(uri, headers=self.Headers, json=body) return requests.put(uri, headers=self.Headers, json=body)
def UpdateItemUser(self, itemID, itemType, username): def UpdateItemUser(self, itemID, itemType, username):
self.CheckConnection()
body = { body = {
"input" : { "input" : {
"id" : itemID, "id" : itemID,
@@ -262,6 +278,7 @@ class GLPIAPI:
- containerName is block label name - containerName is block label name
- containerID is block id - containerID is block id
''' '''
self.CheckConnection()
uri = f"{self.Server}/apirest.php/PluginFields{itemType}{containerName}" uri = f"{self.Server}/apirest.php/PluginFields{itemType}{containerName}"
searchURI = f"{self.Server}/apirest.php/PluginFields{itemType}{containerName}?range=0-999999999" searchURI = f"{self.Server}/apirest.php/PluginFields{itemType}{containerName}?range=0-999999999"
result = requests.get(searchURI, headers=self.Headers) result = requests.get(searchURI, headers=self.Headers)