Final refactor

Normal http code handling (first stage)
This commit is contained in:
BarsTiger
2022-02-10 22:27:09 +02:00
parent 7bb411871e
commit 085dacae2a
2 changed files with 110 additions and 65 deletions

View File

@@ -1,48 +1,102 @@
CONTINUE = 100 codes = {'CONTINUE': 100,
SWITCHING_PROTOCOLS = 101 'SWITCHING_PROTOCOLS': 101,
PROCESSING = 102 'PROCESSING': 102,
EARLYHINTS = 103 'EARLYHINTS': 103,
OK = 200 'OK': 200,
CREATED = 201 'CREATED': 201,
ACCEPTED = 202 'ACCEPTED': 202,
NON_AUTHORITATIVE_INFORMATION = 203 'NON_AUTHORITATIVE_INFORMATION': 203,
NO_CONTENT = 204 'NO_CONTENT': 204,
RESET_CONTENT = 205 'RESET_CONTENT': 205,
PARTIAL_CONTENT = 206 'PARTIAL_CONTENT': 206,
AMBIGUOUS = 300 'AMBIGUOUS': 300,
MOVED_PERMANENTLY = 301 'MOVED_PERMANENTLY': 301,
FOUND = 302 'FOUND': 302,
SEE_OTHER = 303 'SEE_OTHER': 303,
NOT_MODIFIED = 304 'NOT_MODIFIED': 304,
TEMPORARY_REDIRECT = 307 'TEMPORARY_REDIRECT': 307,
PERMANENT_REDIRECT = 308 'PERMANENT_REDIRECT': 308,
BAD_REQUEST = 400 'BAD_REQUEST': 400,
UNAUTHORIZED = 401 'UNAUTHORIZED': 401,
PAYMENT_REQUIRED = 402 'PAYMENT_REQUIRED': 402,
FORBIDDEN = 403 'FORBIDDEN': 403,
NOT_FOUND = 404 'NOT_FOUND': 404,
METHOD_NOT_ALLOWED = 405 'METHOD_NOT_ALLOWED': 405,
NOT_ACCEPTABLE = 406 'NOT_ACCEPTABLE': 406,
PROXY_AUTHENTICATION_REQUIRED = 407 'PROXY_AUTHENTICATION_REQUIRED': 407,
REQUEST_TIMEOUT = 408 'REQUEST_TIMEOUT': 408,
CONFLICT = 409 'CONFLICT': 409,
GONE = 410 'GONE': 410,
LENGTH_REQUIRED = 411 'LENGTH_REQUIRED': 411,
PRECONDITION_FAILED = 412 'PRECONDITION_FAILED': 412,
PAYLOAD_TOO_LARGE = 413 'PAYLOAD_TOO_LARGE': 413,
URI_TOO_LONG = 414 'URI_TOO_LONG': 414,
UNSUPPORTED_MEDIA_TYPE = 415 'UNSUPPORTED_MEDIA_TYPE': 415,
REQUESTED_RANGE_NOT_SATISFIABLE = 416 'REQUESTED_RANGE_NOT_SATISFIABLE': 416,
EXPECTATION_FAILED = 417 'EXPECTATION_FAILED': 417,
I_AM_A_TEAPOT = 418 'I_AM_A_TEAPOT': 418,
MISDIRECTED = 421 'MISDIRECTED': 421,
UNPROCESSABLE_ENTITY = 422 'UNPROCESSABLE_ENTITY': 422,
FAILED_DEPENDENCY = 424 'FAILED_DEPENDENCY': 424,
PRECONDITION_REQUIRED = 428 'PRECONDITION_REQUIRED': 428,
TOO_MANY_REQUESTS = 429 'TOO_MANY_REQUESTS': 429,
INTERNAL_SERVER_ERROR = 500 'INTERNAL_SERVER_ERROR': 500,
NOT_IMPLEMENTED = 501 'NOT_IMPLEMENTED': 501,
BAD_GATEWAY = 502 'BAD_GATEWAY': 502,
SERVICE_UNAVAILABLE = 503 'SERVICE_UNAVAILABLE': 503,
GATEWAY_TIMEOUT = 504 'GATEWAY_TIMEOUT': 504,
HTTP_VERSION_NOT_SUPPORTED = 505 'HTTP_VERSION_NOT_SUPPORTED': 505,
100: 'CONTINUE',
101: 'SWITCHING_PROTOCOLS',
102: 'PROCESSING',
103: 'EARLYHINTS',
200: 'OK',
201: 'CREATED',
202: 'ACCEPTED',
203: 'NON_AUTHORITATIVE_INFORMATION',
204: 'NO_CONTENT',
205: 'RESET_CONTENT',
206: 'PARTIAL_CONTENT',
300: 'AMBIGUOUS',
301: 'MOVED_PERMANENTLY',
302: 'FOUND',
303: 'SEE_OTHER',
304: 'NOT_MODIFIED',
307: 'TEMPORARY_REDIRECT',
308: 'PERMANENT_REDIRECT',
400: 'Bad request, it could be invalid form data',
401: 'You are not authorized to access this resource, please login',
402: 'PAYMENT_REQUIRED',
403: 'Forbidden, you do not have permission to access this resource or you need to log in',
404: 'Not found',
405: 'METHOD_NOT_ALLOWED',
406: 'NOT_ACCEPTABLE',
407: 'PROXY_AUTHENTICATION_REQUIRED',
408: 'REQUEST_TIMEOUT',
409: 'CONFLICT',
410: 'GONE',
411: 'LENGTH_REQUIRED',
412: 'PRECONDITION_FAILED',
413: 'PAYLOAD_TOO_LARGE',
414: 'URI_TOO_LONG',
415: 'UNSUPPORTED_MEDIA_TYPE',
416: 'REQUESTED_RANGE_NOT_SATISFIABLE',
417: 'EXPECTATION_FAILED',
418: 'I_AM_A_TEAPOT',
421: 'MISDIRECTED',
422: 'UNPROCESSABLE_ENTITY',
424: 'FAILED_DEPENDENCY',
428: 'PRECONDITION_REQUIRED',
429: 'Too many requests, you have been rate limited. Slow down!',
500: 'Internal Server Error, something went wrong on server side',
501: 'NOT_IMPLEMENTED',
502: 'BAD_GATEWAY',
503: 'Server is temporarily unavailable',
504: 'GATEWAY_TIMEOUT',
505: 'HTTP_VERSION_NOT_SUPPORTED'
}
def handle(code):
print(codes[code])
return [codes[code], code]

