Skip to content

Commit 502dfb3

Browse files
refactor: centralize result server JSON fetch [skip-ci]
1 parent d270aac commit 502dfb3

3 files changed

Lines changed: 42 additions & 15 deletions

File tree

scripts/estimate_common.sh

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
set -euo pipefail
1111

12+
source "$(dirname "${BASH_SOURCE[0]}")/result_server_client.sh"
13+
1214
# ---------------------------------------------------------------------------
1315
# Global variables — populated by read_values
1416
# ---------------------------------------------------------------------------
@@ -364,15 +366,6 @@ fetch_current_fom() {
364366
local code="$2"
365367
local exp="${3:-}"
366368

367-
if [[ -z "${RESULT_SERVER:-}" ]]; then
368-
echo "ERROR: RESULT_SERVER is not set" >&2
369-
exit 1
370-
fi
371-
if [[ -z "${RESULT_SERVER_KEY:-}" ]]; then
372-
echo "ERROR: RESULT_SERVER_KEY is not set" >&2
373-
exit 1
374-
fi
375-
376369
local url="${RESULT_SERVER}/api/query/result?system=${system}&code=${code}"
377370
if [[ -n "$exp" ]]; then
378371
url="${url}&exp=${exp}"
@@ -381,7 +374,7 @@ fetch_current_fom() {
381374
local response
382375
local curl_exit
383376
set +e
384-
response=$(curl -sf -H "X-API-Key: ${RESULT_SERVER_KEY}" "$url")
377+
response=$(bk_result_server_get_json "/api/query/result?system=${system}&code=${code}${exp:+&exp=${exp}}")
385378
curl_exit=$?
386379
set -e
387380
if [[ $curl_exit -ne 0 || -z "$response" ]]; then

scripts/fetch_result_by_uuid.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# RESULT_SERVER - Base URL of the result server
1111
set -euo pipefail
1212

13+
source "$(dirname "$0")/result_server_client.sh"
14+
1315
if [[ -z "${code:-}" ]]; then
1416
echo "ERROR: code must be specified" >&2
1517
exit 1
@@ -21,9 +23,9 @@ resolved_result_uuid="${result_uuid:-}"
2123

2224
if [[ -z "$resolved_result_uuid" && -n "${estimate_result_uuid:-}" ]]; then
2325
echo "Fetching estimate for UUID: $estimate_result_uuid"
24-
curl --fail -L -sS -H "X-API-Key: ${RESULT_SERVER_KEY}" \
25-
-o "results/source_estimate.json" \
26-
"${RESULT_SERVER}/api/query/estimate?uuid=${estimate_result_uuid}"
26+
bk_result_server_get_json_to_file \
27+
"/api/query/estimate?uuid=${estimate_result_uuid}" \
28+
"results/source_estimate.json"
2729

2830
resolved_result_uuid="$(jq -r '.estimate_metadata.source_result_uuid // empty' results/source_estimate.json)"
2931
if [[ -z "$resolved_result_uuid" ]]; then
@@ -44,6 +46,7 @@ if [[ -z "$resolved_result_uuid" ]]; then
4446
fi
4547

4648
echo "Fetching result for UUID: $resolved_result_uuid"
47-
curl --fail -L -sS -H "X-API-Key: ${RESULT_SERVER_KEY}" -o "results/result0.json" \
48-
"${RESULT_SERVER}/api/query/result?uuid=${resolved_result_uuid}"
49+
bk_result_server_get_json_to_file \
50+
"/api/query/result?uuid=${resolved_result_uuid}" \
51+
"results/result0.json"
4952
echo "Fetched result to results/result0.json"

scripts/result_server_client.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
# Shared helpers for reading JSON from the result server.
3+
4+
set -euo pipefail
5+
6+
bk_result_server_require_env() {
7+
if [[ -z "${RESULT_SERVER:-}" ]]; then
8+
echo "ERROR: RESULT_SERVER is not set" >&2
9+
exit 1
10+
fi
11+
if [[ -z "${RESULT_SERVER_KEY:-}" ]]; then
12+
echo "ERROR: RESULT_SERVER_KEY is not set" >&2
13+
exit 1
14+
fi
15+
}
16+
17+
bk_result_server_get_json() {
18+
local path_and_query="$1"
19+
bk_result_server_require_env
20+
curl --fail -L -sS -H "X-API-Key: ${RESULT_SERVER_KEY}" \
21+
"${RESULT_SERVER}${path_and_query}"
22+
}
23+
24+
bk_result_server_get_json_to_file() {
25+
local path_and_query="$1"
26+
local output_path="$2"
27+
bk_result_server_require_env
28+
curl --fail -L -sS -H "X-API-Key: ${RESULT_SERVER_KEY}" \
29+
-o "$output_path" \
30+
"${RESULT_SERVER}${path_and_query}"
31+
}

0 commit comments

Comments
 (0)