コード例 #1
0
def QA_fetch_bitmex_kline(symbol="XBTUSD",
                          count=100,
                          startTime="",
                          endTime='',
                          binSize="1m",
                          partial='false',
                          reverse='false',
                          proxies=proxies):
    body = ''

    url = '%s/trade/bucketed?' \
          'binSize=%s&partial=%s&symbol=%s&count=%d&reverse=%s&startTime=%s&endTime=%s' \
          % (Bitmex_base_url, binSize, partial, symbol, count, reverse, startTime, endTime)
    try:
        req = requests.get(url, timeout=TIMEOUT, proxies=proxies)
        remaining = int(req.headers['x-ratelimit-remaining'])
        if remaining <20:
            time.sleep(5)
        elif remaining <10:
            time.sleep(10)
        elif remaining <3:
            time.sleep(30)
    except ConnectTimeout:
        raise ConnectTimeout('Bitmex connect timeout when getting kline.')
    except (ssl.SSLError, requests.exceptions.SSLError) as ex:
        QA_util_log_info(ex)
    except Exception as ex:
        QA_util_log_expection(ex)

    return json.loads(req.content)
コード例 #2
0
def post(url,
         data=None,
         headers=None,
         encoding='UTF-8',
         files=None,
         proxies=None,
         tiemout=10):
    """POST请求发送包装"""
    try:
        html = requests.post(url,
                             data=data,
                             headers=headers,
                             proxies=proxies,
                             timeout=tiemout,
                             files=files)
        html = html.content.decode(encoding)
        return html.replace('\x00', '').strip()
    except ChunkedEncodingError as e:
        html = post_stream(url, data, headers, encoding, files)
        return html
    except ConnectionError as e:
        raise ConnectionError("ERROR:" + "HTTP连接错误")
    except ConnectTimeout as e:
        raise ConnectTimeout("ERROR:" + "HTTP连接超时错误")
    except Exception as e:
        raise Exception('ERROR:' + str(e))
コード例 #3
0
def QA_fetch_binance_kline(symbol, start_time, end_time, frequency):
    datas = list()
    start_time *= 1000
    end_time *= 1000
    while start_time < end_time:
        url = urljoin(Binance_base_url, "/api/v1/klines")
        try:
            req = requests.get(url, params={"symbol": symbol, "interval": frequency,
                                            "startTime": int(start_time),
                                            "endTime": int(end_time)}, timeout=TIMEOUT, proxies=proxies)
            # 防止频率过快被断连
            time.sleep(1)
        except ConnectTimeout:
            raise ConnectTimeout(ILOVECHINA)
        except (ssl.SSLError, requests.exceptions.SSLError) as ex:
            QA_util_log_info(ex)
            time.sleep(120)
            req = requests.get(url, timeout=TIMEOUT, proxies=proxies)
        except Exception as ex:
            break
        klines = json.loads(req.content)
        if len(klines) == 0:
            break
        datas.extend(klines)
        start_time = klines[-1][6]
    if len(datas) == 0:
        return None
    frame = pd.DataFrame(datas)
    frame.columns = columne_names
    frame['symbol'] = symbol
    return json.loads(frame.to_json(orient='records'))
コード例 #4
0
def test_post_data_timeout():
    """Test post data with timeout."""
    # Arrange
    api_key = '12345'
    resource_id = '1234-1234'
    uri = '/blah'
    expected_url = f'{BASE_URL}{uri}'

    measurement_single = """{
        "measurement": {
            "period_end": "2018-02-02T03:30:00.0000000Z",
            "period": "PT5M",
            "total_power": 1.23456
        }
    }"""

    responses.add(responses.POST,
                  expected_url,
                  body=ConnectTimeout(),
                  status=200,
                  content_type='applicaiton/json')

    # Act
    obj = PySolcast(api_key, resource_id)
    with pytest.raises(ConnectTimeout):
        obj._post_data(uri, measurement_single)  # pylint: disable=protected-access
コード例 #5
0
    def test_get_failed_servers_with_connection_error(self):
        servers = {
            'The Rosalind server': {
                'host': 'rosalind.info',
                'schemes': ['http', 'https']
            },
            'My Digital Ocean': {
                'host': 'vika.space',
                'ports': [80, 443]
            }
        }

        responses.add(responses.GET,
                      'https://rosalind.info',
                      body=ConnectionError('Not reachable'))
        responses.add(responses.GET, 'http://rosalind.info', status=200)
        responses.add(responses.GET,
                      'http://vika.space',
                      body=ConnectTimeout('Timeout'))
        responses.add(responses.GET, 'https://vika.space', status=302)
        results = list(get_failed_servers(servers))

        expected_data = [('https://rosalind.info', 'Not reachable'),
                         ('http://vika.space', 'Timeout')]
        self.assertEqual(sorted(results), sorted(expected_data))
コード例 #6
0
async def test_asserts(hass, test_api):
    """Test the _site_in_configuration_exists method."""
    flow = init_config_flow(hass)

    # test with inactive site
    test_api.get_details.return_value = {"details": {"status": "NOK"}}
    result = await flow.async_step_user(
        {CONF_NAME: NAME, CONF_API_KEY: API_KEY, CONF_SITE_ID: SITE_ID}
    )
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["errors"] == {CONF_SITE_ID: "site_not_active"}

    # test with api_failure
    test_api.get_details.return_value = {}
    result = await flow.async_step_user(
        {CONF_NAME: NAME, CONF_API_KEY: API_KEY, CONF_SITE_ID: SITE_ID}
    )
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["errors"] == {CONF_SITE_ID: "invalid_api_key"}

    # test with ConnectionTimeout
    test_api.get_details.side_effect = ConnectTimeout()
    result = await flow.async_step_user(
        {CONF_NAME: NAME, CONF_API_KEY: API_KEY, CONF_SITE_ID: SITE_ID}
    )
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["errors"] == {CONF_SITE_ID: "could_not_connect"}

    # test with HTTPError
    test_api.get_details.side_effect = HTTPError()
    result = await flow.async_step_user(
        {CONF_NAME: NAME, CONF_API_KEY: API_KEY, CONF_SITE_ID: SITE_ID}
    )
    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["errors"] == {CONF_SITE_ID: "could_not_connect"}
コード例 #7
0
def test_create_payment_record_rollback_on_paybc_connection_error(
        session, public_user_mock):
    """Assert that the payment records are not created."""
    # Create a payment account
    factory_payment_account()

    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectionError('mocked error')):
        with pytest.raises(ServiceUnavailableException) as excinfo:
            PaymentService.create_invoice(get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == ServiceUnavailableException

    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectTimeout('mocked error')):
        with pytest.raises(ServiceUnavailableException) as excinfo:
            PaymentService.create_invoice(get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == ServiceUnavailableException

    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=HTTPError('mocked error')) as post_mock:
        post_mock.status_Code = 503
        with pytest.raises(HTTPError) as excinfo:
            PaymentService.create_invoice(get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == HTTPError
コード例 #8
0
def QA_fetch_kline(symbol, start_time, end_time, frequency):
    datas = list()
    start_time *= 1000
    end_time *= 1000
    while start_time < end_time:
        url = urljoin(Binance_base_url, "/api/v1/klines")
        try:
            req = requests.get(url,
                               params={
                                   "symbol": symbol,
                                   "interval": frequency,
                                   "startTime": int(start_time),
                                   "endTime": int(end_time)
                               },
                               timeout=TIMEOUT)
        except ConnectTimeout:
            raise ConnectTimeout(MESSAGE_NOTICE)
        klines = json.loads(req.content)
        if len(klines) == 0:
            break
        datas.extend(klines)
        start_time = klines[-1][6]
    if len(datas) == 0:
        return None
    frame = pd.DataFrame(datas)
    frame.columns = columne_names
    frame['symbol'] = symbol
    return json.loads(frame.to_json(orient='records'))
コード例 #9
0
def test_create_payment_record_rollback_on_paybc_connection_error(session):
    """Assert that the payment records are not created."""
    from unittest.mock import Mock
    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectionError('mocked error')):
        with pytest.raises(ServiceUnavailableException) as excinfo:
            PaymentService.create_payment(get_payment_request(), 'test')
        assert excinfo.type == ServiceUnavailableException
    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectTimeout('mocked error')):
        with pytest.raises(ServiceUnavailableException) as excinfo:
            PaymentService.create_payment(get_payment_request(), 'test')
        assert excinfo.type == ServiceUnavailableException

    mock_create_site = patch('pay_api.services.oauth_service.requests.post')

    mock_post = mock_create_site.start()
    mock_post.return_value = Mock(status_code=503)

    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=HTTPError('mocked error')) as post_mock:
        post_mock.status_Code = 503
        with pytest.raises(HTTPError) as excinfo:
            PaymentService.create_payment(get_payment_request(), 'test')
        assert excinfo.type == HTTPError
コード例 #10
0
def post_stream(url,
                data=None,
                headers=None,
                encoding='UTF-8',
                files=None,
                proxies=None,
                tiemout=10):
    """分块接受数据"""
    try:
        lines = requests.post(url,
                              data=data,
                              headers=headers,
                              timeout=tiemout,
                              stream=True,
                              proxies=proxies,
                              files=None)
        html = list()
        for line in lines.iter_lines():
            line = line.decode(encoding)
            html.append(line.strip())
        return '\r\n'.join(html).strip()
    except ChunkedEncodingError as e:
        return '\r\n'.join(html).strip()
    except ConnectionError as e:
        raise ConnectionError("ERROR:" + "HTTP连接错误")
    except ConnectTimeout as e:
        raise ConnectTimeout("ERROR:" + "HTTP连接超时错误")
    except Exception as e:
        raise Exception('ERROR:' + str(e))
コード例 #11
0
ファイル: test_config_flow.py プロジェクト: jbouwh/core
async def test_asserts(hass: HomeAssistant, test_api: Mock) -> None:
    """Test the _site_in_configuration_exists method."""

    # test with inactive site
    test_api.get_details.return_value = {"details": {"status": "NOK"}}

    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USER},
        data={
            CONF_NAME: NAME,
            CONF_API_KEY: API_KEY,
            CONF_SITE_ID: SITE_ID
        },
    )
    assert result.get("type") == data_entry_flow.FlowResultType.FORM
    assert result.get("errors") == {CONF_SITE_ID: "site_not_active"}

    # test with api_failure
    test_api.get_details.return_value = {}
    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USER},
        data={
            CONF_NAME: NAME,
            CONF_API_KEY: API_KEY,
            CONF_SITE_ID: SITE_ID
        },
    )
    assert result.get("type") == data_entry_flow.FlowResultType.FORM
    assert result.get("errors") == {CONF_SITE_ID: "invalid_api_key"}

    # test with ConnectionTimeout
    test_api.get_details.side_effect = ConnectTimeout()
    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USER},
        data={
            CONF_NAME: NAME,
            CONF_API_KEY: API_KEY,
            CONF_SITE_ID: SITE_ID
        },
    )
    assert result.get("type") == data_entry_flow.FlowResultType.FORM
    assert result.get("errors") == {CONF_SITE_ID: "could_not_connect"}

    # test with HTTPError
    test_api.get_details.side_effect = HTTPError()
    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": SOURCE_USER},
        data={
            CONF_NAME: NAME,
            CONF_API_KEY: API_KEY,
            CONF_SITE_ID: SITE_ID
        },
    )
    assert result.get("type") == data_entry_flow.FlowResultType.FORM
    assert result.get("errors") == {CONF_SITE_ID: "could_not_connect"}
コード例 #12
0
    def test_connect_timeout_exception(self, requests):
        requests.post.side_effect = ConnectTimeout()

        with self.assertRaises(TimeoutException):
            self.http_client.post(url="http://test.com/",
                                  headers=None,
                                  payload=None,
                                  timeout=1000)
コード例 #13
0
def QA_fetch_symbol():
    url = urljoin(Binance_base_url, "/api/v1/exchangeInfo")
    try:
        req = requests.get(url, timeout=TIMEOUT)
    except ConnectTimeout:
        raise ConnectTimeout(MESSAGE_NOTICE)
    body = json.loads(req.content)
    return body["symbols"]
