Add ALIYUN_SDK_LAST_HTTP_CODE variable

This commit is contained in:
Zhong Lufan 2022-06-20 03:27:09 +08:00
parent 6317079f20
commit 142bc49a07
No known key found for this signature in database
GPG Key ID: BE2B3A1E76AD7D04
2 changed files with 7 additions and 3 deletions

View File

@ -8,6 +8,8 @@ for _aliapi_command in openssl curl; do
done
unset _aliapi_command
ALIYUN_SDK_LAST_HTTP_CODE=0
# aliapi_rpc <http_method> <host> <api_version> <api_action> [<--key> <value>...]
aliapi_rpc() {
_aliapi_check_vars || return $?
@ -68,11 +70,11 @@ aliapi_rpc() {
local _signature
_signature=$(_aliapi_signature_rpc "$_http_method" "${_query_str:0:-1}")
_query_str+="Signature=$(_aliapi_urlencode "$_signature")"
local _curl_out _http_code _http_url="https://$_http_host/?$_query_str"
local _curl_out _http_url="https://$_http_host/?$_query_str"
_curl_out=$(mktemp)
_http_code=$(curl --location --silent --show-error --request "$_http_method" --output "$_curl_out" --write-out "%{http_code}" --connect-timeout 3 "$_http_url") && cat "$_curl_out" - <<< ""
ALIYUN_SDK_LAST_HTTP_CODE=$(curl --location --silent --show-error --request "$_http_method" --output "$_curl_out" --write-out "%{http_code}" --connect-timeout 3 "$_http_url") && cat "$_curl_out" - <<< ""
rm -f "$_curl_out"
[[ $_http_code -eq 200 ]] && return 0 || return 1
[[ $ALIYUN_SDK_LAST_HTTP_CODE -eq 200 ]] && return 0 || return 1
}
_aliapi_check_vars() {

View File

@ -8,6 +8,7 @@
## 依赖
* coreutils ()
* curl
* openssl
@ -48,6 +49,7 @@ get_show_size() {
# 解析参数时会执行函数 (所以 ShowSize 的值是 50)
aliapi_rpc GET cas.aliyuncs.com 2018-07-13 DescribeUserCertificateList --CurrentPage 1 --ShowSize "get_show_size()"
# $? == 0 代表 HTTP CODE == 200 反之 $? == 1
# 可以通过 ALIYUN_SDK_HTTP_CODE 变量获取最后一次的 HTTP CODE
# 只要 curl 的退出代码 == 0 就会返回接收到的数据
if [[ $? -eq 0 ]]; then
# 执行成功