Skip to content

Instantly share code, notes, and snippets.

@trscavo
Last active August 29, 2015 14:05
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 trscavo/7506b85ccae6292d7430 to your computer and use it in GitHub Desktop.
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
# 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