#!/bin/bash
# WC Licensed Product API - cURL Examples
#
# Replace YOUR_SITE_URL with your WordPress site URL
# Replace YOUR_LICENSE_KEY with your actual license key
# Replace YOUR_DOMAIN with the domain to validate/activate

BASE_URL="https://YOUR_SITE_URL/wp-json/wc-licensed-product/v1"
LICENSE_KEY="XXXX-XXXX-XXXX-XXXX"
DOMAIN="example.com"

# ------------------------------------------------------------------------------
# Validate License
# ------------------------------------------------------------------------------
# Validates if a license key is valid for a specific domain
# Returns: valid (bool), license details, or error message

echo "=== Validate License ==="
curl -X POST "${BASE_URL}/validate" \
  -H "Content-Type: application/json" \
  -d "{
    \"license_key\": \"${LICENSE_KEY}\",
    \"domain\": \"${DOMAIN}\"
  }"
echo -e "\n"

# ------------------------------------------------------------------------------
# Check License Status
# ------------------------------------------------------------------------------
# Gets the current status of a license without domain validation
# Returns: status, domain, expiration date, activation count

echo "=== Check License Status ==="
curl -X POST "${BASE_URL}/status" \
  -H "Content-Type: application/json" \
  -d "{
    \"license_key\": \"${LICENSE_KEY}\"
  }"
echo -e "\n"

# ------------------------------------------------------------------------------
# Activate License
# ------------------------------------------------------------------------------
# Activates a license on a specific domain
# Returns: success (bool), message

echo "=== Activate License ==="
curl -X POST "${BASE_URL}/activate" \
  -H "Content-Type: application/json" \
  -d "{
    \"license_key\": \"${LICENSE_KEY}\",
    \"domain\": \"${DOMAIN}\"
  }"
echo -e "\n"

# ------------------------------------------------------------------------------
# Example with verbose output for debugging
# ------------------------------------------------------------------------------
echo "=== Verbose Request (for debugging) ==="
curl -v -X POST "${BASE_URL}/validate" \
  -H "Content-Type: application/json" \
  -d "{
    \"license_key\": \"${LICENSE_KEY}\",
    \"domain\": \"${DOMAIN}\"
  }" 2>&1

# ------------------------------------------------------------------------------
# Example handling rate limit (429 response)
# ------------------------------------------------------------------------------
# The API returns a Retry-After header when rate limited
# You can use this header to determine when to retry

echo "=== Handling Rate Limits ==="
response=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/validate" \
  -H "Content-Type: application/json" \
  -d "{
    \"license_key\": \"${LICENSE_KEY}\",
    \"domain\": \"${DOMAIN}\"
  }")

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

if [ "$http_code" == "429" ]; then
  echo "Rate limited. Please wait before retrying."
  echo "Response: $body"
else
  echo "HTTP $http_code: $body"
fi
