Skip to content

Instantly share code, notes, and snippets.

@tophyr
Created April 23, 2014 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tophyr/11199362 to your computer and use it in GitHub Desktop.
Save tophyr/11199362 to your computer and use it in GitHub Desktop.
dsc
#!/bin/bash
dsc $1 $2 $($(dirname $0)/dsc_local) $3 put json "$4"
#!/bin/bash
if [ "$1" = "usage" ]; then
echo "dsc <username> <password> <host (incl protocol)> <endpoint> [<method> [<content type> [<data>]]]"
echo
echo "host must include either the http:// or https:// and a full domain name."
echo "endpoint is everything else in the url, except restapi/v2/ (ex: /accounts/2/connect/mobile_notifiers)"
echo "method is {get|put|post|delete} (case insensitive)"
echo "content type is {raw|xml|json}. if valid content type is not specified, data will not be sent."
echo "data is any data you'd like to send. currently does not support streaming from stdin; this is a TODO"
exit
fi
USERNAME=$1
PASSWORD=$2
AUTH="-H \"X-Authentication:<censored>\""
HOST=$3
ENDPOINT=$4
METHOD=$(echo $5 | tr '[:lower:]' '[:upper:]')
CURL=""
if [ ! -z "$7" ]; then
DATA="--data-binary \"$7\""
else
DATA=
fi
if [ "$6" == "raw" ]; then
TYPE="-H \"Content-Type: application/octet-stream\""
POSTPROCESS=
elif [ "$6" == "xml" ]; then
TYPE="-H \"Content-Type: application/xml\""
POSTPROCESS=
elif [ "$6" == "json" ]; then
TYPE="-H \"Content-Type: application/json\""
POSTPROCESS=" | python -mjson.tool"
else
echo Unrecognized or no content type specified.
TYPE=
POSTPROCESS=
if [ ! -z "${DATA}" ]; then
echo Disallowing data send.
DATA=
fi
fi
CURL="curl -v ${AUTH} -X ${METHOD:-GET} ${HOST}/restapi/v2/${ENDPOINT} ${TYPE} ${DATA} 2> >(grep -e '^[<>]' 1>&2) ${POSTPROCESS}"
eval ${CURL}
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment