Skip to content

Commit b21de7a

Browse files
authored
Merge pull request #417 from ansforge/feat/converter-helper-script
feat(converter): helper script
2 parents 5f40cfb + 28ce973 commit b21de7a

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

converter/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,43 @@ curl -X POST http://localhost:8080/convert \
100100
-H "Content-Type: application/json" \
101101
-d "$(jq --argjson usecase "$(cat ../src/main/resources/sample/examples/RS-EDA/RS-EDA-SMUR_FemmeEnceinte_DelphineVigneau.01.json)" '.sourceVersion = "v3" | .targetVersion = "v3" | .cisuConversion = true | .edxl.content[0].jsonContent.embeddedJsonContent.message |= (. + $usecase)' tests/fixtures/EDXL/edxl_envelope_health_to_fire.json)"
102102
```
103+
104+
### Helper script — `scripts/call_convert.sh`
105+
106+
Executable helper to call `/convert` quickly without building the payload by hand.
107+
108+
**Prerequisite**: local-dev setup running (see [launch local dev setup](https://github.com/ansforge/SAMU-Hub-Config/blob/main/tools/local-dev/README.md)).
109+
110+
Usage:
111+
112+
```bash
113+
scripts/call_convert.sh <sourceVersion> <targetVersion> <type> <useCase> [options]
114+
```
115+
116+
Positional args:
117+
- `sourceVersion`, `targetVersion`: `v1` | `v2` | `v3` | `vactive`
118+
- `type`: `HealthVersionConversion` | `CISUTranscoding` | `CISUVersionConversion` (see `ConversionType` in `converter/constants.py`)
119+
- `useCase`: local path to a JSON file **or** `http(s)://` URL of the message payload
120+
121+
Options:
122+
- `--envelope <path|url>`: EDXL envelope file or URL (default: `tests/fixtures/EDXL/edxl_envelope_health_to_health.json`)
123+
- `--converter_url <url>`: converter base URL, `/convert` is appended (default: `http://localhost:8083`)
124+
- `-h`, `--help`: show usage
125+
126+
Examples:
127+
128+
```bash
129+
# Local fixture, default envelope (health-to-health)
130+
scripts/call_convert.sh v3 v2 HealthVersionConversion tests/fixtures/RS-RI/RS-RI_V3.0_exhaustive_fill.json
131+
132+
# CISU transcoding with a custom envelope
133+
scripts/call_convert.sh v3 vactive CISUTranscoding ./tests/fixtures/RC-EDA/RC-EDA_required_fields.json \
134+
--envelope tests/fixtures/EDXL/edxl_envelope_fire_to_health.json
135+
136+
# Remote payload via URL
137+
scripts/call_convert.sh v3 vactive CISUTranscoding https://raw.githubusercontent.com/org/repo/main/sample.json
138+
139+
# Override endpoint (e.g. staging)
140+
scripts/call_convert.sh v3 vactive CISUTranscoding ./sample.json \
141+
--converter_url https://staging.example.com
142+
```

converter/scripts/call_convert.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
5+
ENVELOPE_DEFAULT="$SCRIPT_DIR/../tests/fixtures/EDXL/edxl_envelope_health_to_health.json"
6+
CONVERTER_URL_DEFAULT="http://localhost:8083"
7+
8+
usage() {
9+
cat >&2 <<EOF
10+
Usage: $(basename "$0") <sourceVersion> <targetVersion> <type> <useCase> [options]
11+
12+
Positional arguments:
13+
sourceVersion e.g. v1, v2, v3, vactive (passed as-is)
14+
targetVersion e.g. v1, v2, v3, vactive (passed as-is)
15+
type HealthVersionConversion | CISUTranscoding | CISUVersionConversion
16+
useCase path to a JSON file OR http(s) URL of the message payload
17+
18+
Options:
19+
--envelope <path|url> EDXL envelope file or URL
20+
(default: tests/fixtures/EDXL/edxl_envelope_health_to_health.json)
21+
--converter_url <url> Converter base URL (default: http://localhost:8083)
22+
-h, --help Show this message
23+
24+
Example:
25+
$(basename "$0") v3 v2 HealthVersionConversion tests/fixtures/RS-RI/RS-RI_V3.0_exhaustive_fill.json
26+
EOF
27+
}
28+
29+
ENVELOPE="$ENVELOPE_DEFAULT"
30+
CONVERTER_URL="$CONVERTER_URL_DEFAULT"
31+
POSITIONAL=()
32+
33+
while [[ $# -gt 0 ]]; do
34+
case "$1" in
35+
--envelope)
36+
[[ $# -ge 2 ]] || { echo "Error: --envelope requires a value" >&2; usage; exit 1; }
37+
ENVELOPE="$2"; shift 2;;
38+
--converter_url)
39+
[[ $# -ge 2 ]] || { echo "Error: --converter_url requires a value" >&2; usage; exit 1; }
40+
CONVERTER_URL="$2"; shift 2;;
41+
-h|--help)
42+
usage; exit 0;;
43+
--*)
44+
echo "Error: unknown flag: $1" >&2; usage; exit 1;;
45+
*)
46+
POSITIONAL+=("$1"); shift;;
47+
esac
48+
done
49+
50+
if [[ ${#POSITIONAL[@]} -ne 4 ]]; then
51+
echo "Error: expected 4 positional arguments, got ${#POSITIONAL[@]}" >&2
52+
usage
53+
exit 1
54+
fi
55+
56+
SOURCE_VERSION="${POSITIONAL[0]}"
57+
TARGET_VERSION="${POSITIONAL[1]}"
58+
TYPE="${POSITIONAL[2]}"
59+
USECASE="${POSITIONAL[3]}"
60+
61+
for dep in jq curl; do
62+
if ! command -v "$dep" >/dev/null 2>&1; then
63+
echo "Error: missing required dependency: $dep" >&2
64+
exit 1
65+
fi
66+
done
67+
68+
read_source() {
69+
local src="$1"
70+
local label="$2"
71+
if [[ "$src" =~ ^https?:// ]]; then
72+
if ! curl -fsSL "$src"; then
73+
echo "Error: failed to download $label from $src" >&2
74+
return 1
75+
fi
76+
else
77+
if [[ ! -f "$src" ]]; then
78+
echo "Error: $label file not found: $src" >&2
79+
return 1
80+
fi
81+
cat "$src"
82+
fi
83+
}
84+
85+
ENVELOPE_JSON=$(read_source "$ENVELOPE" "envelope") || exit 1
86+
USECASE_JSON=$(read_source "$USECASE" "useCase") || exit 1
87+
88+
PAYLOAD=$(jq -n \
89+
--argjson envelope "$ENVELOPE_JSON" \
90+
--argjson usecase "$USECASE_JSON" \
91+
--arg sourceVersion "$SOURCE_VERSION" \
92+
--arg targetVersion "$TARGET_VERSION" \
93+
--arg type "$TYPE" \
94+
'{
95+
sourceVersion: $sourceVersion,
96+
targetVersion: $targetVersion,
97+
type: $type,
98+
edxl: ($envelope.edxl | .content[0].jsonContent.embeddedJsonContent.message |= (. + $usecase))
99+
}')
100+
101+
RESPONSE=$(curl -sS -X POST "$CONVERTER_URL/convert" \
102+
-H "Content-Type: application/json" \
103+
-d "$PAYLOAD")
104+
105+
if printf '%s' "$RESPONSE" | jq . >/dev/null 2>&1; then
106+
printf '%s' "$RESPONSE" | jq .
107+
else
108+
printf '%s\n' "$RESPONSE"
109+
fi

0 commit comments

Comments
 (0)