Files
hyprmonitors/examples.sh
2025-08-15 16:28:28 +05:30

139 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Hyprmonitors API Examples
# Make sure the server is running with: cargo run
BASE_URL="http://localhost:3000"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_step() {
echo -e "${BLUE}$1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}$1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
# Function to check if server is running
check_server() {
if ! curl -s "$BASE_URL/health" > /dev/null 2>&1; then
print_error "Server is not running at $BASE_URL"
print_warning "Please start the server with: cargo run"
exit 1
fi
print_success "Server is running at $BASE_URL"
}
# Function to make API call with error handling
api_call() {
local method="$1"
local endpoint="$2"
local description="$3"
print_step "$description"
local response
if [[ "$method" == "POST" ]]; then
response=$(curl -s -X POST "$BASE_URL$endpoint" 2>&1)
else
response=$(curl -s "$BASE_URL$endpoint" 2>&1)
fi
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
echo "$response" | jq '.' 2>/dev/null || echo "$response"
print_success "Request completed"
else
print_error "Request failed: $response"
return 1
fi
echo
}
echo "=== Hyprmonitors API Examples ==="
echo
# Check if required tools are available
if ! command -v curl &> /dev/null; then
print_error "curl is required but not installed"
exit 1
fi
if ! command -v jq &> /dev/null; then
print_warning "jq is not installed - output will not be formatted"
fi
# Check server status
check_server
# Health check
api_call "GET" "/health" "1. Health Check"
# Get monitor status
api_call "GET" "/monitors/status" "2. Current Monitor Status"
# Turn all monitors off
api_call "POST" "/monitors/off" "3. Turning all monitors OFF"
sleep 2
# Check status after turning off
api_call "GET" "/monitors/status" "4. Status after turning off"
# Turn all monitors on
api_call "POST" "/monitors/on" "5. Turning all monitors ON"
sleep 2
# Check status after turning on
api_call "GET" "/monitors/status" "6. Status after turning on"
# Get available monitors and test with first one
print_step "7. Testing specific monitor control"
monitor_status=$(curl -s "$BASE_URL/monitors/status" 2>/dev/null)
if [[ $? -eq 0 ]]; then
MONITOR_NAME=$(echo "$monitor_status" | jq -r '.monitors | keys[0]' 2>/dev/null)
if [[ "$MONITOR_NAME" != "null" && "$MONITOR_NAME" != "" ]]; then
print_success "Found monitor: $MONITOR_NAME"
api_call "POST" "/monitors/$MONITOR_NAME/off" " Turning $MONITOR_NAME off"
sleep 2
api_call "POST" "/monitors/$MONITOR_NAME/on" " Turning $MONITOR_NAME on"
else
print_warning "No monitors found for specific testing"
fi
else
print_warning "Could not retrieve monitor list for specific testing"
fi
# Final status check
api_call "GET" "/monitors/status" "8. Final status check"
print_success "=== Examples completed ==="
echo
print_step "Available endpoints:"
echo " GET $BASE_URL/health"
echo " POST $BASE_URL/monitors/on"
echo " POST $BASE_URL/monitors/off"
echo " POST $BASE_URL/monitors/:monitor/on"
echo " POST $BASE_URL/monitors/:monitor/off"
echo " GET $BASE_URL/monitors/status"
echo
print_step "Tips:"
echo "• To find your monitor names, run: hyprctl monitors"
echo "• To start the server: cargo run"
echo "• To run with debug output: RUST_LOG=debug cargo run"
echo "• To change port: HYPRMONITORS_PORT=8080 cargo run"