From 6a962ab0b1157c84590d90446ab5a29cda1e419f Mon Sep 17 00:00:00 2001 From: Jeny Sadadia Date: Thu, 4 Jan 2024 10:27:54 +0530 Subject: [PATCH] api: remove `client` module `api.client` was implemented as a command-line utility for sending and receiving Pub/Sub events. `kci` CLI tool has been implemented to use API in `kernelci-core`. The tool can be used to send all kinds of requests to API. Hence, `api.client` can be dropped and `kci` can be used instead. Signed-off-by: Jeny Sadadia --- api/client.py | 83 --------------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 api/client.py diff --git a/api/client.py b/api/client.py deleted file mode 100644 index e7827b17..00000000 --- a/api/client.py +++ /dev/null @@ -1,83 +0,0 @@ -import argparse -import os -import requests -import urllib.parse - -from cloudevents.http import CloudEvent, to_structured, from_json - -DEFAULT_URL = 'http://localhost:8000' - -TOKEN = os.getenv('TOKEN') - - -def publish(args): - attributes = { - "type": "api.kernelci.org", - "source": "https://api.kernelci.org/", - } - data = {"message": args.message} - event = CloudEvent(attributes, data) - headers, body = to_structured(event) - headers['Authorization'] = f"Bearer {TOKEN}" - path = '/'.join(['publish', args.channel]) - url = urllib.parse.urljoin(args.url, path) - res = requests.post(url, data=body, headers=headers) - res.raise_for_status() - - -def listen(args): - headers = { - 'Authorization': f"Bearer {TOKEN}", - } - path = '/'.join(['subscribe', args.channel]) - url = urllib.parse.urljoin(args.url, path) - res = requests.post(url, headers=headers) - res.raise_for_status() - - print(f"Listening for events on channel {args.channel}.") - print("Press Ctrl-C to stop.") - - try: - # Fetch subscription ID from /subscribe endpoint response - subscription_id = str(res.json()['id']) - # Pass subscription ID to /listen endpoint URL - listen_path = '/'.join(['listen', subscription_id]) - url = urllib.parse.urljoin(args.url, listen_path) - - while True: - res = requests.get(url, headers=headers) - res.raise_for_status() - json_data = res.json().get('data') - event = from_json(json_data) - print(f"Message: {event.data.get('message')}") - except Exception as e: - print(f"Error: {e}") - except KeyboardInterrupt as e: - print(f"Stopping.") - finally: - # Pass subscription ID to /unsubscribe endpoint URL - unsubscribe_path = '/'.join(['unsubscribe', subscription_id]) - url = urllib.parse.urljoin(args.url, unsubscribe_path) - res = requests.post(url, headers=headers) - res.raise_for_status() - - -if __name__ == '__main__': - parser = argparse.ArgumentParser("KernelCI API Client") - sub_parsers = parser.add_subparsers(title="Commands") - - def _add_args(sub_parser): - sub_parser.add_argument('channel', help="Channel name") - sub_parser.add_argument('--url', default=DEFAULT_URL, help="Base URL") - - parser_listen = sub_parsers.add_parser('listen', help="Listen to events") - parser_listen.set_defaults(func=listen) - _add_args(parser_listen) - - parser_publish = sub_parsers.add_parser('publish', help="Publish events") - parser_publish.set_defaults(func=publish) - _add_args(parser_publish) - parser_publish.add_argument('message', help="Message to send") - - args = parser.parse_args() - args.func(args)