Add basic API functions and working account.auth method

This commit is contained in:
2022-09-17 12:01:14 +03:00
parent 59ec0647dc
commit d51ffa0eb2
9 changed files with 262 additions and 9 deletions

41
api/api_errors.py Normal file
View File

@@ -0,0 +1,41 @@
import traceback
# как создавать ошибку
# raise Exception(API_ERROR_XXX, <related_obj>)
API_ERROR_INTERNAL_ERROR = (100, 'internal error')
API_ERROR_METHOD_NOT_FOUND = (200, 'method not found')
API_ERROR_MISSING_ARGUMENT = (201, 'missing argument')
API_ERROR_UNKNOWN_ARGUMENT = (202, 'unknown argument')
API_ERROR_INVALID_ARGUMENT_TYPE = (203, 'invalid argument type')
API_ERROR_TOKEN_CREATION = (500, 'token creation error')
API_ERROR_INVALID_LOGIN = (501, 'invalid login')
API_ERROR_INVALID_PASSWORD = (502, 'invalid password')
API_ERROR_INVALID_TOKEN = (503, 'invalid token')
def make_error_object(ex: Exception):
try:
data = {
"error": {
"code": ex.args[0][0],
"message": ex.args[0][1]
}
}
if len(ex.args) >= 2:
data["error"]["related"] = ex.args[1]
return data
except Exception:
traceback.print_exc()
return {
"error": {
"code": API_ERROR_INTERNAL_ERROR[0],
"message": API_ERROR_INTERNAL_ERROR[1]
}
}