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 31, 2024
1 parent 253ddb2 commit 8cb559a
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 example will retrieve the latest events from the KernelCI API
and print them to the console.
It will filter only completed kernel builds, limit to 100 events per request,
and retrieve corresponding nodes with artifacts.
"""
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 8cb559a

Please sign in to comment.