Exemplo n.º 1
0
async def discover(
        *,
        resource: resources.Resource,
        subresource: Optional[str] = None,
        context: Optional[auth.APIContext] = None,  # injected by the decorator
) -> Optional[Dict[str, object]]:
    if context is None:
        raise RuntimeError("API instance is not injected by the decorator.")

    if resource.api_version not in context._discovered_resources:
        async with context._discovery_lock:
            if resource.api_version not in context._discovered_resources:
                context._discovered_resources[resource.api_version] = {}

                try:
                    url = resource.get_version_url(server=context.server)
                    rsp = await errors.parse_response(await
                                                      context.session.get(url))

                    context._discovered_resources[resource.api_version].update(
                        {info['name']: info
                         for info in rsp['resources']})

                except (errors.APINotFoundError, errors.APIForbiddenError):
                    pass

    name = resource.plural if subresource is None else f'{resource.plural}/{subresource}'
    return context._discovered_resources[resource.api_version].get(name, None)
Exemplo n.º 2
0
async def discover(
        *,
        resource: resources.Resource,
        context: Optional[auth.APIContext] = None,  # injected by the decorator
) -> Optional[Dict[str, object]]:
    if context is None:
        raise RuntimeError("API instance is not injected by the decorator.")

    if resource.api_version not in context._discovered_resources:
        async with context._discovery_lock:
            if resource.api_version not in context._discovered_resources:
                context._discovered_resources[resource.api_version] = {}

                try:
                    response = await context.session.get(
                        url=resource.get_version_url(server=context.server), )
                    response.raise_for_status()
                    respdata = await response.json()

                    context._discovered_resources[resource.api_version].update(
                        {
                            resources.Resource(resource.group,
                                               resource.version, info['name']):
                            info
                            for info in respdata['resources']
                        })

                except aiohttp.ClientResponseError as e:
                    if e.status in [403, 404]:
                        pass
                    else:
                        raise

    return context._discovered_resources[resource.api_version].get(
        resource, None)