Exemplo n.º 1
0
def get_game():
    """get a all games or filter with query param filter.

  filters supported:
    active: games which have not winner.

  Returns:
    dict: list of games.
  """
    cursor = int(request.args.get('cursor', 1))
    filter_selected = request.args.get('filter')

    if filter_selected == 'active':
        game_query = Game.all().filter('winner', None)
    else:
        filter_selected = None
        game_query = Game.all()

    games = game_query.fetch(offset=PAGE_LIMIT * (cursor - 1),
                             limit=PAGE_LIMIT)

    if not games:
        error_response = {
            'errors': {
                'msg': 'No games found.'
            },
        }
        return (jsonify(error_response), 404)

    links = {'self': request.url}
    if cursor > 1:
        links['previous'] = '{base}?cursor={cursor}'.format(
            base=request.base_url, cursor=cursor - 1)
        if filter_selected is not None:
            links['previous'] = '{previous}&filter={filter}'.format(
                previous=links['previous'], filter=filter_selected)
    if len(games) == PAGE_LIMIT:
        links['next'] = '{base}?cursor={cursor}'.format(base=request.base_url,
                                                        cursor=cursor + 1)
        if filter_selected is not None:
            links['next'] = '{next}&filter={filter}'.format(
                next=links['next'], filter=filter_selected)

    response = {
        'data': [game.encode() for game in games],
        'links': links,
        'meta': {
            'cursor': cursor + 1
        }
    }

    return (jsonify(response), 200)
Exemplo n.º 2
0
def search_game(game_id):
    """Search a game which starts with the given game_id.

  Args:
    game_id (str): the game identifier.

  Returns:
    dict: the endpoint response.
  """
    cursor = int(request.args.get('cursor', 1))
    game_query = (Game.all().filter('game_id >=', game_id).filter(
        'game_id <',
        game_id + u'\uFFFD').fetch(offset=PAGE_LIMIT * (cursor - 1),
                                   limit=PAGE_LIMIT))

    if not game_query:
        error_response = {
            'errors': {
                'msg': 'No games found.'
            },
        }
        return (jsonify(error_response), 404)

    links = {'self': request.url}
    if cursor > 1:
        links['previous'] = '{base}?cursor={cursor}'.format(
            base=request.base_url, cursor=cursor - 1)
    if len(game_query) == PAGE_LIMIT:
        links['next'] = '{base}?cursor={cursor}'.format(base=request.base_url,
                                                        cursor=cursor + 1)

    response = {
        'data': [game.encode() for game in game_query],
        'links': links,
        'meta': {
            'cursor': cursor + 1
        }
    }

    return (jsonify(response), 200)