fix: Improve API endpoint testing and selection
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
# Claude Code Configuration Script for XCodeCLI
|
# Claude Code Configuration Script for XCodeCLI
|
||||||
# This script configures Claude Code to use your XCodeCLI instance
|
# This script configures Claude Code to use your XCodeCLI instance
|
||||||
|
# Automatically tests multiple API endpoints and selects the working one
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
@@ -73,55 +74,54 @@ validate_api_key() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to test API connection
|
# Function to test API connection and return working base URL
|
||||||
test_api_connection() {
|
test_api_connection() {
|
||||||
local base_url="$1"
|
local api_key="$1"
|
||||||
local api_key="$2"
|
|
||||||
|
|
||||||
print_info "Testing API connection..."
|
local test_urls=(
|
||||||
|
"https://api.xcodecli.com"
|
||||||
|
"https://newapi.sususu.cf"
|
||||||
|
)
|
||||||
|
|
||||||
# Determine the correct endpoint based on whether this is a team URL
|
print_info "Testing API connections..."
|
||||||
local test_endpoint
|
|
||||||
local balance_field
|
for base_url in "${test_urls[@]}"; do
|
||||||
if [[ "$base_url" == */team ]]; then
|
print_info "Testing $base_url..."
|
||||||
test_endpoint="$base_url/api/v1/team/stats/spending"
|
|
||||||
balance_field="daily_remaining"
|
local test_endpoint="$base_url/v1/models"
|
||||||
else
|
|
||||||
test_endpoint="$base_url/api/v1/claude/balance"
|
|
||||||
balance_field="balance"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Test a simple request to the API
|
# Test a simple request to the API
|
||||||
local response
|
local response
|
||||||
response=$(curl -s -w "%{http_code}" -o /tmp/claude_test_response \
|
response=$(curl -s -w "%{http_code}" -o /tmp/claude_test_response \
|
||||||
-X GET "$test_endpoint" \
|
-X GET "$test_endpoint" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "X-API-Key: $api_key" \
|
-H "Authorization: Bearer $api_key" \
|
||||||
2>/dev/null || echo "000")
|
2>/dev/null || echo "000")
|
||||||
|
|
||||||
if [ "$response" = "200" ]; then
|
if [ "$response" = "200" ]; then
|
||||||
local balance
|
local model_count
|
||||||
balance=$(cat /tmp/claude_test_response | jq -r ".${balance_field}" 2>/dev/null || echo "unknown")
|
model_count=$(cat /tmp/claude_test_response | jq -r '.data | length' 2>/dev/null || echo "0")
|
||||||
if [[ "$base_url" == */team ]]; then
|
if [ "$model_count" -gt "0" ]; then
|
||||||
print_success "API connection successful! Daily remaining: \$${balance}"
|
print_success "API connection successful! Found $model_count models at $base_url"
|
||||||
else
|
|
||||||
print_success "API connection successful! Current balance: \$${balance}"
|
|
||||||
fi
|
|
||||||
rm -f /tmp/claude_test_response
|
rm -f /tmp/claude_test_response
|
||||||
|
echo "$base_url"
|
||||||
return 0
|
return 0
|
||||||
elif [ "$response" = "401" ]; then
|
|
||||||
print_error "API key authentication failed. Please check your API key."
|
|
||||||
rm -f /tmp/claude_test_response
|
|
||||||
return 1
|
|
||||||
elif [ "$response" = "000" ]; then
|
|
||||||
print_error "Cannot connect to API server. Please check the URL and your internet connection."
|
|
||||||
rm -f /tmp/claude_test_response
|
|
||||||
return 1
|
|
||||||
else
|
else
|
||||||
print_error "API test failed with HTTP status: $response"
|
print_warning "API responded but no models found at $base_url"
|
||||||
rm -f /tmp/claude_test_response
|
|
||||||
return 1
|
|
||||||
fi
|
fi
|
||||||
|
elif [ "$response" = "401" ]; then
|
||||||
|
print_warning "API key authentication failed for $base_url"
|
||||||
|
elif [ "$response" = "000" ]; then
|
||||||
|
print_warning "Cannot connect to $base_url"
|
||||||
|
else
|
||||||
|
print_warning "API test failed for $base_url with HTTP status: $response"
|
||||||
|
fi
|
||||||
|
|
||||||
|
rm -f /tmp/claude_test_response
|
||||||
|
done
|
||||||
|
|
||||||
|
print_error "All API connections failed. Please check your API key and internet connection."
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to create Claude Code settings
|
# Function to create Claude Code settings
|
||||||
@@ -135,12 +135,9 @@ create_settings() {
|
|||||||
"env": {
|
"env": {
|
||||||
"ANTHROPIC_BASE_URL": "$base_url",
|
"ANTHROPIC_BASE_URL": "$base_url",
|
||||||
"ANTHROPIC_AUTH_TOKEN": "$api_key",
|
"ANTHROPIC_AUTH_TOKEN": "$api_key",
|
||||||
"CLAUDE_CODE_MAX_OUTPUT_TOKENS": 20000,
|
|
||||||
"DISABLE_TELEMETRY": 1,
|
"DISABLE_TELEMETRY": 1,
|
||||||
"DISABLE_ERROR_REPORTING": 1,
|
"DISABLE_ERROR_REPORTING": 1,
|
||||||
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": 1,
|
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": 1,
|
||||||
"CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR": 1,
|
|
||||||
"MAX_THINKING_TOKENS": 12000
|
|
||||||
},
|
},
|
||||||
"model": "sonnet"
|
"model": "sonnet"
|
||||||
}
|
}
|
||||||
@@ -180,17 +177,17 @@ main() {
|
|||||||
check_jq
|
check_jq
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
local base_url=""
|
|
||||||
local api_key=""
|
local api_key=""
|
||||||
local test_only=false
|
local test_only=false
|
||||||
local show_settings=false
|
local show_settings=false
|
||||||
|
|
||||||
|
# Check for environment variable API_KEY
|
||||||
|
if [ -n "$API_KEY" ]; then
|
||||||
|
api_key="$API_KEY"
|
||||||
|
fi
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
-u|--url)
|
|
||||||
base_url="$2"
|
|
||||||
shift 2
|
|
||||||
;;
|
|
||||||
-k|--key)
|
-k|--key)
|
||||||
api_key="$2"
|
api_key="$2"
|
||||||
shift 2
|
shift 2
|
||||||
@@ -207,20 +204,26 @@ main() {
|
|||||||
cat <<EOF
|
cat <<EOF
|
||||||
Usage: $0 [OPTIONS]
|
Usage: $0 [OPTIONS]
|
||||||
|
|
||||||
|
This script automatically tests multiple API endpoints and selects the working one:
|
||||||
|
- https://api.xcodecli.com
|
||||||
|
- https://newapi.sususu.cf
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-u, --url URL Set the XCodeCLI base URL (default: $DEFAULT_BASE_URL)
|
|
||||||
-k, --key KEY Set the API key
|
-k, --key KEY Set the API key
|
||||||
-t, --test Test API connection only (requires -u and -k)
|
-t, --test Test API connections only (requires -k)
|
||||||
-s, --show Show current settings and exit
|
-s, --show Show current settings and exit
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
$0 --url https://your-domain.tld --key your-api-key-here
|
$0 --key your-api-key-here
|
||||||
$0 --test --url https://your-domain.tld --key your-api-key-here
|
$0 --test --key your-api-key-here
|
||||||
$0 --show
|
$0 --show
|
||||||
|
|
||||||
Interactive mode (no arguments):
|
Interactive mode (no arguments):
|
||||||
$0
|
$0
|
||||||
|
|
||||||
|
Environment Variables:
|
||||||
|
API_KEY API key for authentication
|
||||||
EOF
|
EOF
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
@@ -238,17 +241,11 @@ EOF
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Interactive mode if no arguments provided
|
# Interactive mode if no API key provided
|
||||||
if [ -z "$base_url" ] && [ -z "$api_key" ]; then
|
if [ -z "$api_key" ]; then
|
||||||
print_info "Interactive setup mode"
|
print_info "Interactive setup mode"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
# Get base URL
|
|
||||||
read -p "Enter XCodeCLI URL [$DEFAULT_BASE_URL]: " base_url
|
|
||||||
if [ -z "$base_url" ]; then
|
|
||||||
base_url="$DEFAULT_BASE_URL"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Get API key
|
# Get API key
|
||||||
while [ -z "$api_key" ]; do
|
while [ -z "$api_key" ]; do
|
||||||
read -p "Enter your API key: " api_key
|
read -p "Enter your API key: " api_key
|
||||||
@@ -261,8 +258,8 @@ EOF
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Validate inputs
|
# Validate inputs
|
||||||
if [ -z "$base_url" ] || [ -z "$api_key" ]; then
|
if [ -z "$api_key" ]; then
|
||||||
print_error "Both URL and API key are required"
|
print_error "API key is required"
|
||||||
print_info "Use --help for usage information"
|
print_info "Use --help for usage information"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -272,26 +269,30 @@ EOF
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove trailing slash from URL
|
print_info "API Key: ${api_key:0:8}...${api_key: -4}"
|
||||||
base_url="${base_url%/}"
|
|
||||||
|
|
||||||
print_info "Configuration:"
|
|
||||||
print_info " Base URL: $base_url"
|
|
||||||
print_info " API Key: ${api_key:0:8}...${api_key: -4}"
|
|
||||||
echo
|
echo
|
||||||
|
|
||||||
# Test API connection
|
# Test API connection and get working base URL
|
||||||
if ! test_api_connection "$base_url" "$api_key"; then
|
local base_url
|
||||||
|
base_url=$(test_api_connection "$api_key")
|
||||||
|
|
||||||
|
if [ $? -ne 0 ] || [ -z "$base_url" ]; then
|
||||||
if [ "$test_only" = true ]; then
|
if [ "$test_only" = true ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
read -p "API test failed. Continue anyway? (y/N): " -n 1 -r
|
read -p "All API tests failed. Continue anyway? (y/N): " -n 1 -r
|
||||||
echo
|
echo
|
||||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||||
print_info "Setup cancelled"
|
print_info "Setup cancelled"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# If user chooses to continue anyway, use default URL
|
||||||
|
base_url="$DEFAULT_BASE_URL"
|
||||||
|
print_warning "Using default URL: $base_url"
|
||||||
|
else
|
||||||
|
print_info "Selected working base URL: $base_url"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Exit if test only
|
# Exit if test only
|
||||||
|
|||||||
Reference in New Issue
Block a user