-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi-curl-demo.sh
executable file
·62 lines (50 loc) · 1.77 KB
/
api-curl-demo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
# Get the base URL from CLI argument
if [ -z "$4" ]; then
echo "Usage: $0 <BASE_URL> <JSON_FILE_PATH> <USERNAME> <PASSWORD>"
exit 1
fi
BASE_URL="$1"
JSON_FILE_PATH="$2"
USERNAME="$3"
PASSWORD="$4"
# Function to obtain an API key
get_api_key() {
echo "Obtaining API key..."
# Make a POST request to get the API key
RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/get-token" \
-H "Content-Type: application/json" \
-d "{\"username\": \"$USERNAME\", \"password\": \"$PASSWORD\"}")
# Extract the token from the response
API_KEY=$(echo "$RESPONSE" | jq -r '.token')
if [ -z "$API_KEY" ] || [ "$API_KEY" == "null" ]; then
echo "Failed to retrieve API key. Response: $RESPONSE"
exit 1
fi
echo "API key obtained: $API_KEY"
}
# Function to create a visualization
create_visualization() {
echo "Creating visualization..."
RESPONSE=$(curl -s -X POST "$BASE_URL/api/visualizations/" \
-H "Authorization: Token $API_KEY" \
-F "jsonFile=@$JSON_FILE_PATH")
SLUG=$(echo "$RESPONSE" | jq -r '.slug')
VISUALIZE_URL=$(echo "$RESPONSE" | jq -r '.visualizeUrl')
EMBED_URL=$(echo "$RESPONSE" | jq -r '.embedUrl')
EMBED_SANKEY_URL=$(echo "$RESPONSE" | jq -r '.embedSankeyUrl')
EMBED_TABLE_URL=$(echo "$RESPONSE" | jq -r '.embedTableUrl')
if [ -z "$SLUG" ] || [ "$SLUG" == "null" ]; then
echo "Failed to create visualization. Response: $RESPONSE"
exit 1
fi
echo "Visualization created with slug: $SLUG"
echo "View it here: $VISUALIZE_URL"
echo "Embed options:"
echo " - Default: $EMBED_URL"
echo " - Sankey: $EMBED_SANKEY_URL"
echo " - Table: $EMBED_TABLE_URL"
}
# Run the functions
get_api_key
create_visualization