Last active
August 29, 2015 14:05
-
-
Save trscavo/7506b85ccae6292d7430 to your computer and use it in GitHub Desktop.
Bash function to construct a request URL per the Metadata Query Protocol specification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Construct a request URL per the MDQ Protocol specification | |
# See: https://github.com/iay/md-query | |
# To construct a reference to ALL entities served by the | |
# metadata query server, simply omit the second argument | |
construct_mdq_url () { | |
# construct_mdq_url <base_url> <url_encoded_id> | |
# make sure there are one or two command-line arguments | |
if [ $# -lt 1 -o $# -gt 2 ]; then | |
echo "ERROR: $FUNCNAME: incorrect number of arguments: $# (1 or 2 required)" >&2 | |
return 2 | |
fi | |
local base_url=$1 | |
# strip the trailing slash from the base URL if necessary | |
local length="${#1}" | |
if [[ "${base_url:length-1:1}" == '/' ]]; then | |
base_url="${base_url:0:length-1}" | |
fi | |
# append the identifier if there is one | |
if [ $# -eq 2 ]; then | |
echo "${base_url}/entities/$2" | |
else | |
echo "${base_url}/entities" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment