Added some verification to renew session if session token expires
All checks were successful
Build python package / Build (push) Successful in 36s
All checks were successful
Build python package / Build (push) Successful in 36s
This commit is contained in:
21
GLPIAPI.py
21
GLPIAPI.py
@@ -32,6 +32,15 @@ class GLPIAPI:
|
||||
"Session-Token": self.SessionToken,
|
||||
"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):
|
||||
'''
|
||||
@@ -45,6 +54,7 @@ class GLPIAPI:
|
||||
|
||||
Return a tuple with item id, item data and item count.
|
||||
'''
|
||||
self.CheckConnection()
|
||||
searchAll = False
|
||||
if(deviceName != None):
|
||||
# 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).
|
||||
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
|
||||
if(fieldName != None and fieldValue != None):
|
||||
fieldId = list(self.GetSearchOptions(itemType, fieldName))[0]
|
||||
@@ -170,6 +181,7 @@ class GLPIAPI:
|
||||
return self.GetItems("User", fieldName, fieldValue)
|
||||
|
||||
def GetSearchOptions(self, itemType, fieldName=None):
|
||||
self.CheckConnection()
|
||||
queryUri = f"{self.Server}/apirest.php/listSearchOptions/{itemType}"
|
||||
searchOptions = requests.get(queryUri, headers=self.Headers)
|
||||
if(searchOptions.status_code == 200):
|
||||
@@ -182,6 +194,7 @@ class GLPIAPI:
|
||||
return searchOptions.status_code
|
||||
|
||||
def UploadFile(self, file, path):
|
||||
self.CheckConnection()
|
||||
manifest = {
|
||||
"input": {
|
||||
"name": file,
|
||||
@@ -200,6 +213,7 @@ class GLPIAPI:
|
||||
return requests.post(self.Server+"/apirest.php/Document/", headers=headers, data=data, files=files)
|
||||
|
||||
def SetDocumentToItem(self, itemID, itemType, documentID):
|
||||
self.CheckConnection()
|
||||
body = {
|
||||
"input": {
|
||||
"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)
|
||||
|
||||
def UpdateInventory(self, inventory):
|
||||
self.CheckConnection()
|
||||
headers = {
|
||||
"Content-Type":"Application/x-compress",
|
||||
"user-agent":self.UserAgent
|
||||
@@ -225,6 +240,7 @@ class GLPIAPI:
|
||||
...
|
||||
}
|
||||
'''
|
||||
self.CheckConnection()
|
||||
body = {
|
||||
"input" : {
|
||||
"id" : itemId,
|
||||
@@ -236,7 +252,7 @@ class GLPIAPI:
|
||||
return requests.put(uri, headers=self.Headers, json=body)
|
||||
|
||||
def UpdateSerialNumber(self, deviceid, serialnumber):
|
||||
|
||||
self.CheckConnection()
|
||||
body = {
|
||||
"input" : {
|
||||
"id" : deviceid,
|
||||
@@ -247,7 +263,7 @@ class GLPIAPI:
|
||||
return requests.put(uri, headers=self.Headers, json=body)
|
||||
|
||||
def UpdateItemUser(self, itemID, itemType, username):
|
||||
|
||||
self.CheckConnection()
|
||||
body = {
|
||||
"input" : {
|
||||
"id" : itemID,
|
||||
@@ -262,6 +278,7 @@ class GLPIAPI:
|
||||
- containerName is block label name
|
||||
- containerID is block id
|
||||
'''
|
||||
self.CheckConnection()
|
||||
uri = f"{self.Server}/apirest.php/PluginFields{itemType}{containerName}"
|
||||
searchURI = f"{self.Server}/apirest.php/PluginFields{itemType}{containerName}?range=0-999999999"
|
||||
result = requests.get(searchURI, headers=self.Headers)
|
||||
|
||||
Reference in New Issue
Block a user