def __init__(self, app): self._app = app self._session = http.Client() self._handled_events = queue.Queue() self._counter = 0 self._redis = redis.StrictRedis.from_url(config.STREAM_URL, decode_responses=True)
def get_repositories_setuped( token: str, install_id: int ) -> typing.List[github_types.GitHubRepository]: # pragma: no cover repositories = [] url = f"{config.GITHUB_API_URL}/user/installations/{install_id}/repositories" token = f"token {token}" session = http.Client() while True: response = session.get( url, headers={ "Authorization": token, "Accept": "application/vnd.github.machine-man-preview+json", "User-Agent": "PyGithub/Python", }, ) if response.status_code == 200: repositories.extend(response.json()["repositories"]) if "next" in response.links: url = response.links["next"]["url"] continue else: return repositories else: response.raise_for_status()
def run(): parser = argparse.ArgumentParser() parser.add_argument("--clean", action="store_true") parser.add_argument("--dest", default="http://localhost:8802/event") args = parser.parse_args() logs.setup_logging() session = http.Client() payload_data = os.urandom(250) payload_hmac = utils.compute_hmac(payload_data) if args.clean: # httpx doesn't allow data= here yet: https://github.com/encode/httpx/pull/900 r = session.request( "DELETE", "https://gh.mergify.io/events-testing", data=payload_data, headers={"X-Hub-Signature": "sha1=" + payload_hmac}, ) r.raise_for_status() while True: try: # httpx doesn't allow data= here yet: https://github.com/encode/httpx/pull/900 resp = session.request( "GET", "https://gh.mergify.io/events-testing", data=payload_data, headers={"X-Hub-Signature": "sha1=" + payload_hmac}, ) events = resp.json() for event in reversed(events): LOG.info("") LOG.info("==================================================") LOG.info( ">>> GOT EVENT: %s %s/%s", event["id"], event["type"], event["payload"].get("state", event["payload"].get("action")), ) data = json.dumps(event["payload"]) hmac = utils.compute_hmac(data.encode("utf8")) session.post( args.dest, headers={ "X-GitHub-Event": event["type"], "X-GitHub-Delivery": event["id"], "X-Hub-Signature": "sha1=%s" % hmac, "Content-type": "application/json", }, data=data, ) except Exception: LOG.error("event handling failure", exc_info=True) time.sleep(1)
def api_call(url, method="post"): data = os.urandom(250) hmac = utils.compute_hmac(data) with http.Client() as client: r = client.request(method, url, headers={"X-Hub-Signature": "sha1=" + hmac}, data=data) r.raise_for_status() print(r.text)
def test_client_HTTP_400(httpserver): httpserver.expect_oneshot_request("/").respond_with_json( {"message": "This is an 4XX error"}, status=400) with http.Client() as client: with pytest.raises(http.HTTPClientSideError) as exc_info: client.get(httpserver.url_for("/")) assert exc_info.value.message == "This is an 4XX error" assert exc_info.value.status_code == 400 assert exc_info.value.response.status_code == 400 assert str(exc_info.value.request.url) == httpserver.url_for("/") httpserver.check_assertions()
def test_client_HTTP_500(httpserver): httpserver.expect_request("/").respond_with_data("This is an 5XX error", status=500) with http.Client() as client: with pytest.raises(http.HTTPServerSideError) as exc_info: client.get(httpserver.url_for("/")) # 5 retries assert len(httpserver.log) == 5 assert exc_info.value.message == "This is an 5XX error" assert exc_info.value.status_code == 500 assert exc_info.value.response.status_code == 500 assert str(exc_info.value.request.url) == httpserver.url_for("/") httpserver.check_assertions()
def test_client_temporary_HTTP_500(httpserver): httpserver.expect_oneshot_request("/").respond_with_data( "This is an 5XX error", status=500) httpserver.expect_oneshot_request("/").respond_with_data( "This is an 5XX error", status=500) httpserver.expect_oneshot_request("/").respond_with_data( "This is an 5XX error", status=500) httpserver.expect_request("/").respond_with_data("It works now !", status=200) with http.Client() as client: client.get(httpserver.url_for("/")) # 4 retries assert len(httpserver.log) == 4 httpserver.check_assertions()
def _do_test_client_retry_429(httpserver, retry_after, expected_seconds): records = [] def record_date(_): records.append(datetime.datetime.utcnow()) return Response("It works now !", 200) httpserver.expect_oneshot_request("/").respond_with_data( "This is an 429 error", status=429, headers={"Retry-After": retry_after} ) httpserver.expect_request("/").respond_with_handler(record_date) with http.Client() as client: now = datetime.datetime.utcnow() client.get(httpserver.url_for("/")) assert len(httpserver.log) == 2 elapsed_seconds = (records[0] - now).total_seconds() assert expected_seconds - 1 < elapsed_seconds <= expected_seconds + 1 httpserver.check_assertions()
def _retrieve_subscription_from_db(installation_id): LOG.info("Subscription not cached, retrieving it...", install_id=installation_id) with http.Client() as client: try: resp = client.get( config.SUBSCRIPTION_URL % installation_id, auth=(config.OAUTH_CLIENT_ID, config.OAUTH_CLIENT_SECRET), ) except httpx.HTTPNotFound as e: reason = e.response.json().get("message") sub = { "tokens": {}, "subscription_active": False, "subscription_reason": reason, } else: sub = resp.json() sub["tokens"] = dict((login, token["access_token"]) for login, token in sub["tokens"].items()) return sub
def __init__(self, app): self._app = app self._session = http.Client() self._handled_events = queue.Queue() self._counter = 0
def test_client_connection_error(): with http.Client() as client: with pytest.raises(http.RequestError): client.get("http://localhost:12345")
def post(url, data, headers): with http.Client() as client: client.post(url, data=data.encode(), headers=headers)