From d35c72bf0239949eea2d00b6ba241e848e7e5d2f Mon Sep 17 00:00:00 2001 From: BarsTiger Date: Tue, 1 Mar 2022 14:21:42 +0200 Subject: [PATCH] New handling system everywhere --- modules/auth.py | 3 +-- modules/change_email.py | 36 +++++----------------------- modules/change_password.py | 37 +++++----------------------- modules/package_edit.py | 49 ++++++-------------------------------- modules/search.py | 21 ++++------------ modules/source.py | 22 ++++++----------- modules/unerrored.py | 2 +- modules/uploader.py | 2 +- 8 files changed, 34 insertions(+), 138 deletions(-) diff --git a/modules/auth.py b/modules/auth.py index 004979f..c37f866 100644 --- a/modules/auth.py +++ b/modules/auth.py @@ -1,6 +1,5 @@ import json import sys - import modules.vars as horsy_vars import ctypes @@ -35,7 +34,7 @@ def get_auth(is_gui=False, login_ui=None, Ui_LoginWindow=None): login_ui.login_button.clicked.connect(load_login_now) -def get_auth_without_login(is_gui=False): +def get_auth_without_login(): with open(horsy_vars.horsypath + 'config.cfg') as f: config = json.load(f) diff --git a/modules/change_email.py b/modules/change_email.py index 3386f4c..4624e24 100644 --- a/modules/change_email.py +++ b/modules/change_email.py @@ -1,10 +1,10 @@ import requests import modules.gui as gui -from modules.auth import del_auth, get_auth +from modules.auth import get_auth import modules.vars as horsy_vars -import json import threading from PyQt5 import QtWidgets +from modules.http_status import handle def change(email): @@ -21,34 +21,10 @@ def change(email): try: def change_in_new_thread(): try: - r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', - json={'auth': auth, 'email': email}) - try: - r = r.json() - except: - if r.text == '': - gui.cpopup('Success', 'Success, your email has been changed') - with open(horsy_vars.horsypath + 'config.cfg') as f: - config = json.load(f) - config['auth'] = {'email': email, 'password': config['auth']['password']} - with open(horsy_vars.horsypath + 'config.cfg', 'w') as f: - json.dump(config, f) - try: - if r['message'] == 'Unauthorized': - gui.cpopup('Error', 'Invalid credentials \nDeleting auth from config') - del_auth() - - elif r['message'] == 'Internal server error': - gui.cpopup('Error', 'Internal server error') - return 'Internal server error' - - else: - print('Unknown error, please try again') - print('Server response:') - print(r.text) - return 'Unknown error, please try again, \n Server response: \n' + str(r.text) - except: - pass + r_code = handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', + json={'auth': auth, 'email': email}).status_code) + if r_code[1] not in [200, 201]: + gui.cpopup("Error", r_code[1]) except: gui.cpopup('Error', 'Unexpected error.') threading.Thread(target=change_in_new_thread).start() diff --git a/modules/change_password.py b/modules/change_password.py index 22fdb0b..75c5f87 100644 --- a/modules/change_password.py +++ b/modules/change_password.py @@ -1,8 +1,8 @@ import requests import modules.gui as gui -from modules.auth import del_auth, get_auth +from modules.auth import get_auth import modules.vars as horsy_vars -import json +from modules.http_status import handle from PyQt5 import QtWidgets @@ -26,34 +26,9 @@ def change(oldpass, newpass): return try: - r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', - json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()), - 'password': newpass}) - try: - r = r.json() - except: - if r.text == '': - gui.popup('Success', 'Success, your password has been changed') - with open(horsy_vars.horsypath + 'config.cfg') as f: - config = json.load(f) - config['auth'] = {'email': config['auth']['email'], 'password': newpass} - with open(horsy_vars.horsypath + 'config.cfg', 'w') as f: - json.dump(config, f) - try: - if r['message'] == 'Unauthorized': - gui.popup('Error', 'Invalid credentials \nDeleting auth from config') - del_auth() - - elif r['message'] == 'Internal server error': - gui.popup('Error', 'Internal server error') - return 'Internal server error' - - else: - print('Unknown error, please try again') - print('Server response:') - print(r) - return 'Unknown error, please try again, \n Server response: \n' + str(r) - except: - pass + gui.cpopup("Changing password", + handle(requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', + json={'auth': get_auth(True, login_ui, QtWidgets.QMainWindow()), + 'password': newpass}).status_code)[1]) except: gui.popup('Error', 'Unexpected error.') diff --git a/modules/package_edit.py b/modules/package_edit.py index 9e4fb32..b8ea83b 100644 --- a/modules/package_edit.py +++ b/modules/package_edit.py @@ -1,10 +1,9 @@ -import time from modules.auth import get_auth from horsygui import login_ui, UiLoginWindow as Ui_LoginWindow import modules.gui as gui import requests import modules.vars as horsy_vars -import modules.http_status as status +from modules.http_status import handle import json @@ -12,33 +11,13 @@ def edit(package, UiPackageWindow): package_ui = gui.Ui_PackageWindow() package_ui.setupUi(UiPackageWindow) - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text - if r == "": - gui.cpopup("Installation", f"Package {package} not found") - return f"Package {package} not found" + r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") try: + r_code = handle(r.status_code) + if r_code[1] not in [200, 201]: + gui.cpopup("Error", r_code[1]) + r = r.text r = json.loads(r) - except: - print("[red]Error with unsupported message[/]") - gui.cpopup("Error", "Error with unsupported message") - return "Error with unsupported message" - try: - if r["message"] == "Internal server error": - print("Internal server error") - gui.cpopup("Error", "Internal server error") - return "Internal server error" - if r["message"] == "Too many requests": - try: - if r['message'] == 'Too many requests': - while r['message'] == 'Too many requests': - time.sleep(0.5) - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text - try: - r = json.loads(r) - except: - pass - except: - pass except: pass @@ -64,20 +43,6 @@ def edit(package, UiPackageWindow): 'run': (lambda x: x if x != '' else None)(package_ui.main_exe_box.text()) } - r = requests.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages", json=body) - if r.status_code == status.OK: - gui.cpopup('Success', 'Success, package edited') - elif r.status_code == status.UNAUTHORIZED: - gui.cpopup('Error', 'Unauthorized') - elif r.status_code == status.NOT_FOUND: - gui.cpopup('Error', 'Package not found or its not yours') - elif r.status_code == status.BAD_REQUEST: - gui.cpopup('Error', 'Bad request') - elif r.status_code == status.INTERNAL_SERVER_ERROR: - gui.cpopup('Error', 'Internal server error') - elif r.status_code == status.TOO_MANY_REQUESTS: - gui.cpopup('Error', 'Too many requests') - else: - gui.cpopup('Error', 'Unknown error') + handle(requests.put(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages", json=body).status_code) package_ui.update_button.clicked.connect(send) diff --git a/modules/search.py b/modules/search.py index 4a0aa5a..b15f4fb 100644 --- a/modules/search.py +++ b/modules/search.py @@ -5,6 +5,7 @@ import requests import modules.vars as horsy_vars import json from rich import print +from modules.http_status import handle client = SearchClient.create( requests.get('https://raw.githubusercontent.com/horsy-ml/horsy/master/web_vars/search_app').json()['APP_ID'], @@ -26,21 +27,10 @@ def search(query, is_gui=False): def info(package, download_ui=None, UiDownloadWindow=None): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text - if r == "": - print(f"[red]Package {package} not found[/]") - return f"Package {package} not found" - try: - r = json.loads(r) - except: - print("[red]Error with unsupported message[/]") - return "Error with unsupported message" - try: - if r["message"] == "Internal server error": - print("[red]Internal server error[/]") - return "Internal server error" - except: - pass + r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r_code = handle(r.status_code) + r = r.text + r = json.loads(r) print(f"[bold]{r['name']}{'✅' if r['verified'] else ''} by {r['authorName']}[/]") print(f"{r['description']}") @@ -71,4 +61,3 @@ def info(package, download_ui=None, UiDownloadWindow=None): "We don't call you to trust this app, use it at your own risk \n" "but we recommend you more to install verified packages") return None - diff --git a/modules/source.py b/modules/source.py index 991061c..ef49a8a 100644 --- a/modules/source.py +++ b/modules/source.py @@ -3,24 +3,16 @@ import json import webbrowser import modules.vars as horsy_vars from rich import print +from modules.http_status import handle def get_source(package): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text - try: - r = json.loads(r) - except: - print("[red]Error with unsupported message[/]") - return "Error with unsupported message" - try: - if r["message"] == "not found": - print("[red]Package not found[/]") - return "Package not found" - if r["message"] == "Internal server error": - print("[red]Internal server error[/]") - return "Internal server error" - except: - pass + r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r_code = handle(r.status_code) + if r_code[1] not in [200, 201]: + return r_code[1] + r = r.text + r = json.loads(r) try: webbrowser.open(r["sourceUrl"]) diff --git a/modules/unerrored.py b/modules/unerrored.py index 79cd0f3..278e11f 100644 --- a/modules/unerrored.py +++ b/modules/unerrored.py @@ -1,7 +1,7 @@ import threading -import subprocess import time + def run_threaded(func, *args): try: threading.Thread(target=func, args=args).start() diff --git a/modules/uploader.py b/modules/uploader.py index 45b2ba0..93a92db 100644 --- a/modules/uploader.py +++ b/modules/uploader.py @@ -95,7 +95,7 @@ def upload(is_gui=False, ui=None): } else: - auth = get_auth_without_login(is_gui) + auth = get_auth_without_login() project_name = ui.packagename_box.text() if not matches(project_name) or len(project_name) > 64 or len(project_name) < 3: