Skip to content

Instantly share code, notes, and snippets.

@lmeyerov
Created February 14, 2021 16:55
Show Gist options
  • Save lmeyerov/9c76c17e223cf7dfa06043d0a2036374 to your computer and use it in GitHub Desktop.
Save lmeyerov/9c76c17e223cf7dfa06043d0a2036374 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e # exit on fail
# Curl REST API Tutorial
# * Script goes along with the version in the docs
# * assumes `jq` is installed
### SET ACCOUNT + SERVER
# Free test API account: See https://hub.graphistry.com via email signup
GRAPHISTRY_USERNAME=${GRAPHISTRY_USERNAME:-testuser}
GRAPHISTRY_PASSWORD=${GRAPHISTRY_PASSWORD:-testpwd}
GRAPHISTRY_BASE_PATH=${GRAPHISTRY_BASE_PATH:-http://localhost}
### CREATE DEMO FILES ###
rm -f graphistry_edges.csv
echo "s,d,txt,num" >> graphistry_edges.csv
echo "a,b,the,2" >> graphistry_edges.csv
echo "b,c,quick,4" >> graphistry_edges.csv
echo "c,a,brown,6" >> graphistry_edges.csv
rm -f graphistry_nodes.csv
echo "n,v,v2" >> graphistry_nodes.csv
echo "a,2,a" >> graphistry_nodes.csv
echo "b,4,aa" >> graphistry_nodes.csv
echo "c,6,aaa" >> graphistry_nodes.csv
### 1. GET TOKEN
### 2,3. CREATE+POPULATE SERVER EDGE, NODE FILES
### 4. TURN INTO A VIZ
echo "1. Generate JWT token -- POST /api-token-auth/"
OUT=$( curl -fsv -X POST \
-H "Content-Type: application/json" \
-d "{\"username\": \"${GRAPHISTRY_USERNAME}\", \"password\": \"${GRAPHISTRY_PASSWORD}\"}" \
${GRAPHISTRY_BASE_PATH}/api-token-auth/ )
echo $OUT | jq .
export GRAPHISTRY_TOKEN=$( echo "$OUT" | jq -jr .token )
echo "2a. Create File for edges -- POST /api/v2/files/"
OUT=$( curl -fsv -X POST \
-H "Authorization: Bearer ${GRAPHISTRY_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"file_type": "csv", "name": "my edges", "file_compression": "" }' \
${GRAPHISTRY_BASE_PATH}/api/v2/files/ )
echo $OUT | jq .
export EDGES_FILE_ID=$( echo "$OUT" | jq -jr .file_id )
echo "2b. Upload data into edges File -- POST /api/v2/upload/files/<file_id>?erase=true"
OUT=$( curl -fsv -X POST \
-H "Authorization: Bearer ${GRAPHISTRY_TOKEN}" \
-H "Content-Type: application/json" \
-T edges.csv \
${GRAPHISTRY_BASE_PATH}/api/v2/upload/files/${EDGES_FILE_ID}?erase=true )
echo $OUT | jq .
echo "3a. Create File for nodes -- POST /api/v2/files/"
OUT=$( curl -fsv -X POST \
-H "Authorization: Bearer ${GRAPHISTRY_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"file_type": "csv", "name": "my nodes", "file_compression": "" }' \
${GRAPHISTRY_BASE_PATH}/api/v2/files/ )
echo $OUT | jq .
export NODES_FILE_ID=$( echo "$OUT" | jq -jr .file_id )
echo "3b. Upload data into nodes File -- POST /api/v2/upload/files/<file_id>?erase=true"
OUT=$( curl -fsv -X POST \
-H "Authorization: Bearer ${GRAPHISTRY_TOKEN}" \
-H "Content-Type: application/json" \
-T graphistry_nodes.csv \
${GRAPHISTRY_BASE_PATH}/api/v2/upload/files/${NODES_FILE_ID}?erase=true )
echo $OUT | jq .
echo "4. Create visualization from files -- POST /api/v2/dataset"
DATASET_BINDINGS="{\
\"node_encodings\": {\"bindings\": {\"node\": \"n\"}},\
\"edge_encodings\": {\"bindings\": {\"source\": \"s\", \"destination\": \"d\"}},\
\"node_files\": [\"${NODES_FILE_ID}\"],\
\"edge_files\": [\"${EDGES_FILE_ID}\"],\
\"metadata\": {},\
\"name\": \"my csv viz\"\
}"
OUT=$( curl -fsv -X POST \
-H "Authorization: Bearer $GRAPHISTRY_TOKEN" \
-H "Content-Type: application/json" \
-d"$DATASET_BINDINGS" \
${GRAPHISTRY_BASE_PATH}/api/v2/upload/datasets/ )
echo $OUT | jq .
export GRAPHISTRY_DATASET_ID=$( echo "$OUT" | jq -jr .data.dataset_id )
echo
echo "URL: ${GRAPHISTRY_BASE_PATH}/graph/graph.html?dataset=${GRAPHISTRY_DATASET_ID}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment