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 # 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 # usage : getJSONValue field
# if the result is between double quotes, only get the value inside
#
# usage : getJSONValue "json" field
# #
getJSONValue() 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_BIN="${OVHAPI_BASHCLIENT_DIR}/ovh-api-bash-client.sh"
readonly OVHAPI_BASHCLIENT_CONTRIB_DIR="${OVHAPI_BASHCLIENT_DIR}/contrib" 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_STATUS=
OVHAPI_HTTP_RESPONSE= OVHAPI_HTTP_RESPONSE=
@ -43,13 +44,14 @@ OvhRequestApi()
local data=$3 local data=$3
local client_response= local client_response=
local cmd_profile=
local cmd=(${OVHAPI_BASHCLIENT_BIN}) local cmd=(${OVHAPI_BASHCLIENT_BIN})
## construct arguments array ## construct arguments array
if [ -n "${OVHAPI_BASHCLIENT_PROFILE}" ]; then if [ -n "${OVHAPI_BASHCLIENT_PROFILE}" ]; then
cmd+=(--profile ${OVHAPI_BASHCLIENT_PROFILE}) cmd+=(--profile ${OVHAPI_BASHCLIENT_PROFILE})
fi fi
cmd_profile=${cmd[*]}
if [ -n "${url}" ]; then if [ -n "${url}" ]; then
cmd+=(--url ${url}) cmd+=(--url ${url})
@ -77,6 +79,14 @@ OvhRequestApi()
OVHAPI_HTTP_STATUS=$(echo "${client_response}" | cut -d ' ' -f1) OVHAPI_HTTP_STATUS=$(echo "${client_response}" | cut -d ' ' -f1)
OVHAPI_HTTP_RESPONSE="$(echo "${client_response}" | cut -d ' ' -f2-)" 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}" _ovhapilib_echo_debug "http_status=${OVHAPI_HTTP_STATUS}"
# forward result to JSON.sh to be usable with JSONSH functions # 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 OvhRequestApi /me
if [ "${OVHAPI_HTTP_STATUS}" != "200" ]; then if [ ${OVHAPI_HTTP_STATUS} -ne 200 ]; then
echo "profile error:" echo "profile error:"
echo "${OVHAPI_HTTP_RESPONSE}" getJSONValues
exit exit
fi fi
@ -29,4 +29,6 @@ if [ "${OVHAPI_HTTP_STATUS}" -eq 200 ]; then
echo "-- get all values --" echo "-- get all values --"
getJSONValues getJSONValues
done done
else
getJSONValues
fi fi

View File

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