예제 #1
0
    def get(self, user_id):
        """
        Returns a specific user.
        :param user_id: The user identifier
        """
        user = next((u for u in known_users if u['id'] == user_id), None)

        if user is None:
            return ErrorModel(**{'message': "User id {} not found".format(user_id)}), 404

        # Return data through schema model
        return UserModel(**user), 200
예제 #2
0
 def post_error(self, e):
     error_params = {
         'exception_name': e.__class__.__name__,
         'message': e.message if hasattr(e, 'message') else f"{e}"
     }
     logger.error(f"{self.name} {e}")
     with create_session() as session:
         account = session.query(AccountModel).filter_by(
             api_key=self.api_key).first()
         error = ErrorModel(message=error_params['message'],
                            exception_name=error_params['exception_name'],
                            account=account)
         session.add(error)
         session.commit()
예제 #3
0
    def post(self):
        """
        Adds a user.
        """
        # Validate request body with schema model
        try:
            data = UserModel(**request.get_json())

        except ValueError as e:
            return ErrorModel(**{'message': e.args[0]}), 400

        data['id'] = len(known_users) + 1
        known_users.append(data)

        return data, 201, {'Location': request.path + '/' + str(data['id'])}
예제 #4
0
def not_authorized(error):
    error = ErrorModel(405, 'Method is not allowed, check your request you may passing wrong parameters')
    return jsonify({
        "error": error.format()
    }), 405
예제 #5
0
def not_authorized(error):
    error = ErrorModel(403, 'You do not have access to this feature')
    return jsonify({
        "error": error.format()
    }), 403
예제 #6
0
def not_authorized(error):
    error = ErrorModel(400, 'Authorization error')
    return jsonify({
        "error": error.format()
    }), 400
예제 #7
0
def not_found(error):
    error = ErrorModel(401, 'You are unauthorized')
    return jsonify({
        "error": error.format()
    }), 401
예제 #8
0
def internal_server_error(error):
    error = ErrorModel(500, 'Internal server error')
    return jsonify({
        "error": error.format()
    }), 500
예제 #9
0
def not_found(error):
    error = ErrorModel(404, 'Page not found')
    return jsonify({
        "error": error.format()
    }), 404