API & Client - update API error messages to remove i18n workarounds
This commit is contained in:
@ -52,7 +52,7 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
|
||||
{
|
||||
"auth_token": "JSON Web Token",
|
||||
"message": "Successfully registered.",
|
||||
"message": "successfully registered",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Errors: Valid email must be provided.\\n",
|
||||
"message": "Errors: email: valid email must be provided\\n",
|
||||
"status": "error"
|
||||
}
|
||||
|
||||
@ -73,23 +73,23 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
:<json string password: password (8 characters required)
|
||||
:<json string password_conf: password confirmation
|
||||
|
||||
:statuscode 201: Successfully registered.
|
||||
:statuscode 201: successfully registered
|
||||
:statuscode 400:
|
||||
- Invalid payload.
|
||||
- Sorry. That user already exists.
|
||||
- invalid payload
|
||||
- sorry, that user already exists
|
||||
- Errors:
|
||||
- 3 to 12 characters required for usernanme.
|
||||
- Valid email must be provided.
|
||||
- Password and password confirmation don't match.
|
||||
- 8 characters required for password.
|
||||
- username: 3 to 12 characters required
|
||||
- email: valid email must be provided
|
||||
- password: password and password confirmation don't match
|
||||
- password: 8 characters required
|
||||
:statuscode 403:
|
||||
Error. Registration is disabled.
|
||||
error, registration is disabled
|
||||
:statuscode 500:
|
||||
Error. Please try again or contact the administrator.
|
||||
error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
if not current_app.config.get('is_registration_enabled'):
|
||||
return ForbiddenErrorResponse('Error. Registration is disabled.')
|
||||
return ForbiddenErrorResponse('error, registration is disabled')
|
||||
|
||||
# get post data
|
||||
post_data = request.get_json()
|
||||
@ -121,7 +121,7 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
).first()
|
||||
if user:
|
||||
return InvalidPayloadErrorResponse(
|
||||
'Sorry. That user already exists.'
|
||||
'sorry, that user already exists'
|
||||
)
|
||||
|
||||
# add new user to db
|
||||
@ -133,7 +133,7 @@ def register_user() -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
auth_token = new_user.encode_auth_token(new_user.id)
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'Successfully registered.',
|
||||
'message': 'successfully registered',
|
||||
'auth_token': auth_token,
|
||||
}, 201
|
||||
# handler errors
|
||||
@ -164,7 +164,7 @@ def login_user() -> Union[Dict, HttpResponse]:
|
||||
|
||||
{
|
||||
"auth_token": "JSON Web Token",
|
||||
"message": "Successfully logged in.",
|
||||
"message": "successfully logged in",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -176,17 +176,17 @@ def login_user() -> Union[Dict, HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Invalid credentials.",
|
||||
"message": "invalid credentials",
|
||||
"status": "error"
|
||||
}
|
||||
|
||||
:<json string email: user email
|
||||
:<json string password_conf: password confirmation
|
||||
|
||||
:statuscode 200: Successfully logged in.
|
||||
:statuscode 400: Invalid payload.
|
||||
:statuscode 401: Invalid credentials.
|
||||
:statuscode 500: Error. Please try again or contact the administrator.
|
||||
:statuscode 200: successfully logged in
|
||||
:statuscode 400: invalid payload
|
||||
:statuscode 401: invalid credentials
|
||||
:statuscode 500: error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
# get post data
|
||||
@ -203,10 +203,10 @@ def login_user() -> Union[Dict, HttpResponse]:
|
||||
auth_token = user.encode_auth_token(user.id)
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'Successfully logged in.',
|
||||
'message': 'successfully logged in',
|
||||
'auth_token': auth_token,
|
||||
}
|
||||
return UnauthorizedErrorResponse('Invalid credentials.')
|
||||
return UnauthorizedErrorResponse('invalid credentials')
|
||||
# handler errors
|
||||
except (exc.IntegrityError, exc.OperationalError, ValueError) as e:
|
||||
return handle_error_and_return_response(e, db=db)
|
||||
@ -235,7 +235,7 @@ def logout_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Successfully logged out.",
|
||||
"message": "successfully logged out",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -247,20 +247,20 @@ def logout_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Provide a valid auth token.",
|
||||
"message": "provide a valid auth token",
|
||||
"status": "error"
|
||||
}
|
||||
|
||||
:reqheader Authorization: OAuth 2.0 Bearer Token
|
||||
|
||||
:statuscode 200: Successfully logged out.
|
||||
:statuscode 401: Provide a valid auth token.
|
||||
:statuscode 200: successfully logged out
|
||||
:statuscode 401: provide a valid auth token
|
||||
|
||||
"""
|
||||
# get auth token
|
||||
auth_header = request.headers.get('Authorization')
|
||||
if not auth_header:
|
||||
return UnauthorizedErrorResponse('Provide a valid auth token.')
|
||||
return UnauthorizedErrorResponse('provide a valid auth token')
|
||||
|
||||
auth_token = auth_header.split(' ')[1]
|
||||
resp = User.decode_auth_token(auth_token)
|
||||
@ -269,7 +269,7 @@ def logout_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'Successfully logged out.',
|
||||
'message': 'successfully logged out',
|
||||
}
|
||||
|
||||
|
||||
@ -365,9 +365,9 @@ def get_authenticated_user_profile(
|
||||
|
||||
:statuscode 200: success.
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
|
||||
"""
|
||||
user = User.query.filter_by(id=auth_user_id).first()
|
||||
@ -457,7 +457,7 @@ def edit_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
"username": "sam"
|
||||
"weekm": true,
|
||||
},
|
||||
"message": "User profile updated.",
|
||||
"message": "user profile updated",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -471,15 +471,15 @@ def edit_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
|
||||
:reqheader Authorization: OAuth 2.0 Bearer Token
|
||||
|
||||
:statuscode 200: User profile updated.
|
||||
:statuscode 200: user profile updated
|
||||
:statuscode 400:
|
||||
- Invalid payload.
|
||||
- Password and password confirmation don't match.
|
||||
- invalid payload
|
||||
- password: password and password confirmation don't match
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
:statuscode 500: Error. Please try again or contact the administrator.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 500: error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
# get post data
|
||||
@ -527,7 +527,7 @@ def edit_user(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'User profile updated.',
|
||||
'message': 'user profile updated',
|
||||
'data': user.serialize(),
|
||||
}
|
||||
|
||||
@ -619,7 +619,7 @@ def edit_user_preferences(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
"username": "sam"
|
||||
"weekm": true,
|
||||
},
|
||||
"message": "User preferences updated.",
|
||||
"message": "user preferences updated",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -629,15 +629,15 @@ def edit_user_preferences(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
|
||||
:reqheader Authorization: OAuth 2.0 Bearer Token
|
||||
|
||||
:statuscode 200: User profile updated.
|
||||
:statuscode 200: user preferences updated
|
||||
:statuscode 400:
|
||||
- Invalid payload.
|
||||
- Password and password confirmation don't match.
|
||||
- invalid payload
|
||||
- password: password and password confirmation don't match
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
:statuscode 500: Error. Please try again or contact the administrator.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 500: error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
# get post data
|
||||
@ -663,7 +663,7 @@ def edit_user_preferences(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'User preferences updated.',
|
||||
'message': 'user preferences updated',
|
||||
'data': user.serialize(),
|
||||
}
|
||||
|
||||
@ -693,7 +693,7 @@ def edit_picture(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "User picture updated.",
|
||||
"message": "user picture updated",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -701,18 +701,18 @@ def edit_picture(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
|
||||
:reqheader Authorization: OAuth 2.0 Bearer Token
|
||||
|
||||
:statuscode 200: User picture updated.
|
||||
:statuscode 200: user picture updated
|
||||
:statuscode 400:
|
||||
- Invalid payload.
|
||||
- No file part.
|
||||
- No selected file.
|
||||
- File extension not allowed.
|
||||
- invalid payload
|
||||
- no file part
|
||||
- no selected file
|
||||
- file extension not allowed
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
:statuscode 413: Error during picture update: file size exceeds 1.0MB.
|
||||
:statuscode 500: Error during picture update.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 413: error during picture update: file size exceeds 1.0MB
|
||||
:statuscode 500: error during picture update
|
||||
|
||||
"""
|
||||
try:
|
||||
@ -750,12 +750,12 @@ def edit_picture(auth_user_id: int) -> Union[Dict, HttpResponse]:
|
||||
db.session.commit()
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'User picture updated.',
|
||||
'message': 'user picture updated',
|
||||
}
|
||||
|
||||
except (exc.IntegrityError, ValueError) as e:
|
||||
return handle_error_and_return_response(
|
||||
e, message='Error during picture update.', status='fail', db=db
|
||||
e, message='error during picture update', status='fail', db=db
|
||||
)
|
||||
|
||||
|
||||
@ -783,10 +783,10 @@ def del_picture(auth_user_id: int) -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
|
||||
:statuscode 204: picture deleted
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
:statuscode 500: Error during picture deletion.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 500: error during picture deletion
|
||||
|
||||
"""
|
||||
try:
|
||||
@ -799,7 +799,7 @@ def del_picture(auth_user_id: int) -> Union[Tuple[Dict, int], HttpResponse]:
|
||||
return {'status': 'no content'}, 204
|
||||
except (exc.IntegrityError, ValueError) as e:
|
||||
return handle_error_and_return_response(
|
||||
e, message='Error during picture deletion.', status='fail', db=db
|
||||
e, message='error during picture deletion', status='fail', db=db
|
||||
)
|
||||
|
||||
|
||||
@ -823,14 +823,14 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Password reset request processed.",
|
||||
"message": "password reset request processed",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
:<json string email: user email
|
||||
|
||||
:statuscode 200: Password reset request processed.
|
||||
:statuscode 400: Invalid payload.
|
||||
:statuscode 200: password reset request processed
|
||||
:statuscode 400: invalid payload
|
||||
|
||||
"""
|
||||
post_data = request.get_json()
|
||||
@ -862,7 +862,7 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
|
||||
reset_password_email.send(user_data, email_data)
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'Password reset request processed.',
|
||||
'message': 'password reset request processed',
|
||||
}
|
||||
|
||||
|
||||
@ -886,7 +886,7 @@ def update_password() -> Union[Dict, HttpResponse]:
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"message": "Password updated.",
|
||||
"message": "password updated",
|
||||
"status": "success"
|
||||
}
|
||||
|
||||
@ -894,10 +894,10 @@ def update_password() -> Union[Dict, HttpResponse]:
|
||||
:<json string password_conf: password confirmation
|
||||
:<json string token: password reset token
|
||||
|
||||
:statuscode 200: Password updated.
|
||||
:statuscode 400: Invalid payload.
|
||||
:statuscode 401: Invalid token.
|
||||
:statuscode 500: Error. Please try again or contact the administrator.
|
||||
:statuscode 200: password updated
|
||||
:statuscode 400: invalid payload
|
||||
:statuscode 401: invalid token, please request a new token
|
||||
:statuscode 500: error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
post_data = request.get_json()
|
||||
@ -931,7 +931,7 @@ def update_password() -> Union[Dict, HttpResponse]:
|
||||
db.session.commit()
|
||||
return {
|
||||
'status': 'success',
|
||||
'message': 'Password updated.',
|
||||
'message': 'password updated',
|
||||
}
|
||||
except (exc.OperationalError, ValueError) as e:
|
||||
return handle_error_and_return_response(e, db=db)
|
||||
|
@ -86,9 +86,9 @@ class User(BaseModel):
|
||||
try:
|
||||
return decode_user_token(auth_token)
|
||||
except jwt.ExpiredSignatureError:
|
||||
return 'Signature expired. Please log in again.'
|
||||
return 'signature expired, please log in again'
|
||||
except jwt.InvalidTokenError:
|
||||
return 'Invalid token. Please log in again.'
|
||||
return 'invalid token, please log in again'
|
||||
|
||||
@hybrid_property
|
||||
def workouts_count(self) -> int:
|
||||
|
@ -155,9 +155,9 @@ def get_users(auth_user_id: int) -> Dict:
|
||||
|
||||
:statuscode 200: success
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
|
||||
"""
|
||||
params = request.args.copy()
|
||||
@ -310,11 +310,11 @@ def get_single_user(
|
||||
|
||||
:statuscode 200: success
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 404:
|
||||
- User does not exist.
|
||||
- user does not exist
|
||||
"""
|
||||
try:
|
||||
user = User.query.filter_by(username=user_name).first()
|
||||
@ -350,7 +350,7 @@ def get_picture(user_name: str) -> Any:
|
||||
|
||||
:statuscode 200: success
|
||||
:statuscode 404:
|
||||
- User does not exist.
|
||||
- user does not exist
|
||||
- No picture.
|
||||
|
||||
"""
|
||||
@ -466,12 +466,12 @@ def update_user(
|
||||
|
||||
:statuscode 200: success
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
:statuscode 403: You do not have permissions.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 403: you do not have permissions
|
||||
:statuscode 404:
|
||||
- User does not exist.
|
||||
- user does not exist
|
||||
:statuscode 500:
|
||||
"""
|
||||
user_data = request.get_json()
|
||||
@ -527,15 +527,15 @@ def delete_user(
|
||||
|
||||
:statuscode 204: user account deleted
|
||||
:statuscode 401:
|
||||
- Provide a valid auth token.
|
||||
- Signature expired. Please log in again.
|
||||
- Invalid token. Please log in again.
|
||||
- provide a valid auth token
|
||||
- signature expired, please log in again
|
||||
- invalid token, please log in again
|
||||
:statuscode 403:
|
||||
- You do not have permissions.
|
||||
- you do not have permissions
|
||||
- You can not delete your account, no other user has admin rights.
|
||||
:statuscode 404:
|
||||
- User does not exist.
|
||||
:statuscode 500: Error. Please try again or contact the administrator.
|
||||
- user does not exist
|
||||
:statuscode 500: error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
try:
|
||||
|
@ -37,9 +37,9 @@ def check_passwords(password: str, password_conf: str) -> str:
|
||||
"""
|
||||
ret = ''
|
||||
if password_conf != password:
|
||||
ret = 'Password and password confirmation don\'t match.\n'
|
||||
ret = 'password: password and password confirmation do not match\n'
|
||||
if len(password) < 8:
|
||||
ret += 'Password: 8 characters required.\n'
|
||||
ret += 'password: 8 characters required\n'
|
||||
return ret
|
||||
|
||||
|
||||
@ -53,9 +53,9 @@ def register_controls(
|
||||
"""
|
||||
ret = ''
|
||||
if not 2 < len(username) < 13:
|
||||
ret += 'Username: 3 to 12 characters required.\n'
|
||||
ret += 'username: 3 to 12 characters required\n'
|
||||
if not is_valid_email(email):
|
||||
ret += 'Valid email must be provided.\n'
|
||||
ret += 'email: valid email must be provided\n'
|
||||
ret += check_passwords(password, password_conf)
|
||||
return ret
|
||||
|
||||
@ -67,7 +67,7 @@ def verify_user(
|
||||
Return user id, if the provided token is valid and if user has admin
|
||||
rights if 'verify_admin' is True
|
||||
"""
|
||||
default_message = 'Provide a valid auth token.'
|
||||
default_message = 'provide a valid auth token'
|
||||
auth_header = current_request.headers.get('Authorization')
|
||||
if not auth_header:
|
||||
return UnauthorizedErrorResponse(default_message), None
|
||||
|
Reference in New Issue
Block a user