-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example_api_events.py): Add example for new events endpoint
Provide example how to use new events endpoint. Signed-off-by: Denys Fedoryshchenko <[email protected]>
- Loading branch information
1 parent
253ddb2
commit ba703e7
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |