Skip to content

Instantly share code, notes, and snippets.

@syntaqx
Created July 2, 2024 20:00
Show Gist options
  • Save syntaqx/ec8654ca396904624a2bb59659dbbb90 to your computer and use it in GitHub Desktop.
Save syntaqx/ec8654ca396904624a2bb59659dbbb90 to your computer and use it in GitHub Desktop.
Script to create secrets on my personal API
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 <secret_value>"
exit 1
}
# Check if a secret value is provided
if [ -z "$1" ]; then
usage
fi
# Assign the first argument to the SECRET variable
SECRET=$1
# Define the base API endpoint
BASE_API_URL="https://api.syntaqx.com/secrets"
# Define the JSON payload
JSON_PAYLOAD="{\"secret\": \"$SECRET\"}"
# Create a temporary directory and ensure cleanup
TMP_DIR=$(mktemp -d)
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT
# Define temporary files
RESPONSE_BODY_FILE="$TMP_DIR/response_body.txt"
STATUS_CODE_FILE="$TMP_DIR/status_code.txt"
# Post the secret value to the API and capture the response body and HTTP status code
curl -s -o "$RESPONSE_BODY_FILE" -w "%{http_code}" -X POST "$BASE_API_URL" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" > "$STATUS_CODE_FILE"
# Extract the HTTP status code from the response
HTTP_STATUS=$(cat "$STATUS_CODE_FILE")
# Extract the body from the response
RESPONSE_BODY=$(<"$RESPONSE_BODY_FILE")
# Check if the HTTP status code is 200 (success)
if [ "$HTTP_STATUS" -ne 200 ]; then
echo "Error: Received HTTP status code $HTTP_STATUS"
echo "Response from API: $RESPONSE_BODY"
exit 1
fi
# Extract the ID from the response body
ID=$(echo "$RESPONSE_BODY" | jq -r '.id')
# Create the read link
READ_LINK="$BASE_API_URL/$ID"
# Output the read link
echo "$READ_LINK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment