Skip to content

Instantly share code, notes, and snippets.

@dlhaines
Last active February 7, 2018 21:40
Show Gist options
  • Save dlhaines/f1984b5294f4f171416e430298fe967a to your computer and use it in GitHub Desktop.
Save dlhaines/f1984b5294f4f171416e430298fe967a to your computer and use it in GitHub Desktop.
Bash scripts to run API get requests with IBM Api manager and WSO2 ESB.

Scripts to make it easy to ask authenticated GET queries of an ESB with the IBM api manager and WSO2 backend.

These scripts assume input is a GET request and output is in json format. It uses jq to format the output.

authentication - The scripts expect to find credentials in the file credentials in the current directory. Use symbolic links to keep multiple different credential files in the same directory. See the file credentials.TEMPLATE for more information.

  • getApiToken.sh - Use the credentials to get an up-to-date access token. Just append it to the credentials file to make the token available to the run script. E.g.
             ./getApiToken.sh >> credentials
  • runApiRequest.sh - Run a query using the current credentials. The command line parameter is the tail end of the url request. The prefix for the url can be stored in the credentials file.
# Credentials for accessing IBM api mananger / WSO2 ESB combination.
# API calls will require a (temporary) valid access token. If you have
# one in hand put it here to make it easily reusable. If you don't
# have a valid token use getApiToken.sh to generate one.
NAME="QA API"
# URL for token renewal server.
TOKEN_URL=<token renewal url>
# These are persistent values provided by the API manager when you
# sign up to access a particular API.
CLIENT_SECRET=<secret>
CLIENT_ID=<id or key>
SCOPE=<scope>
# Prefix of the url for the API call. The tail of the url will be
# provided on the testCanvasTLAdmin.sh command line.
URL_PREFIX=https://apigw-tst.it.umich.edu/um/aa/CanvasTLAdmin/
########### new access tokens can be appended to this file.
ACCESS_TOKEN=<CoolToken>
#!/bin/bash
# Get and print new access token based on current credentials.
# Can be appended to the current credentials to override prior tokens.
# Very minimal error handling.
# Require jq to format json output.
if ! hash jq 2>/dev/null; then
echo "$0: requires jq (or temporarily removing jq from script)"
exit 1;
fi
# load authorization information.
source ./credentials
#set -x
set -e
DATA="grant_type=client_credentials&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&scope=${SCOPE}"
curl -s \
--request POST \
--url ${TOKEN_URL} \
--header 'accept: application/json' \
--header 'content-type: application/x-www-form-urlencoded' \
--data ${DATA} \
| jq -r '. | .access_token' | perl -p -e "print \"ACCESS_TOKEN=$_\" "
#end
#!/bin/bash
# Run an api query using WSO2 API and IBM API Manager then format the
# result. See sample "credentials" file for the required information.
# Use the sibling script getApiToken.sh to get a new token if the
# current one has expired. E.g. ./getApiToken.sh >> credentials
# Require jq for formatting json output.
if ! hash jq 2>/dev/null; then
echo "$0: requires jq (or temporarily removing jq from script)"
exit 1;
fi
# Load the authorization information.
source ./credentials
# Build the query url based on base url and command line argument.
URL_SUFFIX=${1:-NONSENSE}
URL="${URL_PREFIX}${URL_SUFFIX}"
echo "CREDENTIALS: ${NAME}"
echo "URL: ${URL}"
#set -x
set -e
# --globoff allows passing {} or [] in url without special quoting.
CURL_ARGS=" --silent --globoff "
# can be useful for debugging.
#CURL_ARGS=" -v "
R=$(\
curl ${CURL_ARGS} \
--request GET \
--url ${URL} \
--header 'accept: application/json' \
--header "authorization: Bearer ${ACCESS_TOKEN}" \
--header "x-ibm-client-id: ${CLIENT_ID}" \
)
echo $R | jq -r '.'
#end
@lsloan
Copy link

lsloan commented Feb 7, 2018

Nice! Since "README.md" appears as the title of your gist, you might want to rename it to something like "UMich-ESB-bash.md".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment