From 3aaa21388a2d97f81f50f27a130ca9936b2b0552 Mon Sep 17 00:00:00 2001 From: benjamyn Date: Sun, 11 Nov 2018 19:29:22 +1100 Subject: [PATCH 1/5] testing branch --- testfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 testfile diff --git a/testfile b/testfile new file mode 100644 index 0000000..e69de29 From d0705886a1178aab8824f658587e3fb6e0948fcf Mon Sep 17 00:00:00 2001 From: benjamyn Date: Sun, 11 Nov 2018 19:59:42 +1100 Subject: [PATCH 2/5] Started refactoring --- gotipy.py | 339 ++++++++++++++++++++++++++++++------------------------ testfile | 0 2 files changed, 186 insertions(+), 153 deletions(-) delete mode 100644 testfile diff --git a/gotipy.py b/gotipy.py index 8f1e085..9fb37eb 100644 --- a/gotipy.py +++ b/gotipy.py @@ -3,203 +3,236 @@ 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 addClient(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' + def deleteClient(self, clientid): + 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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/testfile b/testfile deleted file mode 100644 index e69de29..0000000 From cb7e798d50c866d9d9b2a6f2cfe582a78b0d36e0 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 11 Nov 2018 20:09:19 +1100 Subject: [PATCH 3/5] Update README.md --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 6044e3d..34321ff 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,35 @@ Initialize the wrapper with the token you want to use i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD") +applications class + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionArgumentsReturn
getApplicationsN/AJSON formatted list
createApplicationname, descriptionJSON formatted list
deleteApplicationApplication IDHTTP resopnse code
updateApplicationImageApplication ID, Image DataHTTP response code
+

Functions Implimented

- basicAuth(user, passwd) From 726446cff971a62168c8b79a9adb581df4d0e097 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 11 Nov 2018 20:11:44 +1100 Subject: [PATCH 4/5] Update README.md From 01fdbc4d31e862d0da92f783a08c75c0d108c3c5 Mon Sep 17 00:00:00 2001 From: benjamyn Date: Sun, 11 Nov 2018 20:29:31 +1100 Subject: [PATCH 5/5] Updated README.md with a new layout Fixed name of function to better fit the standard (addClient -> createClient) --- README.md | 205 ++++++++++++++++++++++++++++++++++++------------------ gotipy.py | 5 +- 2 files changed, 139 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 34321ff..badd8f5 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Initialize the wrapper with the token you want to use i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD") -applications class +

applications class

@@ -20,7 +20,7 @@ i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD") - + @@ -35,73 +35,142 @@ i.e: gotipy_usr = gotipy.gotipy("user token", "https://URL.TLD")
Function
createApplicationname, descriptionName, Description JSON formatted list
-

Functions Implimented

+

clients class

+ + + + + + + + + + + + + + + + + + + + + +
FunctionArgumentsReturn
getClientsN/AJSON formatted list
createClientNameJSON formatted list
deleteClientClient IDHTTP response code
-- basicAuth(user, passwd) -Sets up basic Auth for inclusion in the header +

msgManager class

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionArgumentsReturn
getMessagesByAppApplication IDJSON formatted list
deleteAllMessagesFromAppApplication IDHTTP response code
getAllMesagesN/AJSON formatted list
deleteAllMessagesN/AHTTP response code
deleteMessagesByIDMessage IDHTTP response code
getStreamN/AJSON formatted list
-- getApplications() -Returns a JSON formatted list of applications +

messages class

+ + + + + + + + + + + +
FunctionArgumentsReturn
sendMessageTitle, MessageJSON formatted list
-- createApplication(name, description) -Creates an application with defined name and description +

users class

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionArgumentsReturn
getCurrentUser()N/AJSON formatted list
changeUserPasswordPasswordJSON formatted list
getUsersN/AJSON formatted list
createUserAdmin, Username, PasswordJSON formatted list
getUserByIDUser IDJSON formatted list
updateUserByIDUser ID, Admin, Username, PasswordJSON formatted list
deleteUserByIDUser IDHTTP response code
-- 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 \ No newline at end of file +

utils class

+ + + + + + + + + + + + + + + + +
FunctionArgumentsReturn
getVersionN/AJSON formatted list
basicAuthUsername, passwordJSON formatted list
\ No newline at end of file diff --git a/gotipy.py b/gotipy.py index 9fb37eb..a610367 100644 --- a/gotipy.py +++ b/gotipy.py @@ -62,7 +62,7 @@ class gotipy(object): req = requests.get(self.requrl, headers=self.headers) return req - def addClient(self, name): + def createClient(self, name): self.name = name self.requrl = self.url + '/client' data = {"name": self.name} @@ -71,7 +71,6 @@ class gotipy(object): 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) @@ -235,4 +234,4 @@ class gotipy(object): print(self.string.encode('utf-8')) self.headers["authorization"] = "Basic " + base64.b64encode(self.string) print(self.encoded) - return self.headers \ No newline at end of file + return self.headers