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:
|
||||
Build:
|
||||
runs-on: windows
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: python
|
||||
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
|
||||
uses: actions/checkout@main
|
||||
- name: Setting up python modules to build package
|
||||
run: |
|
||||
python -m pip install build twine
|
||||
- name: Building the package
|
||||
run: |
|
||||
powershell mv ${{ gitea.workspace }}\GLPIAPI.py ${{ gitea.workspace }}\build\src\GLPIAPI\
|
||||
cd ${{ gitea.workspace }}\build
|
||||
mkdir ${{ gitea.workspace }}/build/src/GLPIAPI/
|
||||
mv ${{ gitea.workspace }}/GLPIAPI.py ${{ gitea.workspace }}/build/src/GLPIAPI/GLPIAPI.py
|
||||
cd ${{ gitea.workspace }}/build
|
||||
python -m build
|
||||
- name: Publish package
|
||||
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'
|
||||
112
GLPIAPI.py
112
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.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):
|
||||
'''
|
||||
@@ -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
|
||||
@@ -100,8 +110,55 @@ class GLPIAPI:
|
||||
else:
|
||||
itemID = list(search["data"].keys())
|
||||
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):
|
||||
'''
|
||||
@@ -112,37 +169,32 @@ class GLPIAPI:
|
||||
|
||||
Returns a tuple with user id, user data and user count
|
||||
'''
|
||||
searchAll = False
|
||||
fieldName = None
|
||||
fieldValue = 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):
|
||||
search_parameter = f'is_deleted=0&criteria[0][field]=5&withindexes=true&criteria[0][searchtype]=contains&criteria[0][value]=^{email}$'
|
||||
else:
|
||||
searchAll = True
|
||||
fieldName = 'Emails'
|
||||
fieldValue = email
|
||||
|
||||
if(searchAll):
|
||||
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 self.GetItems("User", fieldName, fieldValue)
|
||||
|
||||
return userID, data, search["totalcount"]
|
||||
elif(search["totalcount"] > 1):
|
||||
if(searchAll):
|
||||
# requires id to be in the display preferences of the api user
|
||||
userID = [i["2"] for i in search["data"]]
|
||||
else:
|
||||
userID = list(search["data"].keys())
|
||||
return userID, search["data"], search["totalcount"]
|
||||
|
||||
return None, None, 0
|
||||
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):
|
||||
searchOptions = searchOptions.json()
|
||||
if(fieldName != None):
|
||||
for k,v in searchOptions.items():
|
||||
if(v['name'].lower() == fieldName.lower()):
|
||||
return {k : searchOptions[k]}
|
||||
return searchOptions
|
||||
return searchOptions.status_code
|
||||
|
||||
def UploadFile(self, file, path):
|
||||
self.CheckConnection()
|
||||
manifest = {
|
||||
"input": {
|
||||
"name": file,
|
||||
@@ -161,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,
|
||||
@@ -171,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
|
||||
@@ -186,6 +240,7 @@ class GLPIAPI:
|
||||
...
|
||||
}
|
||||
'''
|
||||
self.CheckConnection()
|
||||
body = {
|
||||
"input" : {
|
||||
"id" : itemId,
|
||||
@@ -197,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,
|
||||
@@ -208,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,
|
||||
@@ -223,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)
|
||||
|
||||
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "GLPIAPI"
|
||||
version = "1.0.3"
|
||||
version = "1.0.4"
|
||||
description = "A module python to make it easier to use GLPI API"
|
||||
readme = "README.md"
|
||||
dependencies = [
|
||||
|
||||
Reference in New Issue
Block a user