wmo-im / wis2downloader

The backend Python package for downloading real-time data from the WIS2 network.
Apache License 2.0
0 stars 0 forks source link

add wis2downloader CLI to list, add and remove subscriptions #22

Open maaikelimper opened 2 weeks ago

maaikelimper commented 2 weeks ago

To allow users to list, add and remove subscriptions without making curl-commands, can we please add CLI commands ?

here is the code I removed from wis2box:

import logging

import click
import requests

from wis2box import cli_helpers

LOGGER = logging.getLogger(__name__)

DOWNLOAD_URL = 'http://wis2downloader:5000/'

@click.group()
def downloader():
  """Interact with the wis2downloader"""
  pass

@click.command('list-subscriptions')
@click.pass_context
@cli_helpers.OPTION_VERBOSITY
def list_subscriptions(ctx, verbosity):
  """list all subscriptions"""

  # make a GET requests to http://{DOWNLOAD_URL}/subscriptions
  try:
    response = requests.get(f'{DOWNLOAD_URL}/subscriptions')
    # check response status
    if response.status_code == 200:
      click.echo('Current subscriptions:')
      click.echo(response.text)
    else:
      click.echo(f'Error: {response.status_code}')
      click.echo(response.text)
  except requests.exceptions.ConnectionError:
    click.echo('Error: Connection refused')
    click.echo(f'Is the wis2downloader running on {DOWNLOAD_URL}?')

@click.command('add-subscription')
@click.pass_context
@click.option('--topic', '-t', help='The topic to subscribe to', required=True)
def add_subscription(ctx, topic):
  """add a subscription"""

  # make a POST request to http://{DOWNLOAD_URL}/subscriptions
  try:
    response = requests.post(f'{DOWNLOAD_URL}/subscriptions',
                 json={'topic': topic})
    # check response status
    if response.status_code == 201:
      click.echo('Subscription added')
      click.echo('Added subscription:')
      click.echo(response.text)
    else:
      click.echo('Subscription not added')
      click.echo(f'Error: {response.status_code}')
      click.echo(response.text)
  except requests.exceptions.ConnectionError:
    click.echo('Error: Connection refused')
    click.echo(f'Is the wis2downloader running on {DOWNLOAD_URL}?')

@click.command('remove-subscription')
@click.pass_context
@click.option('--topic', '-t', help='The topic to subscribe to', required=True)
def remove_subscription(ctx, topic):
  """remove a subscription"""

  topic = topic.replace('#', '%23')
  topic = topic.replace('+', '%2B')
  # make a DELETE request to http://{DOWNLOAD_URL}/subscriptions
  try:
    response = requests.delete(f'{DOWNLOAD_URL}/subscriptions/{topic}') # noqa
    # check response status
    if response.status_code == 200:
      click.echo('Subscription deleted')
      click.echo('Current subscriptions:')
      click.echo(response.text)
    else:
      click.echo('Subscription not deleted')
      click.echo(f'Error: {response.status_code}')
      click.echo(response.text)
  except requests.exceptions.ConnectionError:
    click.echo('Error: Connection refused')
    click.echo(f'Is the wis2downloader running on {DOWNLOAD_URL}?')

downloader.add_command(list_subscriptions)
downloader.add_command(add_subscription)
downloader.add_command(remove_subscription)