Update
Improve: Handle errors Improve: Use curl for urlencode Optimization: Auto convert HTTP method to uppercase
This commit is contained in:
parent
68de6308ca
commit
36ed1f46d7
|
@ -24,7 +24,10 @@ aliapi_rpc() {
|
||||||
echo "Aliyun OpenAPI SDK: aliapi_rpc() not enough parameters"
|
echo "Aliyun OpenAPI SDK: aliapi_rpc() not enough parameters"
|
||||||
return 66
|
return 66
|
||||||
fi
|
fi
|
||||||
local _api_action=$4 _api_version=$3
|
local _http_host=$1 _http_method=$2 _api_action=$4 _api_version=$3
|
||||||
|
# 为了兼容 BusyBox
|
||||||
|
# shellcheck disable=SC2018,SC2019
|
||||||
|
_http_method=$(tr "a-z" "A-Z" <<< "$_http_method")
|
||||||
# 公共查询参数键
|
# 公共查询参数键
|
||||||
local _api_common_key=(
|
local _api_common_key=(
|
||||||
"AccessKeyId"
|
"AccessKeyId"
|
||||||
|
@ -54,7 +57,6 @@ aliapi_rpc() {
|
||||||
# 合并查询键值
|
# 合并查询键值
|
||||||
read -r -a _ali_key <<< "${_api_common_key[*]} ${_ali_custom_key[*]}"
|
read -r -a _ali_key <<< "${_api_common_key[*]} ${_ali_custom_key[*]}"
|
||||||
read -r -a _ali_value <<< "${_ali_common_value[*]} ${_ali_custom_value[*]}"
|
read -r -a _ali_value <<< "${_ali_common_value[*]} ${_ali_custom_value[*]}"
|
||||||
local _http_host=$1 _http_method=$2
|
|
||||||
local _query_str=""
|
local _query_str=""
|
||||||
local _key _value
|
local _key _value
|
||||||
local i
|
local i
|
||||||
|
@ -69,12 +71,11 @@ aliapi_rpc() {
|
||||||
local _ali_signature_value
|
local _ali_signature_value
|
||||||
_ali_signature_value=$(_ali_signature_rpc "$_http_method" "$_query_str")
|
_ali_signature_value=$(_ali_signature_rpc "$_http_method" "$_query_str")
|
||||||
_query_str+="Signature=$(_urlencode "$_ali_signature_value")"
|
_query_str+="Signature=$(_urlencode "$_ali_signature_value")"
|
||||||
local _curl_out _result_code _http_url="https://${_http_host}/?${_query_str}"
|
local _curl_out _http_code _http_url="https://${_http_host}/?${_query_str}"
|
||||||
_curl_out=$(mktemp)
|
_curl_out=$(mktemp)
|
||||||
_result_code=$(curl -L -s -X "$_http_method" -o "$_curl_out" --write-out "%{http_code}" "$_http_url" || echo $?)
|
_http_code=$(curl -L -s -S -X "$_http_method" -o "$_curl_out" -w "%{http_code}" --connect-timeout 3 "$_http_url") && cat "$_curl_out" - <<< ""
|
||||||
cat "$_curl_out" && echo
|
|
||||||
rm -f "$_curl_out"
|
rm -f "$_curl_out"
|
||||||
[[ ${_result_code} -eq 200 ]] && return 0 || return 1
|
[[ ${_http_code} -eq 200 ]] && return 0 || return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
_ali_signature_rpc() {
|
_ali_signature_rpc() {
|
||||||
|
@ -96,6 +97,6 @@ _ali_signature_nonce() {
|
||||||
|
|
||||||
_urlencode() {
|
_urlencode() {
|
||||||
local result
|
local result
|
||||||
result=$(curl -G -s -o /dev/null -w "%{url_effective}" --connect-timeout 1 --max-time 1 --data-urlencode "=$1" http://127.0.0.1:65535)
|
result=$(curl -G -s -o /dev/null -w "%{url_effective}" -m 1 --data-urlencode "=$1" http://127.0.0.1:99999)
|
||||||
echo "${result#*\?}"
|
echo "${result#*\?}"
|
||||||
}
|
}
|
||||||
|
|
11
README.md
11
README.md
|
@ -11,14 +11,9 @@
|
||||||
## 依赖
|
## 依赖
|
||||||
|
|
||||||
* bash
|
* bash
|
||||||
* coreutils
|
|
||||||
* curl
|
* curl
|
||||||
* openssl
|
* openssl
|
||||||
|
|
||||||
## 注意事项
|
|
||||||
|
|
||||||
由于 URL 编码使用了 `curl` 的 `--data-urlencode` 实现,所以每次 URL 编码都会对本地的 65535 端口发起 HTTP 请求,如果你的 65535 端口不是空闲的,为了避免对你的服务造成影响,建议修改脚本的 `_urlencode` 函数。
|
|
||||||
|
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
1. 导出环境变量 `AliAccessKeyId` 和 `AliAccessKeySecret`
|
1. 导出环境变量 `AliAccessKeyId` 和 `AliAccessKeySecret`
|
||||||
|
@ -27,7 +22,7 @@
|
||||||
|
|
||||||
函数签名:
|
函数签名:
|
||||||
```
|
```
|
||||||
aliapi_rpc(host, http_method, api_version, api_action, api_custom_key[], api_custom_value[]): JsonResult | ErrorCode
|
aliapi_rpc(host, http_method, api_version, api_action, api_custom_key[], api_custom_value[]): JsonResult
|
||||||
```
|
```
|
||||||
|
|
||||||
**示例:**
|
**示例:**
|
||||||
|
@ -61,8 +56,8 @@ api_custom_value=(
|
||||||
)
|
)
|
||||||
# 获取 SSL 证书列表:https://help.aliyun.com/document_detail/126511.html
|
# 获取 SSL 证书列表:https://help.aliyun.com/document_detail/126511.html
|
||||||
aliapi_rpc "cas.aliyuncs.com" "GET" "2018-07-13" "DescribeUserCertificateList" "${api_custom_key[*]}" "${api_custom_value[*]}"
|
aliapi_rpc "cas.aliyuncs.com" "GET" "2018-07-13" "DescribeUserCertificateList" "${api_custom_key[*]}" "${api_custom_value[*]}"
|
||||||
# $? (返回代码) 等于 0 (HTTP 状态码 == 200) 代表执行成功
|
# $? == 0 代表 HTTP CODE == 200 反之 $? == 1
|
||||||
# 执行成功返回 JSON 数据,执行失败返回 HTTP 状态码或 curl 的错误代码 ($?)。
|
# 只要 curl 的返回代码 == 0 就会返回接收到的数据
|
||||||
if [[ $? -eq 0 ]]; then
|
if [[ $? -eq 0 ]]; then
|
||||||
# 执行成功
|
# 执行成功
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue