OvhRequestApi() enhancements

- OVHAPI_HTTP_STATUS always return a numeric value
- raise error 500 with help message if OVHAPI_HTTP_RESPONSE has many lines
- update samples scripts with OvhRequestApi()
pull/5/head
Didier BONNEFOI 2017-07-19 11:45:54 +02:00
parent 8377493d0a
commit ae35740057
5 changed files with 30 additions and 21 deletions

View File

@ -161,7 +161,7 @@ _JSonSH_rewrite_output()
}
#
# return JSON keys
# print JSON keys
#
# usage : getJSONKeys
#
@ -176,12 +176,9 @@ getJSONKeys()
}
#
# return the value for a defined filed
# print the value for a defined field
#
# if field is a string, it can be set with/without double quotes
# if the result is between double quotes, only get the value inside
#
# usage : getJSONValue "json" field
# usage : getJSONValue field
#
getJSONValue()
{

View File

@ -4,7 +4,8 @@ readonly OVHAPI_BASHCLIENT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}")/.." && pwd)
readonly OVHAPI_BASHCLIENT_BIN="${OVHAPI_BASHCLIENT_DIR}/ovh-api-bash-client.sh"
readonly OVHAPI_BASHCLIENT_CONTRIB_DIR="${OVHAPI_BASHCLIENT_DIR}/contrib"
. "${OVHAPI_BASHCLIENT_DIR}/contrib/jsonsh-lib.sh" || exit 1
JSONSH_DIR="${OVHAPI_BASHCLIENT_DIR}/libs/"
. "${OVHAPI_BASHCLIENT_CONTRIB_DIR}/jsonsh-lib.sh" || exit 1
OVHAPI_HTTP_STATUS=
OVHAPI_HTTP_RESPONSE=
@ -43,13 +44,14 @@ OvhRequestApi()
local data=$3
local client_response=
local cmd_profile=
local cmd=(${OVHAPI_BASHCLIENT_BIN})
## construct arguments array
if [ -n "${OVHAPI_BASHCLIENT_PROFILE}" ]; then
cmd+=(--profile ${OVHAPI_BASHCLIENT_PROFILE})
fi
cmd_profile=${cmd[*]}
if [ -n "${url}" ]; then
cmd+=(--url ${url})
@ -77,6 +79,14 @@ OvhRequestApi()
OVHAPI_HTTP_STATUS=$(echo "${client_response}" | cut -d ' ' -f1)
OVHAPI_HTTP_RESPONSE="$(echo "${client_response}" | cut -d ' ' -f2-)"
# catch profile error
if [[ ! ${OVHAPI_HTTP_STATUS} =~ ^[0-9]+$ ]] && [[ ${OVHAPI_HTTP_RESPONSE} == *$'\n'* ]]; then
OVHAPI_HTTP_STATUS=500
OVHAPI_HTTP_RESPONSE=$(cat <<EOF
["more than one line returned, check your profile : ${cmd_profile}"]
EOF
)
fi
_ovhapilib_echo_debug "http_status=${OVHAPI_HTTP_STATUS}"
# forward result to JSON.sh to be usable with JSONSH functions

View File

@ -4,9 +4,9 @@ source ${HERE}/../ovh-api-lib.sh || exit 1
OvhRequestApi /me
if [ "${OVHAPI_HTTP_STATUS}" != "200" ]; then
if [ ${OVHAPI_HTTP_STATUS} -ne 200 ]; then
echo "profile error:"
echo "${OVHAPI_HTTP_RESPONSE}"
getJSONValues
exit
fi
@ -29,4 +29,6 @@ if [ "${OVHAPI_HTTP_STATUS}" -eq 200 ]; then
echo "-- get all values --"
getJSONValues
done
else
getJSONValues
fi

View File

@ -4,15 +4,15 @@ source ${HERE}/../ovh-api-lib.sh || exit 1
OvhRequestApi /me
if [ "${OVHAPI_HTTP_STATUS}" != "200" ]; then
if [ ${OVHAPI_HTTP_STATUS} -ne 200 ]; then
echo "profile error:"
echo "${OVHAPI_HTTP_RESPONSE}"
exit
else
echo "-- all fields --"
getJSONValues
echo "-- only some fields --"
getJSONValue "email"
getJSONValue "currency.code"
getJSONValue "currency.symbol"
exit
fi
echo "-- all fields --"
getJSONValues
echo "-- only some fields --"
getJSONValue "email"
getJSONValue "currency.code"
getJSONValue "currency.symbol"

View File

@ -4,9 +4,9 @@ source ${HERE}/../ovh-api-lib.sh || exit 1
OvhRequestApi /me
if [ "${OVHAPI_HTTP_STATUS}" != "200" ]; then
if [ ${OVHAPI_HTTP_STATUS} -ne 200 ]; then
echo "profile error:"
echo "${OVHAPI_HTTP_RESPONSE}"
getJSONValues
exit
fi