-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_common.sh
More file actions
92 lines (77 loc) · 2.65 KB
/
Copy pathapi_common.sh
File metadata and controls
92 lines (77 loc) · 2.65 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
#!/bin/bash
# api_util/api_common.sh
# 1. Load Configuration Paths
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJECT_ROOT="$SCRIPT_DIR/.."
CONFIG_PATH="${ATRIUM_CONFIG:-$PROJECT_ROOT/config_api.txt}"
if [ -f "$CONFIG_PATH" ]; then
# shellcheck disable=SC1090 # CONFIG_PATH is dynamic; not followed at lint time
source "$CONFIG_PATH"
else
echo "Error: Config file '$CONFIG_PATH' not found."
exit 1
fi
# 2. Validation
if [ ! -d "$INPUT_TABLES_DIR" ]; then
echo "Error: Input directory '$INPUT_TABLES_DIR' does not exist."
echo "Please update INPUT_TABLES_DIR in config_api.txt"
exit 1
fi
# FIX #11: Removed manifest.py (superseded by build_manifest_row.py) and
# analyze.py (superseded by summarize_nt_udp.py) from the required-scripts
# list. They still exist on disk but are no longer called by any shell script,
# so checking for them was creating false confidence.
for script in chunk.py build_manifest_row.py call_udpipe.py call_nametag.py; do
if [ ! -f "$SCRIPT_DIR/$script" ]; then
echo "Error: Helper script '$script' not found in $SCRIPT_DIR"
exit 1
fi
done
# 3. Setup Output
mkdir -p "$OUTPUT_DIR"
# 4. Helper Functions
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
rate_limit() {
# Adjust sleep based on API limits. 0.2s = 5 req/s.
sleep 0.2
}
parse_json_result() {
local json_file="$1"
# Improved: Pass filename as argument to avoid quoting issues
python3 -c "import sys, json;
try:
with open(sys.argv[1], 'r', encoding='utf-8') as f:
data = json.load(f)
result = data.get('result', '')
if not result: sys.exit(2)
sys.stdout.write(result)
except Exception: sys.exit(1)" "$json_file"
}
api_call_with_retry() {
local api_name="$1"
local url="$2"
local response_file="$3"
shift 3
local attempt=1
local delay=1
while [ "$attempt" -le "$MAX_RETRIES" ]; do
local http_code_file="${response_file}.code"
# Pass remaining arguments (\"$@\") to curl (flags like -F)
curl -s -S -w "%{http_code}" "$@" "$url" -o "$response_file" > "$http_code_file"
local http_code
http_code=$(cat "$http_code_file")
rm -f "$http_code_file"
if [ "$http_code" = "200" ]; then
return 0
fi
log "[WARN] $api_name failed (HTTP $http_code). Retrying in ${delay}s..."
sleep "$delay"
# Calculate backoff using python
delay=$(python3 -c "print(int($delay * $BACKOFF_FACTOR + 1))")
attempt=$((attempt + 1))
done
log "[ERR] $api_name failed permanently after $MAX_RETRIES attempts."
return 1
}