我有一个 API 或端点以下端点使用 GET 方法
https://test.cloudnetwork/devops/api/status?over=658xx899xxx68sspo
其回应如下
{
"info": testing of cloudnetwork data soon,
"test1": [ "reading test" ],
"test2": "FAILED",
"test3": { },
test4": {
"test4.1": "FAILED",
"test4.2": [
{
},
]
},
"test5": {
"test5.1": "FAILED",
"test5.2": [
在此之后,我们有很多类似于测试 4 和测试 5 的响应,例如测试 6 和测试 7 继续
这是我写的小shell脚本
if [ "$WAIT_FOR_COMPLETION" == "true" ]; then
echo "My message to complete..."
test4.1="PROCESSING"
while [[ "$test4.1" == "PROCESSING" ]]; do
TEST_STATUS=$(curl -s --location --request GET "https://test.cloudnetwork/devops/api/status?over=$OVER_ID" \
--header "Authorization: Bearer $ACCESS_TOKEN")
echo ""
test1=$(echo "$TEST_STATUS" | jq -r '.errorsDetected[]')
TEST=$(echo "$TEST_STATUS" | jq -r '.test4')
test4.1=$(echo "$test4" | jq -r '.status')
if [ "$test4.1" == "FAILED" ]; then
test4.1=$(echo "$TEST_STATUS" | jq -r '.test4.test4.1')
echo " "
echo "Test Status: $STATUS"
if [ -z "$TEST_4" ] || [ "$TEST_4" == "null" ]; then
echo "Build failed test4.1. $TEST_4"
exit 1
fi
fi
done
# Once the status is no longer "PROCESSING," assume it completed
echo "my message completed successfully!"
else
echo "my message failed"
exit 1
fi
我能够使用上面的 shell 脚本打印上面的响应消息
你能帮我看看我需要在 shell 脚本中进行哪些更改才能获得以上完整响应,状态显示“失败”、“通过”或“成功”
你就快到了,你可以像下面一样更新脚本 -
if [ "$WAIT_FOR_COMPLETION" == "true" ]; then
echo "My message to complete..."
test4_1="PROCESSING"
while [[ "$test4_1" == "PROCESSING" ]]; do
# Fetch the response
TEST_STATUS=$(curl -s --location --request GET "https://test.cloudnetwork/devops/api/status?over=$OVER_ID" \
--header "Authorization: Bearer $ACCESS_TOKEN")
# Print the full JSON response
echo "Full response:"
echo "$TEST_STATUS" | jq .
# Parse individual fields from the JSON response
test1=$(echo "$TEST_STATUS" | jq -r '.test1[]')
test2=$(echo "$TEST_STATUS" | jq -r '.test2')
test4_1=$(echo "$TEST_STATUS" | jq -r '.test4.test4.1')
test5_1=$(echo "$TEST_STATUS" | jq -r '.test5.test5.1')
# Print statuses
echo "Test1 Status: $test1"
echo "Test2 Status: $test2"
echo "Test4.1 Status: $test4_1"
echo "Test5.1 Status: $test5_1"
# Check if any of the statuses indicate failure
if [[ "$test4_1" == "FAILED" || "$test5_1" == "FAILED" ]]; then
echo "Build failed in test4.1 or test5.1. Status: FAILED"
exit 1
fi
# If "PROCESSING" is complete
if [[ "$test4_1" != "PROCESSING" ]]; then
echo "Test completed with status: SUCCESSFULLY"
break
fi
# Sleep to avoid rapid looping
sleep 5
done
echo "My message completed successfully!"
else
echo "My message failed"
exit 1
fi