コード例 #14
0
def QA_fetch_binance_symbols():
    url = urljoin(Binance_base_url, "/api/v1/exchangeInfo")
    try:
        req = requests.get(url, timeout=TIMEOUT, proxies=proxies)
    except ConnectTimeout:
        raise ConnectTimeout(ILOVECHINA)
    body = json.loads(req.content)
    return body["symbols"]
コード例 #15
0
 def test_wdsl_client_error(self, client_mock):
     client_mock.side_effect = ConnectTimeout()
     task = Mock(retry=RetryExc)
     with patch(
             "treasury.api_requests.get_exponential_request_retry_countdown",
             Mock()):
         with self.assertRaises(RetryExc):
             get_wsdl_client(task)
コード例 #16
0
ファイル: pyrebase.py プロジェクト: shonuma/Pyrebase
def raise_detailed_error(request_object):
    try:
        request_object.raise_for_status()
    except HTTPError as e:
        # raise detailed error message
        # TODO: Check if we get a { "error" : "Permission denied." } and handle automatically
        raise HTTPError(e, request_object.text)
    except ConnectTimeout as e:
        raise ConnectTimeout(e, request_object.text)
コード例 #17
0
    def test_connect_timeout(self):
        responses.add(responses.GET,
                      'https://api.invoiced.com/invoices',
                      body=ConnectTimeout('Something went wrong'))

        client = invoiced.Client('test')

        with self.assertRaises(invoiced.errors.ApiConnectionError):
            client.request("GET", "/invoices")
コード例 #18
0
def get_player_links(team: str):
    url = "http://nfl.com/teams/roster"
    load = {'team': team}
    try:
        response = requests.get(url, load, timeout=10)
        player_links = extract_links(response.text)
        return player_links
    except ConnectTimeout:
        raise ConnectTimeout(f"Connection to {response.url} timed out!")
    def test_timeout_exception_is_not_propagated_for_create_or_update(self):
        dm_mailchimp_client = DMMailChimpClient('username', DUMMY_MAILCHIMP_API_KEY, logging.getLogger('mailchimp'))
        with mock.patch.object(
                dm_mailchimp_client._client.lists.members, 'create_or_update', autospec=True) as create_or_update:
            create_or_update.side_effect = ConnectTimeout()

            assert dm_mailchimp_client.subscribe_new_email_to_list('a_list_id', '*****@*****.**') == \
                {"status": "error", "error_type": "unexpected_error", "status_code": 500}
            assert create_or_update.called is True
コード例 #20
0
ファイル: test_providers.py プロジェクト: lucassimon/hub-cep
    def test_raises_connection_timeout_on_request(self, requests_mock):
        client = Viacep(ZIPCODE)
        requests_mock._adapter.register_uri('GET',
                                            TestViacep.FAKE_URL,
                                            exc=ConnectTimeout('Timeout'))
        error, info, res = client.call(TestViacep.FAKE_URL)

        assert error is True
        assert info == {'error': True, 'timeout': True, 'message': 'Timeout'}
        assert res is None
コード例 #21
0
    def test_init_timeout(self, session_patch, request_patch, client_patch):
        session = session_patch.return_value
        session.send = Mock(side_effect=ConnectTimeout())

        with self.assertRaises(TimedOutRequestError):
            Connection('SERVICE', 'HOST', None, 'CLIENT', 'USER', 'PASSWORD',
                       False, True)

        request_patch.assert_called_once()
        client_patch.assert_not_called()
コード例 #22
0
def QA_fetch_bitmex_symbols(active=False):
    if active:
        url = urljoin(Bitmex_base_url, "instrument/active")
    else:
        url = urljoin(Bitmex_base_url, "instrument")
    try:
        req = requests.get(url, params={"count": 500}, timeout=TIMEOUT)
    except ConnectTimeout:
        raise ConnectTimeout(ILOVECHINA)
    body = json.loads(req.content)
    return body