View File

@@ -6,6 +6,7 @@ from modules.auth import get_auth, del_auth
import re import re
import modules.vars as horsy_vars import modules.vars as horsy_vars
import os import os
from modules.http_status import handle, codes as status_codes
def matches(s): def matches(s):
@@ -139,10 +140,12 @@ def upload(is_gui=False, ui=None, login_ui=None, Ui_LoginWindow=None):
r = None r = None
while r is None: while r is None:
try: try:
r = requests.post(horsy_vars.protocol + horsy_vars.server_url + '/packages/new', json=request).text r = requests.post(horsy_vars.protocol + horsy_vars.server_url + '/packages/new', json=request)
r_code = handle(r.status_code)
r = r.text
r = json.loads(r) r = json.loads(r)
if r['message'] == 'Unauthorized': if r_code[1] in [403, 401]:
print('[red]Invalid credentials[/red]') print('[red]Invalid credentials[/red]')
print('Deleting auth from config') print('Deleting auth from config')
del_auth() del_auth()
@@ -150,28 +153,16 @@ def upload(is_gui=False, ui=None, login_ui=None, Ui_LoginWindow=None):
print(r) print(r)
r = None r = None
elif r['message'] == 'Internal server error': elif r_code == 200:
print('[red]Internal server error[/red]')
return 'Internal server error'
elif r['message'] == 'Success':
print('[green]Success, your project is created. You can install it by running[/] ' print('[green]Success, your project is created. You can install it by running[/] '
'[i]horsy i {0}[/]'.format(request['name'])) '[i]horsy i {0}[/]'.format(request['name']))
return 'Success, your project is created. You can install it by running horsy i {0}'.format( return 'Success, your project is created. You can install it by running horsy i {0}'.format(
request['name']) request['name'])
elif 'already exists' in r['message']: return r_code[0]
print(f"[red]{r['message']}[/red]")
return {r['message']}
else:
print('[red]Unknown error, please try again[/red]')
print('Server response:')
print(r)
return 'Unknown error, please try again, \n Server response: \n' + str(r)
except: except:
with open(f'error_{time.time()}.txt', 'w') as f: with open(f'error_{time.time()}.txt', 'w') as f:
f.write(str(r)) f.write(str(r.text))
print(f'[red]Something went wrong with unsupported error. You can check servers response in ' print(f'[red]Something went wrong with unsupported error. You can check servers response in '
f'{os.getcwd()}/{f.name}[/red]') f'{os.getcwd()}/{f.name}[/red]')
break break