Added the rest of the REST-API funtions to the library

Updated the documentation

The only thing that is still broken is the getStream() as it is 400ing and I cannot tell why
This commit is contained in:
Benjamyn Love 2018-11-11 15:06:16 +11:00
parent 247b82a652
commit 814c0c1f46
3 changed files with 193 additions and 13 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ env/
main*.py main*.py
*.swp *.swp
*.pyc *.pyc
__pycache__/

View File

@ -5,10 +5,72 @@ Initialize the wrapper with the token you want to use
i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD") i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD")
<h3>Functions Implimented</h3> <b/> <h3>Functions Implimented</h3> <b/>
-getApplications() // Gets a list of applications and returns the value in a dictionary
-createApplication(name, description) // Creates a new Application - basicAuth(user, passwd)
Sets up basic Auth for inclusion in the header
-deleteApplication(id) // Deletes the requseted application (Note for now this uses a string) - getApplications()
Returns a JSON formatted list of applications
-sendMessage(title, message) // Sends a notification with the specified title/message - createApplication(name, description)
Creates an application with defined name and description
- deleteApplication(appid)
Delete Application via appid
- updateApplicationImage(appid, imgData)
Updates the application image
- getClients()
Returns a JSON formatted list of Clients
- addClient(name)
Creates a new client with name
- deleteClient(clientid)
Delete clients via clientID
- getMessagesByApp(appid)
Returns a JSON formatted list of messages in an application
- deleteAllMessagesFromApp(appid)
Deletes all messages in an application
- getAllMessages()
Returns a JSON formatted list of all messages
- sendMessage(title, message)
Sends a message using an application auth token (Note this is the only function that requires an application token)
- deleteAllMessages()
Deletes all messages
- deleteMessageByID(msgid)
Delete a message by its ID
- getStream() ## Currently broken, returns 400 error, no idea why
This should return all new messages but it doesn't, also broken when testing with the docs so #nfi
- getCurrentUser()
Returns a JSON formatted list of information for the current user
- changeUserPassword(passwd)
Changes the password of the current account
- getUsers()
Returns a JSON formatted list of users
- createUser(isAdmin, username, password)
Creates a new user with the specified details (isAdmin: boolean value)
- getUserByID(userID)
Returns a JSON formatted list of the selected user
- updateUserByID(userID, isAdmin, username, password) ## I really do not like this implementation will look into a better way
Updates the user information by ID, need to pass an entire user data object currently, working on a better way
- deleteUserByID(userID)
Deletes user by ID
- getVersion()
Returns the version of gotify running on the server

135
gotipy.py
View File

@ -29,10 +29,7 @@ class gotipy(object):
self.requrl = self.url + '/application' self.requrl = self.url + '/application'
req = requests.get(self.requrl, headers=self.headers) req = requests.get(self.requrl, headers=self.headers)
try: return req
return req.json()['error']
except:
return req.json()
def createApplication(self, name, description): def createApplication(self, name, description):
self.name = name self.name = name
@ -49,7 +46,7 @@ class gotipy(object):
def deleteApplication(self, appid): def deleteApplication(self, appid):
self.appid = appid self.appid = appid
self.requrl = self.url + '/application/' + self.appid self.requrl = self.url + '/application/' + str(self.appid)
req = requests.delete(self.requrl, headers=self.headers) req = requests.delete(self.requrl, headers=self.headers)
return req return req
@ -68,7 +65,43 @@ class gotipy(object):
req = requests.get(self.requrl, headers=self.headers) req = requests.get(self.requrl, headers=self.headers)
return req return req
def addClient(self, name):
self.name = name
self.requrl = self.url + '/client'
data = {"name": self.name}
pprint(data)
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
def deleteClient(self, clientid):
self.clientid = clientid
self.requrl = self.url + '/client/' + str(self.clientid)
req = requests.delete(self.requrl, headers=self.headers)
return req
## Message Functions ## Message Functions
def getMessagesByApp(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid) + "/message"
req = requests.get(self.requrl, headers=self.headers)
return req
def deleteAllMessagesFromApp(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid) + "/message"
req = requests.delete(self.requrl, headers=self.headers)
return req
def getAllMessages(self):
self.requrl = self.url + '/message'
req = requests.get(self.requrl, headers=self.headers)
return req
def sendMessage(self, title, message): def sendMessage(self, title, message):
self.title = title self.title = title
self.message = message self.message = message
@ -80,9 +113,93 @@ class gotipy(object):
data["title"] = self.title data["title"] = self.title
req = requests.post(self.requrl, headers=self.headers, json=data) req = requests.post(self.requrl, headers=self.headers, json=data)
try: return req
return req.json()['error']
except: def deleteAllMessages(self):
return "Message Sent :)" self.requrl = self.url + '/message'
req = requests.delete(self.requrl, headers=self.headers)
return req
def deleteMessageByID(self, msgid):
self.msgid = msgid
self.requrl = self.url + '/message/' + str(self.msgid)
req = requests.delete(self.requrl, headers=self.headers)
return req
def getStream(self): ## Currently broken, returns 400 error, no idea why
self.requrl = self.url + '/stream'
req = requests.get(self.requrl, headers=self.headers)
return req
## User Functions ## User Functions
def getCurrentUser(self):
self.requrl = self.url + '/current/user'
req = requests.get(self.requrl, headers=self.headers)
return req
def changeUserPassword(self, passwd):
self.passwd = str(passwd)
self.requrl = self.url + '/current/user/password'
data = {"pass": self.passwd}
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
def getUsers(self):
self.requrl = self.url + '/user'
req = requests.get(self.requrl, headers=self.headers)
return req
def createUser(self, isAdmin, username, password):
self.isAdmin = bool(isAdmin)
self.username = str(username)
self.password = str(password)
self.requrl = self.url + '/user'
data = {"admin": self.isAdmin,
"name": self.username,
"pass": self.password}
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
def getUserByID(self, userID):
self.userID = str(userID)
self.requrl = self.url + '/user/' + self.userID
req = requests.get(self.requrl, headers=self.headers)
return req
def updateUserByID(self, userID, isAdmin, username, password): ## I really do not like this implementation will look into a better way
self.userID = str(userID)
self.isAdmin = bool(isAdmin)
self.username = str(username)
self.password = str(password)
self.requrl = self.url + '/user/' + self.userID
data = {"admin": self.isAdmin,
"name": self.username,
"pass": self.password}
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
def deleteUserByID(self, userID):
self.userID = str(userID)
self.requrl = self.url + '/user/' + self.userID
req = requests.delete(self.requrl, headers=self.headers)
return req
## Get version information
def getVersion(self):
self.requrl = self.url + '/version'
req = requests.get(self.requrl)
return req