コード例 #23
0
def monkeypatch_test_client_connect_timeout_exception(
    self,
    method=None,
    url=None,
    headers=None,
    params=None,
    stream=False,
    proxies=None,
    timeout=None,
):
    raise ConnectTimeout("Test Connect Timeout Error")
コード例 #24
0
def QA_fetch_bitmex_symbols(active=False):
    if active:
        url = '%s/instrument/active' % Bitmex_base_url
    else:
        url = '%s/instrument' % Bitmex_base_url
    try:
        req = requests.get(url, timeout=TIMEOUT, proxies=proxies)
    except ConnectTimeout:
        raise ConnectTimeout('Bitmex connect timeout when getting symbols.')
    body = json.loads(req.content)
    return body
コード例 #25
0
    def test_shutdown_gateway_error_on_connection_timeout(self):
        responses.add(
            responses.POST,
            TEST_SUPERVISOR_SHUTDOWN_URL,
            body=ConnectTimeout('Timout trying to make connection')
        )

        bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY)

        with self.assertRaises(Exception) as exp:
            bs.shutdown()

        assert str(exp.exception) == 'supervisor API not accessible'
コード例 #26
0
    def test_device_status_error_on_connection_timeout(self):
        responses.add(
            responses.GET,
            TEST_SUPERVISOR_DEVICE_STATUS_URL,
            body=ConnectTimeout('Timout trying to make connection')
        )

        bs = BalenaSupervisor(TEST_SUPERVISOR_ADDRESS, TEST_SUPERVISOR_API_KEY)

        with self.assertRaises(RuntimeError) as exp:
            bs.get_device_status('appState')

        assert str(exp.exception).startswith("Device status request failed")
コード例 #27
0
ファイル: urlutils.py プロジェクト: willzhang05/cslbot
def get_short(msg, key):
    if len(msg) < 20:
        return msg
    try:
        data = post('https://www.googleapis.com/urlshortener/v1/url', params={'key': key}, data=json.dumps({'longUrl': msg}),
                    headers={'Content-Type': 'application/json'}, timeout=10).json()
    except ConnectTimeout as e:
        # Sanitize the error before throwing it
        raise ConnectTimeout(re.sub('key=.*', 'key=<removed>', str(e)))
    if 'error' in data:
        return msg
    else:
        return data['id']
コード例 #28
0
    def test_rapid_pro_api_server_down_is_logged(self, mock_requests):
        sent_message_obj = SentMessage(**self.bulk_sms_to_post).save()
        network_error = 'rapid-pro server not responding in time'
        mock_requests.post.side_effect = ConnectTimeout(network_error)

        send_bulk_sms(sent_message_obj)

        self.assertTrue(
            mock_requests.post.called_once_with(
                API_URL, json.dumps(self.bulk_sms_to_post), self.headers))

        retrieved_sms = SentMessage.objects(**self.bulk_sms_to_post)
        self.assertEqual(1, retrieved_sms.count())
        self.assertEqual("ConnectTimeout: %s" % network_error,
                         retrieved_sms[0].log)
コード例 #29
0
    def test_handle_exception(self):
        tests = [(RequestException(), [''], "7", "7"),
                 (ConnectTimeout(), [''], "7", "7"),
                 (RequestsConnectionError(), [''], "7", "7"),
                 (Exception("Any other exception"),
                  ["Any other exception"],
                  "8 Any other exception", "8"
                  ),
                 ]

        for exception, exp_data, exp_english, exp_code in tests:
            self.check_st_exception(ConnectionError,
                                    exp_data, exp_english, exp_code,
                                    self.http_client._handle_exception,
                                    func_args=(exception,))
コード例 #30
0
 async def _open_connection(self, resource, timeout=None, **kwargs):
     scheme, host, port = resource.key
     sock_co = curio.open_connection(
         host=host,
         port=port,
         **kwargs
     )
     if timeout is not None:
         sock_co = curio.timeout_after(timeout, sock_co)
     try:
         sock = await sock_co
     except curio.TaskTimeout as ex:
         raise ConnectTimeout(str(ex)) from None
     conn = Connection(self._pool, resource, sock)
     resource.connection = conn  # bind resource & connection
     return conn