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

Enhance example.sh to reduce hardcoding and improve troubleshootability #32

Merged
merged 3 commits into from
Dec 31, 2023
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
121 changes: 98 additions & 23 deletions scripts/example/example.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
#!/bin/bash
# API REQUEST EXAMPLES

set -e

cd "$(dirname "$0")"

log_checkpoint() {
local message="$1"
echo
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "$message"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
}

log_checkpoint "Check server status:"
URL="http://localhost:8080/"
response="$(curl --write-out "%{http_code}" --silent --output /dev/null "$URL" || true)"
if [ "$response" -eq 200 ] || [ "$response" -eq 301 ] || [ "$response" -eq 302 ]; then
echo "ok"
else
cat << EOF
Error: response ${response} at ${URL}. Please follow these steps:

# From the root directory, run
# > ./configure.sh
#
# and then
# > ./docker/build-docker.sh
# > ./docker/start-docker.sh
EOF
exit 1
fi


if [ -z "$TOKEN" ]; then cat << EOF
Error: TOKEN is not defined. Please follow these steps:

# Access https://developers.google.com/oauthplayground/ with the account that
# you added as admin in 'scripts/init/init_data.cypher', and click in 'Authorize
# APIs' with the scope 'https://www.googleapis.com/auth/userinfo.email'. Or
# select it in list.
#
# In step 2, click in 'Exchange authorization code for tokens' and copy your
# own access token to the variable TOKEN below.
#
# Then, execute
#
# > export TOKEN="YOUR_ACCESS_TOKEN"
#
# and run this script again.
EOF
exit 1
else
echo "ok TOKEN is defined."
fi

TOKEN="CHANGE_ME"
../init/make_init_data.cypher.sh

# Let us try several requests:

# List all users:
log_checkpoint "List all users:"
curl -v --location --request GET 'http://localhost:8080/users?provider=google' --header "Authorization: Bearer $TOKEN"

# List all taxa
log_checkpoint "List all taxa"
echo -n "Taxa: "
curl --location --request GET 'http://localhost:8080/taxa?provider=google' --header "Authorization: Bearer $TOKEN"
echo

# Create taxon:
log_checkpoint "Create taxon:"
curl -v --location --request PUT 'http://localhost:8080/taxa/bbacilliformis?provider=google' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
Expand All @@ -29,17 +76,17 @@ curl -v --location --request PUT 'http://localhost:8080/taxa/bbacilliformis?prov
"description": "Example taxon"
}'

# List all loci:
log_checkpoint "List all loci:"
echo -n "Loci: "
curl --location --request GET 'http://localhost:8080/taxa/bbacilliformis/loci?provider=google' \
--header "Authorization: Bearer $TOKEN"
echo

# Delete locus (mark deprecated):
log_checkpoint "Delete locus (mark deprecated):"
curl -v --location --request DELETE 'http://localhost:8080/taxa/bbacilliformis/loci/ftsZ?provider=google' \
--header "Authorization: Bearer $TOKEN"

# Create locus:
log_checkpoint "Create locus:"
for x in locus1 locus2 locus3 locus4 locus5 locus6 locus7; do
curl -v --location --request PUT "http://localhost:8080/taxa/bbacilliformis/loci/$x?provider=google" \
--header 'Content-Type: application/json' \
Expand All @@ -50,15 +97,15 @@ for x in locus1 locus2 locus3 locus4 locus5 locus6 locus7; do
}";
done

# Load alleles:
log_checkpoint "Load alleles:"
for x in 1 2 3 4 5 6 7; do
curl -v --location --request POST "http://localhost:8080/taxa/bbacilliformis/loci/locus${x}/alleles/files?provider=google" \
--header 'Content-Type: multipart/form-data' \
--header "Authorization: Bearer $TOKEN" \
--form "file=@profiles_${x}.txt" ;
done

# List alleles:
log_checkpoint "List alleles:"
for x in 1 2 3 4 5 6 7; do
echo -n "Alleles[locus${x}]: "
curl --location --request GET "http://localhost:8080/taxa/bbacilliformis/loci/locus${x}/alleles?provider=google" \
Expand All @@ -71,7 +118,7 @@ curl --location --request GET "http://localhost:8080/taxa/bbacilliformis/loci/lo
--header "Authorization: Bearer $TOKEN"
echo

# List all schemas:
log_checkpoint "List all schemas:"
echo -n "Schemas: "
curl --location --request GET 'http://localhost:8080/taxa/bbacilliformis/schemas?provider=google' \
--header "Authorization: Bearer $TOKEN"
Expand All @@ -81,7 +128,7 @@ curl --location --request GET 'http://localhost:8080/taxa/bbacilliformis/schemas
--header "Authorization: Bearer $TOKEN"
echo

# Create schema:
log_checkpoint "Create schema:"
curl -v --location --request PUT "http://localhost:8080/taxa/bbacilliformis/schemas/mlst7?provider=google" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
Expand All @@ -93,12 +140,12 @@ curl -v --location --request PUT "http://localhost:8080/taxa/bbacilliformis/sche
"loci": ["locus1", "locus2", "locus3", "locus4", "locus5", "locus6", "locus7"]
}';

# List all projects:
log_checkpoint "List all projects:"
echo -n "Projects: "
curl --location --request GET 'http://localhost:8080/projects?provider=google' --header "Authorization: Bearer $TOKEN"
echo

# Create project:
log_checkpoint "Create project:"
curl -v --location --request POST 'http://localhost:8080/projects?provider=google' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
Expand All @@ -109,16 +156,31 @@ curl -v --location --request POST 'http://localhost:8080/projects?provider=googl
"users": [{"id": "[email protected]", "provider": "google"}]
}'

# Set the project id.
PROJECT="CHANGE_ME"

# List all datasets:
if [ -z "$PROJECT" ]; then cat << EOF
Error: PROJECT is not defined. Please follow these steps:

# TODO
#
# Then, execute
#
# > export PROJECT="YOUR_PROJECT_ID"
#
# and run this script again.
EOF
exit 1
else
echo "ok PROJECT is defined."
fi


log_checkpoint "List all datasets:"
echo -n "Datasets: "
curl --location --request GET "http://localhost:8080/projects/$PROJECT/datasets?provider=google" \
--header "Authorization: Bearer $TOKEN"
echo

# Create dataset:
log_checkpoint "Create dataset:"
curl -v --location --request POST "http://localhost:8080/projects/$PROJECT/datasets?provider=google" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
Expand All @@ -128,16 +190,29 @@ curl -v --location --request POST "http://localhost:8080/projects/$PROJECT/datas
"description": "Example dataset"
}'

# Set dataset id:
DATASET="CHANGE_ME"
if [ -z "$DATASET" ]; then cat << EOF
Error: DATASET is not defined. Please follow these steps:

# Load profiles:
# TODO
#
# Then, execute
#
# > export DATASET="YOUR_DATASET_ID"
#
# and run this script again.
EOF
exit 1
else
echo "ok DATASET is defined."
fi

log_checkpoint "Load profiles:"
curl -v --location --request POST "http://localhost:8080/projects/$PROJECT/datasets/$DATASET/profiles/files?provider=google" \
--header 'Content-Type: multipart/form-data' \
--header "Authorization: Bearer $TOKEN" \
--form '[email protected]'

# List profiles:
log_checkpoint "List profiles:"
echo -n "Profiles: "
curl --location --request GET "http://localhost:8080/projects/$PROJECT/datasets/$DATASET/profiles?provider=google" \
--header "Authorization: Bearer $TOKEN"
Expand All @@ -149,7 +224,7 @@ for x in $(seq 10); do
echo;
done

# Run inference:
log_checkpoint "Run inference:"
curl -v --location --request POST "http://localhost:8080/projects/$PROJECT/jobs?provider=google" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
Expand All @@ -159,7 +234,7 @@ curl -v --location --request POST "http://localhost:8080/projects/$PROJECT/jobs?
\"parameters\": [\"$DATASET\", 3]
}"

# List inferences:
log_checkpoint "List inferences:"
echo -n "Inferences: "
curl --location --request GET "http://localhost:8080/projects/$PROJECT/datasets/$DATASET/inferences?provider=google" \
--header "Authorization: Bearer $TOKEN"
Expand Down
1 change: 1 addition & 0 deletions scripts/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
init_data.cypher
3 changes: 0 additions & 3 deletions scripts/init/init_data.cypher

This file was deleted.

25 changes: 25 additions & 0 deletions scripts/init/make_init_data.cypher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -e

cd "$(dirname "$0")"

if [ -z "$USER_ID" ]; then cat << EOF
Error: USER_ID is not defined. Please follow these steps:

# Execute
#
# > export USER_ID="YOUR_USER_ID"
#
# where YOUR_USER_ID is the email address associated with the account used to
# create TOKEN.
EOF
exit 1
else
echo "ok USER_ID defined as ${USER_ID}"
fi

cat << EOF > init_data.cypher
CALL apoc.cypher.runMany("MATCH(n) DETACH DELETE n;
CREATE (:User {provider: 'google', id: '${USER_ID}', deprecated: false})-[:CONTAINS_DETAILS {from: datetime(), version: 1}]->(:UserDetails {role: 'admin'})
EOF
Empty file modified util.sh
100644 → 100755
Empty file.