From ba703e74ebbaf1b9945b4650aadd64477b2f5466 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Wed, 30 Oct 2024 22:17:14 +0200 Subject: [PATCH] feat(example_api_events.py): Add example for new events endpoint Provide example how to use new events endpoint. Signed-off-by: Denys Fedoryshchenko --- tools/example_api_events.py | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 tools/example_api_events.py diff --git a/tools/example_api_events.py b/tools/example_api_events.py new file mode 100755 index 000000000..757309847 --- /dev/null +++ b/tools/example_api_events.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +""" +This is simplified example how to subscribe to kernelci.org pubsub +It uses long polling to get events from the server +It is not recommended to use this in production, as it is not efficient, +and in production it is preferable to use cloudevents library +""" +import requests +import json +import sys +import time + + +# This is staging server: "https://staging.kernelci.org:9000/latest" +# For production use "https://kernelci-api.westus3.cloudapp.azure.com/latest/" +BASE_URI = "https://staging.kernelci.org:9000/latest" +EVENTS_PATH = "/events" + +# timestamp format '2024-10-30T00:32:57.537000' +timestamp = "1970-01-01T00:00:00.000000" +#timestamp = "0" + +def pollevents(timestamp): + url = BASE_URI + EVENTS_PATH + f"?kind=kbuild&state=done&limit=100&recursive=true&from={timestamp}" + print(url) + response = requests.get(url) + response.raise_for_status() + return response.json() + + +def main(): + global timestamp + while True: + try: + events = pollevents(timestamp) + if len(events) == 0: + print("No new events, sleeping for 30 seconds") + time.sleep(30) + continue + print(f"Got {len(events)} events") + for event in events: + print(json.dumps(event, indent=2)) + timestamp = event["timestamp"] + except requests.exceptions.RequestException as e: + print(f"Error: {e}") + sys.exit(1) + + +if __name__ == "__main__": + main()