thbkrkr
11/30/2016 - 10:43 AM

Simple Swift HTTP API wrapper

Simple Swift HTTP API wrapper

#!/bin/bash -eu
#
# Simple Swift HTTP API wrapper
#

AUTH_TOKEN=??
CONTAINER_URL=https://??
CONTAINER_NAME=??

help() {
echo 'Usage: o42 COMMAND

Commands:
  ls        List all files
  put       Upload a file
  get       Get a file
  rm        Delete a file
'
}

_api() {
  declare uri=${1:-""} && shift
  curl \
    -H "X-Auth-Token: $AUTH_TOKEN" \
    $CONTAINER_URL/$CONTAINER_NAME$uri $@ \
    -w "\n{\"status\":%{response_code},\"time\":\"%{time_total}s\"}\n";
}

main() {
  declare cmd=${1:-help} && shift
  case $cmd in
    ls)    _api "/" -XGET        ;;
    put)   _api "/" -XPUT -T $1  ;;
    get)   _api "/$1"            ;;
    rm)    _api "/$1" -XDELETE   ;;

    *) help ;;
  esac
}

main "$@"