Skip to content

Commit

Permalink
feat(example_api_events.py): Add example for new events endpoint
Browse files Browse the repository at this point in the history
Provide example how to use new events endpoint.

Signed-off-by: Denys Fedoryshchenko <[email protected]>
  • Loading branch information
nuclearcat committed Oct 30, 2024
1 parent 253ddb2 commit ba703e7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tools/example_api_events.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit ba703e7

Please sign in to comment.