#declaring function for GET request
api_get() {
status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token 123abc" "$1")
#check for condition
if [ "$status" = 200 ]; then
echo "Running successfully! $2 Status code: $status"
else
echo "Error! $2 Status code: $status"
fi
}
#running the function for each URL
api_get https://cms.abc.co/api/method/contentready_edu_cms.api.api_1 "api_1"
api_get https://cms.abc.co/api/method/contentready_edu_cms.api.api_2 "api_2"
api_get https://cms.abc.co/api/method/contentready_edu_cms.api.api_3 "api_3"
#declaring function for POST request
api_post() {
status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token 123abc" -H "Content-Type: application/json" -d "$3" -X POST $1)
#check for condition
if [ "$status" = 200 ]; then
echo "Running successfully! $2 Status code: $status"
else
echo "Error! $2 Status code: $status"
fi
}
#running the function for each URL
api_post https://cms.abc.co/api/method/contentready_edu_cms.api.api_1 "api_1" '{"some":"data1"}'
api_post https://cms.abc.co/api/method/contentready_edu_cms.api.api_2 "api_2" '{"some":"data2"}'
api_post https://cms.abc.co/api/method/contentready_edu_cms.api.api_3 "api_3" '{"some":"data3"}'
基本上代碼的前半部分有 GET 請求 url,后半部分有 POST 請求 url 和一些資料。我想知道如何才能減少這個腳本的重復性。
如何將 2 個if 陳述句合二為一?也可以將“api_get https://cms.abc.co/api/method/contentready_edu_cms.api”這三行(14-16)結合起來。因為唯一不同的是 api_1、api_2、api_3 和之后的 api 名稱(“api_1”、“api_2”、“api_3”)。如果不是 url,我們至少可以將它們全部放在一個api_get函式下嗎?
uj5u.com熱心網友回復:
如何將 2 個 if 陳述句合二為一?
?? 我在你的代碼中沒有看到這樣的東西
是否可以結合三行(14-16)
是的:
api_root=https://cms.abc.co/api/method/contentready_edu_cms.api
for api in api_{1,2,3}; do
api_get "${api_root}.${api}" "$api"
done
對于 POST 呼叫,考慮一個將 api 名稱映射到資料的陣列:
declare -A data=(
[api_1]='{"some":"data1"}'
[api_2]='{"some":"data2"}'
[api_3]='{"some":"data3"}'
)
for api in "${!data[@]}"; do
api_post "${api_root}.${api}" "$api" "${data[$api]}"
done
這兩個功能可以結合使用:只需將 HTTP 命令作為引數傳遞即可。也可以在那里進行 URL 構建。
api() {
local cmd=$1 api_name=$2
local url=${api_root}.${api_name}
local curl_opts=(
-o /dev/null
-s
-w "%{http_code}\n"
-H "Authorization: token 123abc"
)
case $cmd in
GET) : ;;
POST)
curl_opts =(
-X POST
-H "Content-Type: application/json"
-d "$4"
)
;;
*) echo "HTTP $cmd not implemented" >&1; return 1 ;;
esac
local status=$(curl "${curl_opts[@]}" "$url")
#check for condition
if [[ $status == 200 ]]; then
echo "Running successfully! $api_name Status code: $status"
else
echo "Error! $api_name Status code: $status"
fi
}
然后
api_root=...
api GET "api_1"
api POST "api_1" "${data[api_1]}"
這里的另一個優點是您不必在兩個地方對令牌進行硬編碼。
uj5u.com熱心網友回復:
我假設“api_1、api_2 等”并不總是連續的,因此一種簡單的方法是使用 URL 和資料創建單獨的串列,然后將其傳遞給腳本。如果您將以下部分添加到當前腳本中,您可以像這樣運行它
bash request.sh get get_list.txt 和 bash request.sh post post_list.txt
method="${1}"
file="${2}"
case "${method}" in
get|post);;
*) exit 1;;
esac
[ ! -e "${file}" ] && exit 1;
OFS=$IFS
IFS=$'\n'
while read line; do
api_url=$(echo ${line} | cut -d' ' -f1)
# only required by post method, it will be empty if there is no data and ignored by your get function
data=$(echo ${line} | cut -d' ' -f2)
api_name=$(echo ${api_url} | awk -F'.' '{print $NF}')
api_${method} "${api_url}" "${api_name}" "${data}"
done < ${file}
IFS=$OFS
名稱是從 URL 中提取的,因此無需將其作為引數傳遞
get_list.txt:
https://cms.abc.co/api/method/contentready_edu_cms.api.api_1
https://cms.abc.co/api/method/contentready_edu_cms.api.api_2
https://cms.abc.co/api/method/contentready_edu_cms.api.api_3
post_list.txt:
https://cms.abc.co/api/method/contentready_edu_cms.api.api_1 '{"some":"data1"}'
https://cms.abc.co/api/method/contentready_edu_cms.api.api_2 '{"some":"data2"}'
https://cms.abc.co/api/method/contentready_edu_cms.api.api_3 '{"some":"data3"}'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/422917.html
標籤:
