Exemplo n.º 1
0
def set_endpoints_credentials_args_from_remote(args):
    bf_url = os.environ.get('BF_URL')
    project_id = os.environ.get('BF_PROJECT_ID')
    if not project_id or not bf_url:
        return

    if not args.endpoints:
        logger.info("Fetching endpoints from server")
        url = "{}/project/{}/{}".format(bf_url, project_id, "endpoints")
        try:
            args.endpoints = asyncio.get_event_loop().run_until_complete(
                load_from_remote(EndpointConfig(url=url), "endpoints")
            )
        except aiohttp.ClientError:
            raise aiohttp.ClientError('Could not load endpoints for project {}.'.format(project_id))

    if not args.credentials:
        logger.info("Fetching credentials from server")
        url = "{}/project/{}/{}".format(bf_url, project_id, "credentials")
        try:
            args.credentials = asyncio.get_event_loop().run_until_complete(
                load_from_remote(EndpointConfig(url=url), "credentials")
            )
        except aiohttp.ClientError:
            raise aiohttp.ClientError('Could not load credentials for project {}.'.format(project_id))
Exemplo n.º 2
0
async def test_async_set_update_error(crd, caplog):
    """Test manually setting an update failure."""
    update_callback = Mock()
    crd.async_add_listener(update_callback)

    crd.async_set_update_error(aiohttp.ClientError("Client Failure #1"))
    assert crd.last_update_success is False
    assert "Client Failure #1" in caplog.text
    update_callback.assert_called_once()
    update_callback.reset_mock()

    # Additional failure does not log or change state
    crd.async_set_update_error(aiohttp.ClientError("Client Failure #2"))
    assert crd.last_update_success is False
    assert "Client Failure #2" not in caplog.text
    update_callback.assert_not_called()
    update_callback.reset_mock()

    crd.async_set_updated_data(200)
    assert crd.last_update_success is True
    update_callback.assert_called_once()
    update_callback.reset_mock()

    crd.async_set_update_error(aiohttp.ClientError("Client Failure #3"))
    assert crd.last_update_success is False
    assert "Client Failure #2" not in caplog.text
    update_callback.assert_called_once()
Exemplo n.º 3
0
    async def test_reconnect_if_disconnected(self):
        session_mock = CoroutineMock(get=CoroutineMock(
            side_effect=["", aiohttp.ClientError(),
                         aiohttp.ClientError()]))
        self.consumer.session = session_mock

        with asynctest.patch.object(self.consumer, 'keep_runnig', side_effect=[True, True, True, False]), \
                asynctest.patch.object(self.consumer, "_consume_events", side_effect=CoroutineMock()) as consume_events_mock:
            await self.consumer.start()
            self.assertEqual(1, consume_events_mock.await_count)
            self.assertEqual(3, session_mock.get.call_count)
            self.assertEqual(1, session_mock.get.await_count)
Exemplo n.º 4
0
    async def loadImg(self, src, name):
        try:
            async with aiohttp.ClientSession() as session:
                async with session.get(src, headers=headers,
                                       timeout=60) as response:
                    if response.status == 200:
                        image_content = await response.read()
                    else:
                        raise aiohttp.ClientError()
        except Exception as e:
            print(e)
            return

        width = self.width
        height = self.height

        pic = QPixmap()
        pic.loadFromData(image_content)
        localSrc = cacheFolder + '/' + name
        pic.save(localSrc, 'jpg')
        pic = pic.scaled(width, height)

        self.src = localSrc

        # 上遮罩。
        if self.pixMask:
            mask = QPixmap()
            mask.load(self.pixMask)
            mask = mask.scaled(width, height)

            pic.setMask(mask.createHeuristicMask())

        self.setPixmap(pic)
Exemplo n.º 5
0
    async def cycle(self, session):
        try:
            async with session.request(
                    self.Method,
                    self.URL,
                    headers=self.Headers if len(self.Headers) > 0 else None,
                    ssl=self.SSL,
            ) as response:
                if response.status not in self.ResponseCodes:
                    await response.text()
                    raise InvalidResponseStatusCodeError(
                        "The response status code {} from '{}' is invalid".
                        format(response.status, self.URL))
                else:
                    self.FailedResponses = 0  # Reset the counter
                await self.read(response)

        except aiohttp.ClientError as e:
            self.FailedResponses += 1
            if self.FailedResponses < self.MaxFailedResponses:
                L.warn("{}, will retry ({}/{}) in {:0.0f} sec".format(
                    e, self.FailedResponses, self.MaxFailedResponses,
                    self.FailChilldown))
                self.Pipeline.MetricsCounter.add('warning', 1)
                await asyncio.sleep(self.FailChilldown)
            else:
                L.error("{}, {} failed response(s)".format(
                    e, self.FailedResponses))
                raise aiohttp.ClientError("{}, url='{}'".format(e, self.URL))
Exemplo n.º 6
0
async def download_one(semaphore, session, image):
    logger.debug('Downloading No.{} [{}]'.format(image['linkno'],
                                                 image['link']))
    t0 = time.time()

    try:
        async with semaphore:
            async with session.get(image['link']) as response:
                if response.status == 200:
                    image_content = await response.read(
                    )  # Binary Response Content: access the response body as bytes, for non-text requests
                else:
                    logger.error(
                        'received invalid response code: {}, message: {}'.
                        format(response.status, response.reason))
                    raise aiohttp.ClientError()
    except Exception as e:
        logger.error('Exception {} raised on No.{} [{}]'.format(
            e.__class__, image['linkno'], image['link']))
        return False  # 用于告知 download_one() 的调用方,请求此图片URL时失败了

    filename = os.path.split(image['link'])[1]
    async with aiofiles.open(os.path.join(image['path'], filename), 'wb') as f:
        await f.write(image_content)

    t1 = time.time()
    logger.debug('Task No.{} [{}] runs {:.2f} seconds.'.format(
        image['linkno'], image['link'], t1 - t0))

    return True  # 用于告知 download_one() 的调用方,成功请求此图片URL
Exemplo n.º 7
0
    async def download_one(semaphore,session,url,name,path):
        path = os.path.join(basepath,path)
        if not os.path.exists(path):
            os.makedirs(path)
        path = os.path.join(path,name)
        if os.path.exists(path):  # 图片已存在,如果链接对应的图片已存在,则忽略下载
            return {'ignored': True  # 用于告知download_one()的调用方,此图片被忽略下载
            }

        try:
            async with semaphore:
                async with session.get(url) as response:
                    if response.status == 200:
                        image_content = await response.read()  # Binary Response Content: access the response body as bytes, for non-text requests
                    else:
                        raise aiohttp.ClientError()
        except Exception as e:
            print(url)
            return {
                'failed': True  # 用于告知download_one()的调用方,请求此图片URL时失败了
            }

        async with aiofiles.open(path, 'wb') as f:
            await f.write(image_content)
        return {
            'failed': False  # 用于告知download_one()的调用方,此图片被成功下载
        }
Exemplo n.º 8
0
async def update_coro(sess: aiohttp.ClientSession, addrs: Tuple[str, str],
                      skus: List[str]) -> Coroutine:
    """Update tags
    """
    try:
        pull_data_url = urllib.parse.urljoin(addrs[0], _config.PRODUCT_API)
        push_data_url = urllib.parse.urljoin(addrs[1],
                                             _config.ELS_UPDATE_PRODUCT_API)
        p_res = await sess.get(pull_data_url,
                               params=_make_pull_product_request(skus))
        data = await p_res.json()
        if p_res.status != 200:
            raise aiohttp.ClientPayloadError(
                f"Call product server: {data['message']}")
        els_res = await sess.post(push_data_url,
                                  json=_make_push_product_request(
                                      data['result']['products'], skus))
        data = await els_res.json()
        if els_res.status != 200:
            raise aiohttp.ClientError(f"Call els server: {data['ERROR']}")
    except aiohttp.ClientError as err:
        logging.error(str(err))
    else:
        logging.info('Push data success')
        logging.info(data)
Exemplo n.º 9
0
    def test_exceptions(self):
        import aiohttp

        exceptions = aiohttp_.AiohttpClient.exceptions

        with pytest.raises(exceptions.BaseClientException):
            raise aiohttp.ClientError()

        with pytest.raises(exceptions.BaseClientException):
            # Test polymorphism
            raise aiohttp.InvalidURL("invalid")

        with pytest.raises(exceptions.ConnectionError):
            raise aiohttp.ClientConnectionError()

        with pytest.raises(exceptions.ConnectionTimeout):
            raise aiohttp.ClientConnectorError.__new__(
                aiohttp.ClientConnectorError)

        with pytest.raises(exceptions.ServerTimeout):
            raise aiohttp.ServerTimeoutError()

        with pytest.raises(exceptions.SSLError):
            raise aiohttp.ClientSSLError.__new__(aiohttp.ClientSSLError)

        with pytest.raises(exceptions.InvalidURL):
            raise aiohttp.InvalidURL("invalid")
Exemplo n.º 10
0
def test_acquire_updates(mock_post):
    def make_mock(exc):
        cm = CoroutineMock()
        cm.__aenter__ = CoroutineMock(side_effect=exc)
        cm.__aexit__ = CoroutineMock()
        return cm

    mock_post.side_effect = [
        make_mock(aiohttp.ClientError()),
        make_mock(asyncio.CancelledError()),
        make_mock(Exception()),
    ]

    async def test():
        telegram = Telegram(token="token", session=aiohttp.ClientSession())

        await telegram.acquire_updates(None)

        with pytest.raises(asyncio.CancelledError):
            await telegram.acquire_updates(None)

        await telegram.acquire_updates(None)

        await telegram.session.close()

    asyncio.get_event_loop().run_until_complete(test())
Exemplo n.º 11
0
def test_acquire_updates(mock_post):
    def make_mock(exc):
        cm = CoroutineMock()
        cm.__aenter__ = CoroutineMock(side_effect=exc)
        cm.__aexit__ = CoroutineMock()
        return cm

    mock_post.side_effect = [
        make_mock(aiohttp.ClientError()),
        make_mock(asyncio.CancelledError()),
        make_mock(Exception()),
    ]

    async def test():
        vkontakte = VkontakteLongpoll(token="token", session=aiohttp.ClientSession())

        vkontakte.longpoll_data = {
            "ts": "ts",
            "key": "key",
            "server": "server",
        }

        await vkontakte.acquire_updates(None)

        with pytest.raises(asyncio.CancelledError):
            await vkontakte.acquire_updates(None)

        await vkontakte.acquire_updates(None)

    asyncio.get_event_loop().run_until_complete(test())
Exemplo n.º 12
0
async def test_api_ping_exeption(oppio_handler, aioclient_mock):
    """Test setup with API ping exception."""
    aioclient_mock.get("http://127.0.0.1/supervisor/ping",
                       exc=aiohttp.ClientError())

    assert not (await oppio_handler.is_connected())
    assert aioclient_mock.call_count == 1
Exemplo n.º 13
0
def mock_url_open(session, data, error_msg=None):
    """
    Mock opening of an URL.

    The following call needs to be mocked::

        ClientSession.get().__aenter__().read()

    If error message set, then it is raised as `aiohttp.ClientError`
    exception.

    :param session: Mock of `aiohttp` client session.
    :param data: Data to be assigned to HTTP response.
    :param error_msg: Error encountered when an exception is raised.
    """
    mock_get = session.get.return_value
    mock_ctx = mock_get.__aenter__.return_value = mock.MagicMock()

    if error_msg:
        params = {'side_effect': aiohttp.ClientError(error_msg)}
    else:
        params = {'return_value': data}

    mock_ctx.read = mock.AsyncMock(**params)
    yield session
Exemplo n.º 14
0
    async def scrape_and_upload() -> HospitalID:
        scraper = error_boundary(s)
        result = await scraper()
        if not result:
            return ""
        (hospital_id, availability) = result
        primitive_availability = {
            k: v.__str__()
            for k, v in availability.items()
        }

        timeout = aiohttp.ClientTimeout(total=5)
        async with aiohttp.ClientSession(timeout=timeout) as session:
            global API_URL
            url_root = API_URL
            if url_root is None:
                raise NameError("API URL not specified.")
            response = await session.post(
                url_root + "/hospital",
                json={
                    "api_key": API_KEY,
                    "hospital_id": hospital_id,
                    "availability": primitive_availability,
                },
            )
            if response.status != 200:
                raise aiohttp.ClientError("Updating hospitals failed.")
        return hospital_id
Exemplo n.º 15
0
    async def test_request_client_error(self):
        self.auth.token_type = "type"
        self.auth.access_token = "token"
        base_url = "base_url"
        response_data = object()
        response = mock.MagicMock()
        response.json = mock.CoroutineMock(return_value=response_data)
        session = mock.MagicMock()
        error = aiohttp.ClientError("error")
        session.request = mock.CoroutineMock(side_effect=error)
        self.client._get_http_session = mock.CoroutineMock(
            return_value=session
        )
        self.client._get_base_url = mock.CoroutineMock(return_value=base_url)
        self.client._verify_response = mock.CoroutineMock()
        method = "method"
        path = "path"
        json = object()
        params = object()
        auth_header = self.auth.token_type + " " + self.auth.access_token
        expected_headers = {"Authorization": auth_header}

        with self.assertRaisesRegex(NetworkError, str(error)):
            await self.client._request(method,
                                       path,
                                       json=json,
                                       params=params)

        session.request.assert_called_with(method,
                                           base_url + path,
                                           json=json,
                                           params=params,
                                           headers=expected_headers)
Exemplo n.º 16
0
async def fetch(url, params):
    '''Get json object from the url'''

    async with aiohttp.ClientSession() as session:
        async with session.get(url, params=params) as response:
            if response.status != 200:
                print(response.status)
                raise aiohttp.ClientError(
                    'Page request failed, code {}'.format(response.status))

            data = await response.json()

    teams = data['leagueTable']['team']

    # Debug only: pretty print the teams list
    if DEBUG:
        pprint(teams)

    # strip out just the scores for all teams
    scores = [int(team['points']) for team in teams]

    # locate how many points our team scored
    team_score = [
        int(team['points']) for team in teams if team['name'] == TEAM_NAME
    ][0]

    return scores, team_score
Exemplo n.º 17
0
    async def download_one(semaphore1, semaphore2, session, x, y, z):
        path = os.path.join(basepath, str(z), str(x))
        if not os.path.exists(path):
            os.makedirs(path)
        path = os.path.join(path, "{}.jpg".format(y))
        if os.path.exists(path):  # 图片已存在,如果链接对应的图片已存在,则忽略下载
            return {
                'ignored': True  # 用于告知download_one()的调用方,此图片被忽略下载
            }

        url = "{}&x={}&y={}&z={}&udt={}".format(baseurls[(x + y) % 3], x, y, z,
                                                mapfeature)
        try:
            async with semaphore1:
                async with session.get(url, timeout=60) as response:
                    if response.status == 200:
                        image_content = await response.read(
                        )  # Binary Response Content: access the response body as bytes, for non-text requests
                    else:
                        raise aiohttp.ClientError()
        except Exception as e:
            return {
                'failed': True  # 用于告知download_one()的调用方,请求此图片URL时失败了
            }

        async with semaphore2:
            async with aiofiles.open(path, 'wb') as f:
                await f.write(image_content)

        return {
            'failed': False  # 用于告知download_one()的调用方,此图片被成功下载
        }
Exemplo n.º 18
0
def test_async_aiohttp_proxy_stream_client_err(aioclient_mock, camera_client):
    """Test that it fetches the given url."""
    aioclient_mock.get("http://example.com/mjpeg_stream",
                       exc=aiohttp.ClientError())

    resp = yield from camera_client.get(
        "/api/camera_proxy_stream/camera.config_test")
    assert resp.status == 502
Exemplo n.º 19
0
async def test_failed_to_send_raises(hass, caplog, aioclient_mock):
    """Test raises when failed to send payload."""
    aioclient_mock.post(ANALYTICS_ENDPOINT_URL, exc=aiohttp.ClientError())
    analytics = Analytics(hass)
    await analytics.save_preferences({ATTR_BASE: True})
    assert analytics.preferences[ATTR_BASE]
    await analytics.send_analytics()
    assert "Error sending analytics" in caplog.text
Exemplo n.º 20
0
    async def test_network_error(self, ctx):
        """
        Deliberately raises a network error to test the bot's spurious network failure handling.
        """

        self.journal.send(
            "error/network", ctx.guild, "Raising aiohttp network error", icon="error"
        )
        raise aiohttp.ClientError()
Exemplo n.º 21
0
 async def test_get_translations_with_client_error_should_raise_retrieval_error(self, m_aioresponses, m_stderr) -> None:
     entry_language = 'en'
     entry_name = 'Amsterdam'
     api_url = 'https://%s.wikipedia.org/w/api.php?action=query&titles=%s&prop=langlinks&lllimit=500&format=json&formatversion=2' % (entry_language, entry_name)
     m_aioresponses.get(api_url, exception=aiohttp.ClientError())
     with TemporaryDirectory() as cache_directory_path:
         with self.assertRaises(RetrievalError):
             async with aiohttp.ClientSession() as session:
                 await _Retriever(session, Path(cache_directory_path)).get_translations(entry_language, entry_name)
Exemplo n.º 22
0
async def get_city_forecast(session: aiohttp.ClientSession, city: str) -> Dict:
    url = f"{API_URL}?q={city}&appid={API_KEY}"

    try:
        async with session.get(url) as response:
            return await response.json()

    except Exception as e:
        raise aiohttp.ClientError(str(e))
Exemplo n.º 23
0
async def post_async(url, payload):
    async with aiohttp.ClientSession() as async_session:
        async with async_session.post(url, headers = headers, data = payload) as response:
            content = await response.read()

            if response.status >= 400:
                raise aiohttp.ClientError(json.loads(content))
            
            return json.loads(content)
Exemplo n.º 24
0
async def test_async_client_retry_exception(aiohttp_mock):
    client = SwarmAsyncClient(
        'http://server/api/v9',
        'login',
        'password',
        retry=dict(
            total=2,
            statuses=[500],
        )
    )

    aiohttp_mock.get('http://server/api/v9/version', exception=aiohttp.ClientError())
    aiohttp_mock.get('http://server/api/v9/version', exception=aiohttp.ClientError())

    with pytest.raises(SwarmError):
        await client.get_version()

    await client.close()
Exemplo n.º 25
0
 def fake_get(url, *args, **kwargs):
     ip_port = url.split('://')[1].split('/')[0]
     if ip_port == '10.10.7.86:17447':
         json_method = MagicMock(
             return_value=future(tq_stats['10.10.7.86:17447']))
         response = MagicMock(json=json_method)
         return AsyncContextMock(aenter=response)
     if ip_port == '10.10.7.86:17448':
         json_method = MagicMock(
             return_value=future(tq_stats['10.10.7.86:17448']))
         response = MagicMock(json=json_method)
         return AsyncContextMock(aenter=response)
     if ip_port == '10.10.7.86:17449':
         error = aiohttp.ClientError('HTTP 504: Gateway Timeout')
         response = MagicMock(raise_for_status=MagicMock(side_effect=error))
         return AsyncContextMock(aenter=response)
     if ip_port == '10.10.7.86:17450':
         raise aiohttp.ClientError('Connection refused')
Exemplo n.º 26
0
async def test_name_fallback_on_exception(
    hass: HomeAssistant, mock_aircon1_api: MagicMock
):
    """Test name property."""
    mock_aircon1_api.fetch_name = AsyncMock(side_effect=aiohttp.ClientError())

    await init_integration(hass)
    state = hass.states.get("climate.said1")
    assert state.attributes[ATTR_FRIENDLY_NAME] == "said1"
Exemplo n.º 27
0
async def get_token(session: aiohttp.ClientSession) -> str:
    try:
        async with session.get(URL) as response:
            response = await response.text()
            soup = BeautifulSoup(response, 'html.parser')

            return soup.find('input', {'id': 'token'})['value']
    except Exception as e:
        raise aiohttp.ClientError(str(e))
Exemplo n.º 28
0
async def test_failed_to_send_raises(hass, caplog, aioclient_mock):
    """Test raises when failed to send payload."""
    aioclient_mock.post(ANALYTICS_ENDPOINT_URL, exc=aiohttp.ClientError())
    analytics = Analytics(hass)
    await analytics.save_preferences({ATTR_BASE: True})
    assert analytics.preferences[ATTR_BASE]

    with patch("homeassistant.components.analytics.analytics.HA_VERSION", MOCK_VERSION):
        await analytics.send_analytics()
    assert "Error sending analytics" in caplog.text
Exemplo n.º 29
0
async def test_async_client_retry_exception(aiohttp_mock):
    client = AsyncQBClient('http://server',
                           'login',
                           'password',
                           retry=dict(
                               total=2,
                               statuses=[500],
                           ))

    aiohttp_mock.get('http://server/rest/version',
                     exception=aiohttp.ClientError())

    aiohttp_mock.get('http://server/rest/version',
                     exception=aiohttp.ClientError())

    with pytest.raises(QBError):
        await client.system.get_version()

    await client.close()
Exemplo n.º 30
0
 async def test_get_entry_with_client_error_should_raise_retrieval_error(self, m_aioresponses, m_stderr) -> None:
     entry_language = 'en'
     entry_name = 'Amsterdam'
     api_url = 'https://en.wikipedia.org/w/api.php?action=query&titles=Amsterdam&prop=extracts&exintro&format=json&formatversion=2'
     m_aioresponses.get(api_url, exception=aiohttp.ClientError())
     with TemporaryDirectory() as cache_directory_path:
         async with aiohttp.ClientSession() as session:
             retriever = _Retriever(session, Path(cache_directory_path))
             with self.assertRaises(RetrievalError):
                 await retriever.get_entry(entry_language, entry_name)