Skip to content

Commit

Permalink
Update local resource with id from server
Browse files Browse the repository at this point in the history
  • Loading branch information
FikriMilano committed Aug 26, 2024
1 parent b10fa0b commit d268fd6
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions uploader/uploader.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ push_to_server() {
RESOURCE_TYPE=$(cat "$@" | jq -r '.resourceType')

# Post to server
SERVER_URL="$SERVER_URL/${RESOURCE_TYPE}"
ENDPOINT_URL="${SERVER_URL}/${RESOURCE_TYPE}"
echo -e '\n'
echo Resource File: "$@"
echo Endpoint: "$SERVER_URL"
curl --write-out "%{http_code}\n" -X POST $SERVER_URL -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/fhir+json" -d @"$@" --silent >> output.txt
echo Endpoint: "$ENDPOINT_URL"

# Make the POST request
RESPONSE=$(curl -i -X POST "$ENDPOINT_URL" -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/fhir+json" -d @"$@" --silent)
echo "$RESPONSE" >> response.txt

# Extract the location header
LOCATION_HEADER=$(echo "$RESPONSE" | awk '/^location:/ { print $0 }')
echo "Location Header: $LOCATION_HEADER"

# Extract the generated resource ID
GENERATED_ID=$(echo "$LOCATION_HEADER" | sed -n 's|.*/\([^/]*\)/_history.*|\1|p')
echo "Extracted Generated ID: $GENERATED_ID"

if [ ! -z "$GENERATED_ID" ]; then
# Update the resource ID in the file
jq --arg id "$GENERATED_ID" '.id = $id' "$@" > tmp.$$.json && mv tmp.$$.json "$@"
echo "Updated the resource ID in $@ to $GENERATED_ID."
else
echo "Failed to obtain the generated ID from the server."
fi
}

main() {
Expand Down

0 comments on commit d268fd6

Please sign in to comment.