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