Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uploader] Uploader tool enhancement #279

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions uploader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ Install these dependency in-order to use the uploader tool:

- On the command line run `bash uploader.sh`
- Output will be posted to a file named `output.txt` in the same folder

## Grant type option
Grant type decides how your device authenticate to the server in-order to receive an access token, so your device can access the server's resources.
This tool only supports 2 types of grant type at the moment:
1. `client_credentials`
2. `password`
1 change: 1 addition & 0 deletions uploader/config.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GRANT_TYPE=
CLIENT_ID=
CLIENT_SECRET=
USERNAME=
Expand Down
1 change: 1 addition & 0 deletions uploader/sample_config.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GRANT_TYPE="client_credentials"
CLIENT_ID="client_id"
CLIENT_SECRET="client_secret"
USERNAME="username"
Expand Down
40 changes: 36 additions & 4 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 All @@ -20,7 +39,17 @@ main() {
. config.txt

# Get access_token
RESPONSE=$(curl -X POST -d "grant_type=password&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&username=$USERNAME&password=$PASSWORD" $ACCESS_TOKEN_URL)
echo "Requesting access token..."

if [ "$GRANT_TYPE" = "password" ]; then
RESPONSE=$(curl -X POST -d "grant_type=password&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&username=$USERNAME&password=$PASSWORD" $ACCESS_TOKEN_URL)
elif [ "$GRANT_TYPE" = "client_credentials" ]; then
RESPONSE=$(curl -X POST -d "grant_type=client_credentials&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET" $ACCESS_TOKEN_URL)
else
echo "Error: Unsupported grant type: $GRANT_TYPE"
exit 1
fi

if echo "$RESPONSE" | jq -e 'has("error")' >/dev/null; then
echo "Error: Failed to obtain an access token."
echo "Response: $RESPONSE"
Expand All @@ -31,6 +60,9 @@ main() {
ACCESS_TOKEN=$(jq -r '.access_token' <<< $RESPONSE)
export ACCESS_TOKEN
export SERVER_URL

echo "Access token obtained successfully."

# Get the files in the resource folder and push to server
find $RESOURCE_FOLDER -type f -exec bash -c 'push_to_server "$@"' bash {} \;
}
Expand Down