from datetime import datetime


async def do_request(client, url, headers, params=None):
    """Run request to `url` with the provided `params` and `headers`."""

    def requester(url):
        if params:
            return client.post(url, headers=headers, json=params)
        else:
            return client.get(url, headers=headers)

    start = datetime.utcnow()
    async with requester(url) as resp:
        end = datetime.utcnow()
        assert resp.status == 200, f"{url} - {await resp.text()}"
        out = await resp.json()

        timing = (end - start).total_seconds()
        headers = resp.headers

        perf_headers = {
            k: v for k, v in headers.items() if k.startswith("X-Performance")
        }

        return (
            out,
            {
                "url": url,
                "headers": perf_headers,
                "timing": timing,
                "start": start,
                "end": end,
                "params": params,
            },
        )