Пример #1
0
async def fetch_all_user_pages(ctx: HTTPRequestContext, battletag: str, *,
                               platform="pc"):
    """
    Fetches all user pages for a specified user.

    Returns a dictionary in the format of `{"any": etree._Element | None}`.
    """
    if platform != "pc":
        coro = get_user_page(ctx, battletag, platform=platform, cache_404=True)
        result = await coro
        if isinstance(result, etree._Element):
            return {"any": result}
        else:
            # Raise a 404.
            raise NotFound()

    futures = []
    coro = get_user_page(ctx, battletag, platform=platform, cache_404=True)
    futures.append(coro)

    # Gather all the futures to download in parallel.
    results = await asyncio.gather(*futures, return_exceptions=True)
    user_data = {"any": None}
    error = None
    for key, result in zip(["any"], results):
        # Make sure it's either a None or an element.
        if isinstance(result, etree._Element):
            user_data[key] = result
        elif isinstance(result, Exception):
            logger.error("Failed to fetch user page!\n{}".format(
                ''.join(traceback.format_exception(type(result), result, result.__traceback__))
            ))
            error = result
            user_data[key] = None
        else:
            user_data[key] = None

    # Check if we should raise or return.
    if user_data["any"] is None:
        if error is not None:
            e = InternalServerError()
            e.__cause__ = error
            e.__context__ = error
            raise e
        raise NotFound()

    return user_data
Пример #2
0
async def fetch_all_user_pages(ctx: HTTPRequestContext,
                               battletag: str,
                               *,
                               platform="pc"):
    """
    Fetches all user pages for a specified user.

    Returns a dictionary in the format of `{region: etree._Element | None}`.
    """
    if platform != "pc":
        coro = get_user_page(ctx,
                             battletag,
                             region="",
                             platform=platform,
                             cache_404=True)
        result = await coro
        if isinstance(result, etree._Element):
            return {"any": result, "eu": None, "us": None, "kr": None}
        else:
            # Raise a 404.
            raise NotFound()

    futures = []
    for region in AVAILABLE_REGIONS:
        # Add the get_user_page coroutine.
        coro = get_user_page(ctx,
                             battletag,
                             region=region,
                             platform=platform,
                             cache_404=True)
        futures.append(coro)

    # Gather all the futures to download paralellely.
    results = await asyncio.gather(*futures, return_exceptions=True)
    d = {"any": None}
    error = None
    for region, result in zip(AVAILABLE_REGIONS, results):
        # Remove the `/` from the front of the region.
        # This is used internally to make building the URL to get simpler.
        region = region[1:]
        # Make sure it's either a None or an element.
        if isinstance(result, etree._Element):
            d[region] = result
        elif isinstance(result, Exception):
            logger.error("Failed to fetch user page!\n{}".format(''.join(
                traceback.format_exception(type(result), result,
                                           result.__traceback__))))
            error = result
            d[region] = None
        else:
            d[region] = None

    # Check if we should raise or return.
    if not any(d[i[1:]] is not None for i in AVAILABLE_REGIONS):
        if error is not None:
            e = InternalServerError()
            e.__cause__ = error
            e.__context__ = error
            raise e
        raise NotFound()

    return d