Пример #1
0
def test_generic_500_failure(mock_req):
    mock_req.get(client_add_api_key(base_url), status_code=500)
    with pytest.raises(PermanentException):
        call(client_add_api_key(base_url))

    mock_req.get(server_add_api_key(base_url), status_code=500)
    with pytest.raises(PermanentException):
        call(server_add_api_key(base_url))
Пример #2
0
def test_invalid_key_gives_permanent_exception(mock_req):
    mock_req.get(client_add_api_key(base_url), status_code=403)
    with pytest.raises(PermanentException):
        call(client_add_api_key(base_url))

    mock_req.get(server_add_api_key(base_url), status_code=403)
    with pytest.raises(PermanentException):
        call(server_add_api_key(base_url))
Пример #3
0
def test_api_count_zero(mock_req):
    mock_req.get(client_add_api_key(base_url), status_code=429)
    with pytest.raises(ApiCountZeroException):
        call(client_add_api_key(base_url))

    mock_req.get(server_add_api_key(base_url), status_code=429)
    with pytest.raises(ApiCountZeroException):
        call(server_add_api_key(base_url))
Пример #4
0
def test_exception_thrown(mock_req):
    mock_req.get(client_add_api_key(base_url), status_code=300)
    with pytest.raises(TemporaryException):
        call(client_add_api_key(base_url))

    mock_req.get(server_add_api_key(base_url), status_code=300)
    with pytest.raises(TemporaryException):
        call(server_add_api_key(base_url))
Пример #5
0
def api_call_manager(url):
    """
    If there were no errors in making an API call, get the result
    If a Temporary error occurred, sleep for 5 minutes and try again.
    Do this 50 times, and if it continues to fail, raise a CallFailException
    If a Permanent error occurs, raise a CallFailException
    If the user's ApiCount is zero, sleep for one hour to refresh the calls
    :param url: the url that will be used to make the API call
    :return: returns the resulting information of the documents
    """

    pause = 0
    while pause < 51:
        try:
            result = call(url)
            return result
        except TemporaryException:
            logger.error('API call Error, waiting 5 minutes')
            time.sleep(300)
            pause += 1
        except PermanentException:
            logger.error('API call Error')
            break
        except ApiCountZeroException:
            logger.warning('API calls exhausted')
            time.sleep(3600)
    logger.error('API call failed')
    raise CallFailException
Пример #6
0
def test_happy_path(mock_req):
    mock_req.get(client_add_api_key(base_url), status_code=200, text='{}')
    assert call(client_add_api_key(base_url)).text == '{}'

    mock_req.get(server_add_api_key(base_url), status_code=200, text='{}')
    assert call(client_add_api_key(base_url)).text == '{}'