Merge branch 'refactor' into 'master'

Refactor

See merge request benjamyn/gotipy!1
This commit is contained in:
Benjamyn Love 2018-11-11 20:32:09 +11:00
commit 261837112f
2 changed files with 348 additions and 218 deletions

228
README.md
View File

@ -6,73 +6,171 @@ Initialize the wrapper with the token you want to use
i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD")
<h3>Functions Implimented</h3> <b/>
<h3><b>applications class</b></h3>
<table style="width:100%">
<tr>
<th>Function</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>getApplications</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>createApplication</td>
<td>Name, Description</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>deleteApplication</td>
<td>Application ID</td>
<td>HTTP resopnse code</td>
</tr>
<tr>
<td>updateApplicationImage</td>
<td>Application ID, Image Data</td>
<td>HTTP response code</td>
</tr>
</table>
- basicAuth(user, passwd)
Sets up basic Auth for inclusion in the header
<h3><b>clients class</b></h3>
<table style="width:100%">
<tr>
<th>Function</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>getClients</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>createClient</td>
<td>Name</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>deleteClient</td>
<td>Client ID</td>
<td>HTTP response code</td>
</tr>
</table>
- getApplications()
Returns a JSON formatted list of applications
<h3><b>msgManager class</b></h3>
<table style="width:100%">
<tr>
<th>Function</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>getMessagesByApp</td>
<td>Application ID</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>deleteAllMessagesFromApp</td>
<td>Application ID</td>
<td>HTTP response code</td>
</tr>
<tr>
<td>getAllMesages</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>deleteAllMessages</td>
<td>N/A</td>
<td>HTTP response code</td>
</tr>
<tr>
<td>deleteMessagesByID</td>
<td>Message ID</td>
<td>HTTP response code</td>
</tr>
<tr>
<td>getStream</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
</table>
- createApplication(name, description)
Creates an application with defined name and description
<h3><b>messages class</b></h3>
<table style="width:100%">
<tr>
<th>Function</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>sendMessage</td>
<td>Title, Message</td>
<td>JSON formatted list</td>
</tr>
</table>
- deleteApplication(appid)
Delete Application via appid
<h3><b>users class</b></h3>
<table style="width:100%">
<tr>
<th>Function</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>getCurrentUser()</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>changeUserPassword</td>
<td>Password</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>getUsers</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>createUser</td>
<td>Admin, Username, Password</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>getUserByID</td>
<td>User ID</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>updateUserByID</td>
<td>User ID, Admin, Username, Password</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>deleteUserByID</td>
<td>User ID</td>
<td>HTTP response code</td>
</tr>
</table>
- 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
<h3><b>utils class</b></h3>
<table style="width:100%">
<tr>
<th>Function</th>
<th>Arguments</th>
<th>Return</th>
</tr>
<tr>
<td>getVersion</td>
<td>N/A</td>
<td>JSON formatted list</td>
</tr>
<tr>
<td>basicAuth</td>
<td>Username, password</td>
<td>JSON formatted list</td>
</tr>
</table>

338
gotipy.py
View File

