Repository navigation
Expand file tree
/
Copy pathcall-api.sh
More file actions
executable file
·104 lines (90 loc) · 3.96 KB
/
Copy pathcall-api.sh
File metadata and controls
executable file
·104 lines (90 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Variables
# Overridable so the same scripts can deploy to real Azure, where the API Management service, the
# storage account and the Function App all need globally unique names:
# PREFIX=apimdemo SUFFIX=$RANDOM bash scripts/deploy.sh
PREFIX="${PREFIX:-local}"
SUFFIX="${SUFFIX:-test}"
RESOURCE_GROUP_NAME="${PREFIX}-rg"
APIM_NAME="${PREFIX}-inventory-apim-${SUFFIX}"
APIM_API_VERSION='2022-08-01'
API_PATH='inventory'
APIM_SUBSCRIPTION_ID='partner-subscription'
# On Windows, Git Bash and MSYS2 shells pass a path such as /tmp/... unconverted to Windows programs
# (az, curl, jq, python, docker) when MSYS_NO_PATHCONV=1 is set, as the README asks. cygpath, which
# those shells provide, returns the C:/... form those programs can open; elsewhere the path is unchanged.
native_path() {
if command -v cygpath >/dev/null 2>&1; then
cygpath -m "$1"
else
echo "$1"
fi
}
# mktemp rather than fixed paths, as in validate.sh: parallel-safe, and cleaned up on exit.
BODY_FILE="$(native_path "$(mktemp "${TMPDIR:-/tmp}/inventory_call.XXXXXX.json")")"
HEADERS_FILE="$(native_path "$(mktemp "${TMPDIR:-/tmp}/inventory_call_headers.XXXXXX.txt")")"
trap 'rm -f "$BODY_FILE" "$HEADERS_FILE"' EXIT
# Retrieve the API Management service
echo "Retrieving the [$APIM_NAME] API Management service..."
APIM_ID=$(az apim show --name $APIM_NAME --resource-group $RESOURCE_GROUP_NAME --query id --output tsv)
if [[ -n "$APIM_ID" ]]; then
echo "[$APIM_NAME] API Management service successfully retrieved"
else
echo "Failed to retrieve the [$APIM_NAME] API Management service"
exit 1
fi
# Where the gateway answers: Azure's gatewayUrl, or the emulator's local alias (the emulator also
# claims the *.azure-api.net name, but it only resolves once LocalStack's DNS is in front of the machine)
ENVIRONMENT_NAME=$(az account show --query environmentName --output tsv)
if [[ "$ENVIRONMENT_NAME" == "LocalStack" ]]; then
GATEWAY_URL="http://${APIM_NAME}.apim.azure.localhost.localstack.cloud:4566"
else
GATEWAY_URL=$(az apim show --name $APIM_NAME --resource-group $RESOURCE_GROUP_NAME --query gatewayUrl --output tsv)
fi
API_URL="$GATEWAY_URL/$API_PATH"
# Retrieve the subscription key
echo "Reading the [$APIM_SUBSCRIPTION_ID] subscription key..."
KEY=$(az rest --method post \
--url "$APIM_ID/subscriptions/$APIM_SUBSCRIPTION_ID/listSecrets?api-version=$APIM_API_VERSION" \
--query primaryKey \
--output tsv)
if [[ -n "$KEY" ]]; then
echo "[$APIM_SUBSCRIPTION_ID] subscription key successfully retrieved"
else
echo "Failed to retrieve the [$APIM_SUBSCRIPTION_ID] subscription key"
exit 1
fi
# Call the gateway. The API allows ten calls a minute per subscription, so a run right after
# validate.sh may be told to wait: honour the Retry-After the gateway sends and try once more.
call_api() {
local url="$1"
STATUS=$(curl -s -m 20 -o "$BODY_FILE" -D "$HEADERS_FILE" -w "%{http_code}" -H "Ocp-Apim-Subscription-Key: $KEY" "$url")
if [[ "$STATUS" == "429" ]]; then
RETRY_AFTER=$(grep -i "^retry-after:" "$HEADERS_FILE" | tr -d '\r' | awk '{print $2}')
RETRY_AFTER=${RETRY_AFTER:-5}
[[ $RETRY_AFTER -lt 1 ]] && RETRY_AFTER=1
[[ $RETRY_AFTER -gt 60 ]] && RETRY_AFTER=60
echo "Rate limited by the gateway; waiting $RETRY_AFTER seconds as asked..."
sleep "$RETRY_AFTER"
STATUS=$(curl -s -m 20 -o "$BODY_FILE" -D "$HEADERS_FILE" -w "%{http_code}" -H "Ocp-Apim-Subscription-Key: $KEY" "$url")
fi
}
echo "Calling [$API_URL/items]..."
call_api "$API_URL/items"
if [[ "$STATUS" == "200" ]]; then
echo "[$API_URL/items] successfully returned [$STATUS]:"
jq . "$BODY_FILE"
else
echo "[$API_URL/items] returned [$STATUS]: $(cat "$BODY_FILE")"
exit 1
fi
echo "Calling [$API_URL/items/2]..."
call_api "$API_URL/items/2"
if [[ "$STATUS" == "200" ]]; then
echo "[$API_URL/items/2] successfully returned [$STATUS]:"
jq . "$BODY_FILE"
else
echo "[$API_URL/items/2] returned [$STATUS]: $(cat "$BODY_FILE")"
exit 1
fi
echo "Response header added by the gateway's outbound policy: $(grep -i "^x-served-by:" "$HEADERS_FILE" | tr -d '\r')"