Compare commits
8 Commits
1.0.3
...
1c28d0ac84
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c28d0ac84 | ||
| 906564a44a | |||
| b9fd987c5e | |||
| a3a393088c | |||
| 6599b967ab | |||
| 0155644b65 | |||
| 3d920fd5e5 | |||
| 660b8e8463 |
@@ -4,16 +4,25 @@ on: [push]
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
Build:
|
Build:
|
||||||
runs-on: windows
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: python
|
||||||
steps:
|
steps:
|
||||||
|
- name: Act Workaround # https://github.com/nektos/act/issues/973
|
||||||
|
if: ${{ env.ACT }}
|
||||||
|
run: curl -fsSL https://deb.nodesource.com/setup_22.x | bash && apt install -y nodejs
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
uses: actions/checkout@main
|
uses: actions/checkout@main
|
||||||
|
- name: Setting up python modules to build package
|
||||||
|
run: |
|
||||||
|
python -m pip install build twine
|
||||||
- name: Building the package
|
- name: Building the package
|
||||||
run: |
|
run: |
|
||||||
powershell mv ${{ gitea.workspace }}\GLPIAPI.py ${{ gitea.workspace }}\build\src\GLPIAPI\
|
mkdir ${{ gitea.workspace }}/build/src/GLPIAPI/
|
||||||
cd ${{ gitea.workspace }}\build
|
mv ${{ gitea.workspace }}/GLPIAPI.py ${{ gitea.workspace }}/build/src/GLPIAPI/GLPIAPI.py
|
||||||
|
cd ${{ gitea.workspace }}/build
|
||||||
python -m build
|
python -m build
|
||||||
- name: Publish package
|
- name: Publish package
|
||||||
run: |
|
run: |
|
||||||
python -m twine upload -u ${{ secrets.repo_user }} -p ${{ secrets.repo_pass }} --repository-url ${{ secrets.repo_url }} ${{ gitea.workspace }}\build\dist\*
|
python -m twine upload -u ${{ secrets.repo_user }} -p ${{ secrets.repo_pass }} --repository-url ${{ secrets.repo_url }} ${{ gitea.workspace }}/build/dist/*
|
||||||
if: github.ref_type == 'tag'
|
if: github.ref_type == 'tag'
|
||||||
112
GLPIAPI.py
112
GLPIAPI.py
@@ -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.status_code != 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
|
||||||
@@ -100,8 +110,55 @@ class GLPIAPI:
|
|||||||
else:
|
else:
|
||||||
itemID = list(search["data"].keys())
|
itemID = list(search["data"].keys())
|
||||||
return itemID, search["data"], search["totalcount"]
|
return itemID, search["data"], search["totalcount"]
|
||||||
|
else:
|
||||||
|
return None, None, 0
|
||||||
|
|
||||||
return None, None, 0
|
return None, search, 0
|
||||||
|
|
||||||
|
def GetItems(self, itemType, fieldName=None, fieldValue=None):
|
||||||
|
'''
|
||||||
|
Search for items of a specific item type in GLPI. A filter can be set using fieldName and fieldValue :
|
||||||
|
fieldName: must be the name of the field as visible in GLPI
|
||||||
|
fieldValue: a string
|
||||||
|
If only itemType is set, it will search for all items
|
||||||
|
|
||||||
|
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]
|
||||||
|
# Recherche en fonction de l'utilisateur de l'appareil
|
||||||
|
search_parameter = f'is_deleted=0&criteria[0][field]={fieldId}&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{fieldValue}$'
|
||||||
|
else:
|
||||||
|
searchAll = True
|
||||||
|
|
||||||
|
if(searchAll):
|
||||||
|
searchUri = f"{self.Server}/apirest.php/search/{itemType}/?range=0-9999999&is_deleted=0"
|
||||||
|
else:
|
||||||
|
searchUri = f"{self.Server}/apirest.php/search/{itemType}?{search_parameter}"
|
||||||
|
|
||||||
|
search = requests.get(searchUri, headers=self.Headers)
|
||||||
|
|
||||||
|
if(search.status_code == 200):
|
||||||
|
search = search.json()
|
||||||
|
if(search["totalcount"] == 1):
|
||||||
|
itemID = list(search["data"].keys())[0]
|
||||||
|
data = search["data"][itemID]
|
||||||
|
|
||||||
|
return itemID, data, search["totalcount"]
|
||||||
|
elif(search["totalcount"] > 1):
|
||||||
|
if(searchAll):
|
||||||
|
idFieldNumber = list(self.GetSearchOptions(itemType, 'id'))[0]
|
||||||
|
itemID = [i[idFieldNumber] for i in search["data"]]
|
||||||
|
else:
|
||||||
|
itemID = list(search["data"].keys())
|
||||||
|
return itemID, search["data"], search["totalcount"]
|
||||||
|
else:
|
||||||
|
return None, None, 0
|
||||||
|
|
||||||
|
return None, search, 0
|
||||||
|
|
||||||
def GetUsers(self, username=None, email=None):
|
def GetUsers(self, username=None, email=None):
|
||||||
'''
|
'''
|
||||||
@@ -112,37 +169,32 @@ class GLPIAPI:
|
|||||||
|
|
||||||
Returns a tuple with user id, user data and user count
|
Returns a tuple with user id, user data and user count
|
||||||
'''
|
'''
|
||||||
searchAll = False
|
fieldName = None
|
||||||
|
fieldValue = None
|
||||||
if(username != None):
|
if(username != None):
|
||||||
search_parameter = f'is_deleted=0&criteria[0][field]=1&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{username}$'
|
fieldName = 'login'
|
||||||
|
fieldValue = username
|
||||||
elif(email != None):
|
elif(email != None):
|
||||||
search_parameter = f'is_deleted=0&criteria[0][field]=5&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{email}$'
|
fieldName = 'Emails'
|
||||||
else:
|
fieldValue = email
|
||||||
searchAll = True
|
|
||||||
|
|
||||||
if(searchAll):
|
return self.GetItems("User", fieldName, fieldValue)
|
||||||
searchUri = f"{self.Server}/apirest.php/search/User/?range=0-9999999&is_deleted=0"
|
|
||||||
else:
|
|
||||||
searchUri = f"{self.Server}/apirest.php/search/User?{search_parameter}"
|
|
||||||
search = requests.get(searchUri, headers=self.Headers)
|
|
||||||
if(search.status_code == 200):
|
|
||||||
search = search.json()
|
|
||||||
if(search["totalcount"] == 1):
|
|
||||||
userID = list(search["data"].keys())[0]
|
|
||||||
data = search["data"][userID]
|
|
||||||
|
|
||||||
return userID, data, search["totalcount"]
|
def GetSearchOptions(self, itemType, fieldName=None):
|
||||||
elif(search["totalcount"] > 1):
|
self.CheckConnection()
|
||||||
if(searchAll):
|
queryUri = f"{self.Server}/apirest.php/listSearchOptions/{itemType}"
|
||||||
# requires id to be in the display preferences of the api user
|
searchOptions = requests.get(queryUri, headers=self.Headers)
|
||||||
userID = [i["2"] for i in search["data"]]
|
if(searchOptions.status_code == 200):
|
||||||
else:
|
searchOptions = searchOptions.json()
|
||||||
userID = list(search["data"].keys())
|
if(fieldName != None):
|
||||||
return userID, search["data"], search["totalcount"]
|
for k,v in searchOptions.items():
|
||||||
|
if(v['name'].lower() == fieldName.lower()):
|
||||||
return None, None, 0
|
return {k : searchOptions[k]}
|
||||||
|
return searchOptions
|
||||||
|
return searchOptions.status_code
|
||||||
|
|
||||||
def UploadFile(self, file, path):
|
def UploadFile(self, file, path):
|
||||||
|
self.CheckConnection()
|
||||||
manifest = {
|
manifest = {
|
||||||
"input": {
|
"input": {
|
||||||
"name": file,
|
"name": file,
|
||||||
@@ -161,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,
|
||||||
@@ -171,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
|
||||||
@@ -186,6 +240,7 @@ class GLPIAPI:
|
|||||||
...
|
...
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
|
self.CheckConnection()
|
||||||
body = {
|
body = {
|
||||||
"input" : {
|
"input" : {
|
||||||
"id" : itemId,
|
"id" : itemId,
|
||||||
@@ -197,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,
|
||||||
@@ -208,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,
|
||||||
@@ -223,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)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "GLPIAPI"
|
name = "GLPIAPI"
|
||||||
version = "1.0.3"
|
version = "1.0.4"
|
||||||
description = "A module python to make it easier to use GLPI API"
|
description = "A module python to make it easier to use GLPI API"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
Reference in New Issue
Block a user