@ -3,203 +3,235 @@ import json
import base64
from pprint import pprint
proxies = {
'http': "localhost:8080"
}
class gotipy(object):
## Application Functions
class applications(object):
def __init__(self, token, url, proxies = {}):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
self.proxies = proxies
class gotipy(object):
def __init__(self, token, url):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
def getApplications(self):
self.requrl = self.url + '/application'
req = requests.get(self.requrl, headers=self.headers, proxies=self.proxies)
return req
def createApplication(self, name, description):
self.name = name
self.description = description
self.requrl = self.url + '/application'
data = {}
data["name"] = self.name
data["description"] = self.description
req = requests.post(self.requrl, headers=self.headers, json=data)
pprint(req)
return req
def deleteApplication(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid)
req = requests.delete(self.requrl, headers=self.headers)
return req
def updateApplicationImage(self, appid, imgData):
self.id = appid
self.imgData = { 'file': imgData}
self.requrl = self.url + '/application/' + self.id + '/image'
req = requests.post(self.requrl, headers=self.headers, files=self.imgData)
return req
## Auth Function
def basicAuth(self, user, passwd):
self.user = user
self.passwd = passwd
self.string = self.user + ':' + self.passwd
self.encoded = base64.b64encode(self.string.encode("utf-8"))
print(self.string.encode('utf-8'))
self.headers["authorization"] = "Basic " + base64.b64encode(self.string)
print(self.encoded)
return self.headers
## Token Functions
def getApplications(self):
self.requrl = self.url + '/application'
req = requests.get(self.requrl, headers=self.headers)
return req
def createApplication(self, name, description):
self.name = name
self.description = description
self.requrl = self.url + '/application'
## Client Functions
class clients(object):
def __init__(self, token, url, proxies = {}):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
self.proxies = proxies
data = {}
data["name"] = self.name
data["description"] = self.description
def getClients(self):
self.requrl = self.url + '/client'
req = requests.post(self.requrl, headers=self.headers, json=data)
pprint(req)
return req
req = requests.get(self.requrl, headers=self.headers)
return req
def deleteApplication(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid)
def createClient(self, name):
self.name = name
self.requrl = self.url + '/client'
data = {"name": self.name}
pprint(data)
req = requests.delete(self.requrl, headers=self.headers)
return req
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
def updateApplicationImage(self, appid, imgData):
self.id = appid
self.imgData = { 'file': imgData}
self.requrl = self.url + '/application/' + self.id + '/image'
self.clientid = clientid
self.requrl = self.url + '/client/' + str(self.clientid)
req = requests.post(self.requrl, headers=self.headers, files=self.imgData)
return req
def getClients(self):
self.requrl = self.url + '/client'
req = requests.get(self.requrl, headers=self.headers)
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
req = requests.delete(self.requrl, headers=self.headers)
return req
## Message Functions
def getMessagesByApp(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid) + "/message"
class msgManager(object):
def __init__(self, token, url, proxies = {}):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
self.proxies = proxies
req = requests.get(self.requrl, headers=self.headers)
return req
def getMessagesByApp(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid) + "/message"
def deleteAllMessagesFromApp(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid) + "/message"
req = requests.get(self.requrl, headers=self.headers)
return req
req = requests.delete(self.requrl, headers=self.headers)
return req
def deleteAllMessagesFromApp(self, appid):
self.appid = appid
self.requrl = self.url + '/application/' + str(self.appid) + "/message"
def getAllMessages(self):
self.requrl = self.url + '/message'
req = requests.delete(self.requrl, headers=self.headers)
return req
req = requests.get(self.requrl, headers=self.headers)
return req
def getAllMessages(self):
self.requrl = self.url + '/message'
def sendMessage(self, title, message):
self.title = title
self.message = message
self.requrl = self.url + '/message'
req = requests.get(self.requrl, headers=self.headers)
return req
#define a new dict for the message
data = {}
data["message"] = self.message
data["title"] = self.title
def deleteAllMessages(self):
self.requrl = self.url + '/message'
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
req = requests.delete(self.requrl, headers=self.headers)
return req
def deleteAllMessages(self):
self.requrl = self.url + '/message'
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
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)
def getStream(self): ## Currently broken, returns 400 error, no idea why
self.requrl = self.url + '/stream'
req = requests.delete(self.requrl, headers=self.headers)
return req
req = requests.get(self.requrl, headers=self.headers)
return req
def getStream(self): ## Currently broken, returns 400 error, no idea why
self.requrl = self.url + '/stream'
class messages(object):
def __init__(self, token, url, proxies = {}):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
self.proxies = proxies
req = requests.get(self.requrl, headers=self.headers)
return req
def sendMessage(self, title, message):
self.title = title
self.message = message
self.requrl = self.url + '/message'
#define a new dict for the message
data = {}
data["message"] = self.message
data["title"] = self.title
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
## User Functions
class users(object):
def __init__(self, token, url, proxies = {}):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
self.proxies = proxies
def getCurrentUser(self):
self.requrl = self.url + '/current/user'
req = requests.get(self.requrl, headers=self.headers)
return req
def getCurrentUser(self):
self.requrl = self.url + '/current/user'
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
req = requests.get(self.requrl, headers=self.headers)
return req
def getUsers(self):
self.requrl = self.url + '/user'
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
req = requests.get(self.requrl, headers=self.headers)
return req
def getUsers(self):
self.requrl = self.url + '/user'
def createUser(self, isAdmin, username, password):
self.isAdmin = bool(isAdmin)
self.username = str(username)
self.password = str(password)
self.requrl = self.url + '/user'
req = requests.get(self.requrl, headers=self.headers)
return req
data = {"admin": self.isAdmin,
"name": self.username,
"pass": self.password}
def createUser(self, isAdmin, username, password):
self.isAdmin = bool(isAdmin)
self.username = str(username)
self.password = str(password)
self.requrl = self.url + '/user'
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
data = {"admin": self.isAdmin,
"name": self.username,
"pass": self.password}
def getUserByID(self, userID):
self.userID = str(userID)
self.requrl = self.url + '/user/' + self.userID
req = requests.post(self.requrl, headers=self.headers, json=data)
return req
req = requests.get(self.requrl, headers=self.headers)
return req
def getUserByID(self, userID):
self.userID = str(userID)
self.requrl = self.url + '/user/' + self.userID
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
req = requests.get(self.requrl, headers=self.headers)
return req
data = {"admin": self.isAdmin,
"name": self.username,
"pass": self.password}
req = requests.post(self.requrl, headers=self.headers, json=data)
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
def deleteUserByID(self, userID):
self.userID = str(userID)
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
req = requests.delete(self.requrl, headers=self.headers)
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
class utils(object):
def __init__(self, token, url, proxies = {}):
self.token = token
self.url = url
self.headers = {'X-Gotify-Key' : self.token}
self.proxies = proxies
def getVersion(self):
self.requrl = self.url + '/version'
def getVersion(self):
self.requrl = self.url + '/version'
req = requests.get(self.requrl)
return req
req = requests.get(self.requrl)
return req
def basicAuth(self, user, passwd):
self.user = user
self.passwd = passwd
self.string = self.user + ':' + self.passwd
self.encoded = base64.b64encode(self.string.encode("utf-8"))
print(self.string.encode('utf-8'))
self.headers["authorization"] = "Basic " + base64.b64encode(self.string)
print(self.encoded)
return self.headers