Skip to content

Instantly share code, notes, and snippets.

@qmacro
Created July 12, 2024 08:07
Show Gist options
  • Save qmacro/726765c8cbe18d023487e45a3264bad0 to your computer and use it in GitHub Desktop.
Save qmacro/726765c8cbe18d023487e45a3264bad0 to your computer and use it in GitHub Desktop.
request-test script for the July Developer Challenge
#!/usr/bin/env bash
set -eo pipefail
# Tester options (empty by default), possible values: nolog
declare TESTER_OPTIONS="${TESTER_OPTIONS:-}"
# Tester service is <tester-service-base>/tester
declare TESTER_SERVICE_BASE="${TESTER_SERVICE_BASE:-https://developer-challenge-2024-07.cfapps.eu10.hana.ondemand.com}"
declare TESTER_SERVICE="$TESTER_SERVICE_BASE/tester"
# Candidate service is <candidate-service-base>/<servicepath>
declare CANDIDATE_SERVICE_BASE="${CANDIDATE_SERVICE_BASE:-http://localhost:8000}"
usage() {
cat <<EOF
Usage:
request-test <communityid> <servicepath> <task>
Examples:
request-test qmacro /basic basic-sum
request-test qmacro /rest/plain plain-highestValue
request-test qmacro /odata/v4/northbreeze northbreeze-selectProduct
Env vars:
TESTER_SERVICE_BASE defaults to to https://developer-challenge-2024-07.cfapps.eu10.hana.ondemand.com
CANDIDATE_SERVICE_BASE should be set, defaults to http://localhost:8000
TESTER_OPTIONS is empty by default, possible values include: nolog
EOF
}
makeRequest() {
local communityid=$1
local servicepath=$2
local task=$3
jq \
--compact-output \
--null-input \
--arg communityid "$communityid" \
--arg servicebase "$CANDIDATE_SERVICE_BASE" \
--arg servicepath "$servicepath" \
--arg task "$task" \
'{ communityid: $communityid | ascii_downcase, serviceurl: [$servicebase | rtrimstr("/"), $servicepath | ltrimstr("/")] | join("/"), task: $task }' \
| curl \
--data @- \
--header 'Content-Type: application/json' \
--header "X-TESTER-OPTIONS: $TESTER_OPTIONS" \
--silent \
--url "$TESTER_SERVICE/testServer"
}
main() {
if [[ $# -ne 3 ]]; then
usage
exit 1
fi
makeRequest "$@"
}
main "$@"
@andrew-barnard
Copy link

andrew-barnard commented Jul 14, 2024

I wondered how you would approach the arguments for curl and didn't expect to see the usage of jq. Lots of things in this script that could be unpacked - the modularisation into functions, the use of :- in the declares, the localisation of the arguments, and the usage of jq. And of course, the header field X-TESTER-OPTIONS - which I'm guessing is interpreted by your custom CAP handler to control logging. Thanks for sharing.

@qmacro
Copy link
Author

qmacro commented Jul 15, 2024

You're welcome!

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