Beispiel #1
0
def get_user(user_email):
    user = UserModel.find_by_email(user_email)
    if not user:
        response = build_response({'error message': 'User not found'}, 404)
        return response
    response = build_response({'mail': user.email})
    return response
Beispiel #2
0
def delete_kitty(user_id, kitty_id):
    kitty = KittyModel.find_by_id(kitty_id)
    user_kitties = KittyModel.find_by_user(user_id)
    if kitty not in user_kitties:
        response = build_response({'error message': 'Kitty not found'}, 404)
        return response
    kitty.delete()
    response = build_response({'message': 'Kitty deleted'})
    return response
Beispiel #3
0
def delete_fav(user_id, fav_id):
    favourite = FavouritesModel.find_by_id(fav_id)
    user_favs = FavouritesModel.find_by_user(user_id)
    if favourite not in user_favs:
        response = build_response(
            {'error message': 'Favourite kitty not found'}, 404)
        return response
    favourite.delete()
    response = build_response({'message': 'Favourite kitty deleted'})
    return response
Beispiel #4
0
def get_kitty_by_id(user_id, kitty_id):
    kitties_from_db = get_user_kitties(user_id)
    kitty_by_id = KittyModel.find_by_id(kitty_id)
    if kitty_by_id.name not in kitties_from_db:
        response = build_response({'error message': 'Kitty not found'}, 404)
        return response
    user = UserModel.find_by_id(user_id)
    kitty_dict = kitty_by_id.serialize_kitty()
    kitty_dict['user'] = {'id': user_id, 'email': user.email}
    response = build_response(kitty_dict)
    return response
Beispiel #5
0
def put_kitty(kitty_data, user_id, kitty_id):
    kitty = KittyModel.find_by_id(kitty_id)
    user_kitties = KittyModel.find_by_user(user_id)
    if kitty not in user_kitties:
        response = build_response({'error message': 'Kitty not found'}, 404)
        return response
    kitty.name = kitty_data['name']
    kitty.photo = kitty_data['photo']
    kitty.save()
    response = build_response({'name': kitty.name})
    return response
Beispiel #6
0
def login_user(user_data):
    user_email = user_data['email']
    expires = timedelta(seconds=int(os.environ.get('EXPIRE_TIME', '1200')))
    user = UserModel.find_by_email(user_email)
    if user and safe_str_cmp(user.password, user_data['password']):
        access_token = create_access_token(identity=user.id,
                                           expires_delta=expires)
        response = build_response({'token': access_token})
        return response
    response = build_response({'error message': 'Invalid credentials'}, 403)
    return response
 def put(self, user_id):
     if get_jwt_identity() == user_id:
         json_data = request.get_json()
         try:
             data = user_schema.load(json_data)
         except:
             return build_response({'error message': 'Bad request'}, 400)
         response = put_user(data, user_id)
         return response
     else:
         response = build_response({'error message': 'Invalid credentials'},
                                   403)
         return response
    def get(self):
        try:
            r = requests.get(url="{}".format(self.modes_url),
                             timeout=app.config.get("cowbull_timeout"),
                             headers=self.headers)
        except Exception as e:
            return html_error_handler(
                html_status=500,
                html_exception=repr(e),
                html_message=
                "An exception occurred while accessing the game service modes.",
                html_module="Modes.py",
                html_method="get")

        _response = self.response
        if r.status_code in [200, 400, 500]:
            _response["server"] = r.json()
            _response["status"] = r.status_code
        else:
            # TODO Circuit breaker
            return html_error_handler(
                html_status=r.status_code,
                html_message=r.text,
                html_exception="Error occurred while fetching a new game",
                html_module="Game.py",
                html_method="get")

        return build_response(response_data=_response,
                              html_status=r.status_code)
Beispiel #9
0
 def get(self):
     return build_response(html_status=200,
                           response_data={
                               "method": "get",
                               "status": "work in progress"
                           },
                           response_mimetype="application/json")
Beispiel #10
0
 def get(self):
     #
     # TODO: Add readiness checking
     #
     return build_response(html_status=200,
                           response_data={"status": "ready"},
                           response_mimetype="application/json")
Beispiel #11
0
def post_kitty(kitty_data, user_id):
    if not kitty_data:
        response = build_response({'error message': 'Wrong data format'}, 400)
        return response
    kitty_name, kitty_photo = kitty_data['name'], kitty_data['photo']
    if not cat_facade.name_validation(kitty_name):
        response = build_response({'error message': 'Name is empty'}, 400)
        return response
    if KittyModel.find_by_name(kitty_name):
        response = build_response({'error message': 'Name already exists'},
                                  409)
        return response
    kitty = KittyModel(kitty_name, kitty_photo, user_id)
    kitty.save()
    response = build_response({'name': kitty_name, 'photo': kitty_photo})
    return response
    def get(self):
        #
        # TODO: Add some health checking criteria, probably verifying server_url connections.
        #
        _response = {"health": "ok"}
        _status = 200

        return build_response(response_data=_response, html_status=_status)
Beispiel #13
0
def get_kitty():
    kitty_from_db = _get_random_kitty_from_db()
    kitty_from_cataas = _get_random_kitty_from_cataas()
    kitty = random.choice([kitty_from_db, kitty_from_cataas])
    if not kitty:
        kitty = _get_random_kitty_from_cataas()
    response = build_response(kitty)
    return response
 def post(self):
     json_data = request.get_json()
     try:
         data = user_schema.load(json_data)
     except:
         return build_response({'error message': 'Bad request'}, 400)
     response = login_user(data)
     return response
 def get(self, user_email):
     if get_jwt_identity() == user_id:
         response = get_user(user_email)
         return response
     else:
         response = build_response({'error message': 'Invalid credentials'},
                                   403)
         return response
Beispiel #16
0
def post_user(user_data):
    if not user_data:
        response = build_response({'error message': 'Wrong data format'}, 400)
        return response
    user_email, user_password = user_data['email'], user_data['password']
    if not user_facade.email_validation(user_email):
        response = build_response(
            {'error message': 'Email has an incorrect format'}, 400)
        return response
    if UserModel.find_by_email(user_email):
        response = build_response(
            {'error message': 'Email already exists'}, 409)
        return response
    user = UserModel(user_email, user_password)
    user.save()
    response = build_response({'email': user.email})
    return response
Beispiel #17
0
 def get(self, user_id, kitty_id):
     if get_jwt_identity() == user_id:
         response = get_kitty_by_id(user_id, kitty_id)
         return response
     else:
         response = build_response({'error message': 'Invalid credentials'},
                                   403)
         return response
 def get(self, user_id):
     if get_jwt_identity() == user_id:
         limit = request.args.get('limit', type=int)
         page = request.args.get('page', type=int)
         response = get_favourites(user_id, limit, page)
         return response
     else:
         response = build_response({'error message': 'Invalid credentials'},
                                   403)
         return response
Beispiel #19
0
def get_kitties(user_id, limit, page):
    kitties = KittyModel.query.filter_by(user_id=user_id).paginate(
        page=page, max_per_page=limit)
    total = kitties.total
    offset = ((page - 1) * limit)
    if not kitties:
        response = build_response(
            {'error message': 'This user has no kitties yet'}, 404)
        return response
    data = dict()
    for kitty in kitties.items:
        data[kitty.name] = kitty.serialize_kitty()
    response = build_response({
        'offset': offset,
        'limit': limit,
        'total': total,
        'data': data
    })
    return response
Beispiel #20
0
def get_favourites(user_id, limit, page):
    favourites = FavouritesModel.query.filter_by(user_id=user_id).paginate(
        page=page, max_per_page=limit)
    total = favourites.total
    offset = ((page - 1) * limit)
    if not favourites:
        response = build_response(
            {'error message': 'This user has no favourite kitties yet'}, 404)
        return response
    data = dict()
    for favourite in favourites.items:
        data[favourite.kitty_name] = favourite.serialize_favourite()
    response = build_response({
        'offset': offset,
        'limit': limit,
        'total': total,
        'data': data
    })
    return response
Beispiel #21
0
def put_user(user_data, user_id):
    user = UserModel.find_by_id(user_id)  # Buscamos el user por id
    if user:  # Si existe ese user
        # Comprobamos si existe ya un user con ese email
        user_email = UserModel.find_by_email(user_data['email'])
        if not user_email:  # Si el email está disponible: machacamos los datos
            if not user_facade.email_validation(user_data['email']):
                response = build_response(
                    {'error message': 'Email has an incorrect format'}, 400)
                return response
            user.email = user_data['email']
            user.password = user_data['password']
        else:  # El email NO está disponible: devolvemos un error 409
            response = build_response(
                {'error message': 'Email alrady exists'}, 409)
            return response
    else:  # Si no existe ese user, devolvemos un error 404
        response = build_response({'error message': 'User not found'}, 404)
        return response

    user.save()  # Guardo la información
    response = build_response({'email': user.email})
    return response
