Exemple #1
0
def test_connection_keepalive(simple_wsgi_server):
    """Test the connection keepalive works (duh)."""
    session = Session(base_url=simple_wsgi_server['url'])
    pooled = requests.adapters.HTTPAdapter(
        pool_connections=1, pool_maxsize=1000,
    )
    session.mount('http://', pooled)

    def do_request():
        with ExceptionTrap(requests.exceptions.ConnectionError) as trap:
            resp = session.get('info')
            resp.raise_for_status()
        print_tb(trap.tb)
        return bool(trap)

    with ThreadPoolExecutor(max_workers=10 if IS_SLOW_ENV else 50) as pool:
        tasks = [
            pool.submit(do_request)
            for n in range(250 if IS_SLOW_ENV else 1000)
        ]
        failures = sum(task.result() for task in tasks)

    session.close()
    assert not failures
Exemple #2
0
class Client:
    """ cvr.dev API client.

    Contains a dedicated client for the /cvr/ part of the API.
    See https://docs.cvr.dev/#ra-cvr-data for more info.
    """
    def __init__(self, api_key):
        self._session = BaseUrlSession(_BASE_URL)
        self._session.headers.update({'Authorization': api_key})

        self.cvr = CVRClient(self._session)

    def test_api_key(self):
        resp = self._session.get('test/apikey')
        handle_response_status_code(resp)

    def close(self):
        self._session.close()

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.close()