Beispiel #22
0
def post_fav(fav_data, user_id):
    if not fav_data:
        response = build_response({'error message': 'Wrong data format'}, 400)
        return response
    fav_name = fav_data['name']
    if fav_data['photo']:
        fav_image = fav_data['photo']
        source = 'photo'
        favourite = FavouritesModel(
            kitty_name=fav_name, user_id=user_id, photo=fav_image)
        favourite.save()
    elif fav_data['url']:
        fav_image = fav_data['url']
        source = 'url'
        favourite = FavouritesModel(
            kitty_name=fav_name, user_id=user_id, url=fav_image)
        favourite.save()
    else:
        response = build_response(
            {'error message': 'inform "url" or "photo"'}, 400)
        return response
    response = build_response({'name': fav_name, source: fav_image})
    return response
    def post(self):
        load_env(app=app)
        _response = {
            "action": "reload",
            "configuration": {
                "cowbull_server":
                app.config.get("COWBULL_SERVER", "localhost"),
                "cowbull_port":
                app.config.get("COWBULL_PORT", 5000),
                "cowbull_game_version":
                app.config.get("COWBULL_GAME_VERSION", "v1")
            }
        }
        _status = 200

        return build_response(response_data=_response, html_status=_status)
def html_error_handler(
        html_status=None,
        html_exception=None,
        html_message=None,
        html_module=None,
        html_method=None
):
    response_dict = {
        "status": "error",
        "module": html_module or "NA",
        "method": html_method or "NA",
        "exception": html_exception or "An exception occurred.",
        "message": html_message or "Oops! An error has occurred and not been caught! It has been logged."
    }
    return build_response(
        response_data=response_dict,
        html_status=html_status
    )
    def get(self, env=None):
        if not env:
            _response = {
                "action": "get",
                "configuration": {
                    "cowbull_server":
                    app.config.get("COWBULL_SERVER", "localhost"),
                    "cowbull_port":
                    app.config.get("COWBULL_PORT", 5000),
                    "cowbull_game_version":
                    app.config.get("COWBULL_GAME_VERSION", "v1")
                }
            }
        else:
            _response = {
                "action": "get one",
                "configuration": {
                    env: app.config.get(env, "unknown")
                }
            }
        _status = 200

        return build_response(response_data=_response, html_status=_status)
Beispiel #26
0
    def post(self, gamekey=None):
        if not gamekey:
            return html_error_handler(
                html_status=400,
                html_message="The game key must be provided",
                html_exception="No game key",
                html_module="Game.py",
                html_method="post")

        try:
            user_data = request.get_json()
            digits = user_data["digits"]
        except KeyError as ke:
            return html_error_handler(
                html_status=400,
                html_message=
                "There were no digits found in the request's JSON data. "
                "Are you sure they were provided AND that the header was "
                "passed as content-type:application/json?",
                html_exception="No digits in the data",
                html_module="Game.py",
                html_method="post")

        except Exception as e:
            return html_error_handler(
                html_status=400,
                html_message=
                "There was no JSON data found in the request. Are you "
                "sure it was provided AND that the header was passed "
                "as content-type:application/json?",
                html_exception="No JSON data could be found with the request",
                html_module="Game.py",
                html_method="post")

        payload = {"key": gamekey, "digits": digits}

        try:
            r = requests.post(url="{}".format(self.game_url),
                              data=json.dumps(payload),
                              timeout=app.config.get("cowbull_timeout"),
                              headers=self.headers)
        except Exception as e:
            return html_error_handler(
                html_status=500,
                html_exception=repr(e),
                html_message=
                "An exception occurred while accessing the game service.",
                html_module="Game.py",
                html_method="get")
        _response = self.response

        if r.status_code in [200, 201, 400, 500]:
            _response["server"] = r.json()
            _response["status"] = r.status_code
        else:
            # TODO Circuit breaker
            return html_error_handler(
                html_status=r.status_code,
                html_message=r.text,
                html_exception="Error occurred while fetching a new game",
                html_module="Game.py",
                html_method="get")

        return build_response(response_data=_response,
                              html_status=r.status_code)
Beispiel #27
0
def must_not_be_blank(data):
    if not data:
        raise ValidationError(
            build_response({'error message': f'{data} not provided'}))
    def put(self):
        _response = {"config put": "wip"}
        _status = 200

        return build_response(response_data=_response, html_status=_status)
Beispiel #29
0
def get_info():
    name = 'Kittinder'
    version = '1.0'
    response = build_response({'name': name, 'version': version})
    return response
    def delete(self):
        _response = {"config delete": "wip"}
        _status = 200

        return build_response(response_data=_response, html_status=_status)