refactor: Remove BaseUrl parameter and update API connection testing logic
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
# Claude Code Configuration Script for XCodeCLI (Windows)
|
||||
# This script configures Claude Code to use your XCodeCLI instance
|
||||
# Run with: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 -BaseUrl https://api.xcodecli.com -ApiKey YOUR_KEY
|
||||
# Or via one-liner: & { $url='https://api.xcodecli.com'; $key='YOUR_KEY'; iwr -useb $url/setup-claude-code.ps1 | iex }
|
||||
# Run with: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 -ApiKey YOUR_KEY
|
||||
# Or via one-liner: & { $key='YOUR_KEY'; iwr -useb https://api.xcodecli.com/setup-claude-code.ps1 | iex }
|
||||
|
||||
param(
|
||||
[string]$BaseUrl,
|
||||
[string]$ApiKey,
|
||||
[switch]$Test,
|
||||
[switch]$Show,
|
||||
@@ -12,7 +11,6 @@ param(
|
||||
)
|
||||
|
||||
# Check for pre-set variables from one-liner command (use different names to avoid conflict)
|
||||
if (-not $BaseUrl -and (Test-Path Variable:url)) { $BaseUrl = $url }
|
||||
if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
|
||||
|
||||
# Configuration
|
||||
@@ -52,16 +50,19 @@ Claude Code Configuration Script for XCodeCLI (Windows)
|
||||
|
||||
Usage: powershell -ExecutionPolicy Bypass -File setup-claude-code.ps1 [OPTIONS]
|
||||
|
||||
This script automatically tests multiple API endpoints and selects the working one:
|
||||
- https://api.xcodecli.com
|
||||
- https://newapi.sususu.cf
|
||||
|
||||
Options:
|
||||
-BaseUrl <URL> Set the XCodeCLI base URL (default: $DefaultBaseUrl)
|
||||
-ApiKey <KEY> Set the API key
|
||||
-Test Test API connection only (requires -BaseUrl and -ApiKey)
|
||||
-Test Test API connections only (requires -ApiKey)
|
||||
-Show Show current settings and exit
|
||||
-Help Show this help message
|
||||
|
||||
Examples:
|
||||
.\setup-claude-code.ps1 -BaseUrl https://api.xcodecli.com -ApiKey your-api-key-here
|
||||
.\setup-claude-code.ps1 -Test -BaseUrl https://api.xcodecli.com -ApiKey your-api-key-here
|
||||
.\setup-claude-code.ps1 -ApiKey your-api-key-here
|
||||
.\setup-claude-code.ps1 -Test -ApiKey your-api-key-here
|
||||
.\setup-claude-code.ps1 -Show
|
||||
|
||||
Interactive mode (no arguments):
|
||||
@@ -104,52 +105,51 @@ function Test-ApiKey {
|
||||
}
|
||||
}
|
||||
|
||||
# Function to test API connection
|
||||
# Function to test API connection and return working base URL
|
||||
function Test-ApiConnection {
|
||||
param(
|
||||
[string]$BaseUrl,
|
||||
[string]$ApiKey
|
||||
)
|
||||
|
||||
Write-Info "Testing API connection..."
|
||||
$testUrls = @(
|
||||
"https://api.xcodecli.com",
|
||||
"https://newapi.sususu.cf"
|
||||
)
|
||||
|
||||
Write-Info "Testing API connections..."
|
||||
|
||||
foreach ($baseUrl in $testUrls) {
|
||||
Write-Info "Testing $baseUrl..."
|
||||
|
||||
try {
|
||||
$headers = @{
|
||||
"Content-Type" = "application/json"
|
||||
"X-API-Key" = $ApiKey
|
||||
}
|
||||
|
||||
# Determine the correct endpoint based on whether this is a team URL
|
||||
if ($BaseUrl.EndsWith("/team")) {
|
||||
$uri = "$BaseUrl/api/v1/team/stats/spending"
|
||||
$balanceField = "daily_remaining"
|
||||
$balanceLabel = "Daily remaining"
|
||||
} else {
|
||||
$uri = "$BaseUrl/api/v1/claude/balance"
|
||||
$balanceField = "balance"
|
||||
$balanceLabel = "Current balance"
|
||||
"Authorization" = "Bearer $ApiKey"
|
||||
}
|
||||
|
||||
$uri = "$baseUrl/v1/models"
|
||||
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -ErrorAction Stop
|
||||
|
||||
if ($response.$balanceField) {
|
||||
Write-Success "API connection successful! ${balanceLabel}: `$$($response.$balanceField)"
|
||||
return $true
|
||||
if ($response.data -and $response.data.Count -gt 0) {
|
||||
Write-Success "API connection successful! Found $($response.data.Count) models at $baseUrl"
|
||||
return $baseUrl
|
||||
} else {
|
||||
Write-Error "API test failed: Invalid response"
|
||||
return $false
|
||||
Write-Warning "API responded but no models found at $baseUrl"
|
||||
}
|
||||
}
|
||||
catch {
|
||||
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 401) {
|
||||
Write-Error "API key authentication failed. Please check your API key."
|
||||
Write-Warning "API key authentication failed for $baseUrl"
|
||||
} elseif ($_.Exception.Message -like "*Unable to connect*" -or $_.Exception.Message -like "*could not be resolved*") {
|
||||
Write-Error "Cannot connect to API server. Please check the URL and your internet connection."
|
||||
Write-Warning "Cannot connect to $baseUrl"
|
||||
} else {
|
||||
Write-Error "API test failed: $($_.Exception.Message)"
|
||||
Write-Warning "API test failed for $baseUrl`: $($_.Exception.Message)"
|
||||
}
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
Write-Error "All API connections failed. Please check your API key and internet connection."
|
||||
return $null
|
||||
}
|
||||
|
||||
# Function to create Claude Code settings
|
||||
@@ -163,12 +163,9 @@ function New-Settings {
|
||||
env = @{
|
||||
ANTHROPIC_BASE_URL = $BaseUrl
|
||||
ANTHROPIC_AUTH_TOKEN = $ApiKey
|
||||
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"
|
||||
}
|
||||
@@ -238,19 +235,11 @@ function Main {
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Interactive mode if no URL or Key provided
|
||||
if (-not $BaseUrl -and -not $ApiKey) {
|
||||
# Interactive mode if no Key provided
|
||||
if (-not $ApiKey) {
|
||||
Write-Info "Interactive setup mode"
|
||||
Write-Host ""
|
||||
|
||||
# Get base URL
|
||||
$inputUrl = Read-Host "Enter XCodeCLI URL [$DefaultBaseUrl]"
|
||||
if ([string]::IsNullOrWhiteSpace($inputUrl)) {
|
||||
$BaseUrl = $DefaultBaseUrl
|
||||
} else {
|
||||
$BaseUrl = $inputUrl
|
||||
}
|
||||
|
||||
# Get API key
|
||||
while ([string]::IsNullOrWhiteSpace($ApiKey)) {
|
||||
$ApiKey = Read-Host "Enter your API key"
|
||||
@@ -263,8 +252,8 @@ function Main {
|
||||
}
|
||||
|
||||
# Validate inputs
|
||||
if ([string]::IsNullOrWhiteSpace($BaseUrl) -or [string]::IsNullOrWhiteSpace($ApiKey)) {
|
||||
Write-Error "Both URL and API key are required"
|
||||
if ([string]::IsNullOrWhiteSpace($ApiKey)) {
|
||||
Write-Error "API key is required"
|
||||
Write-Info "Use -Help for usage information"
|
||||
exit 1
|
||||
}
|
||||
@@ -274,32 +263,33 @@ function Main {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Remove trailing slash from URL
|
||||
$BaseUrl = $BaseUrl.TrimEnd('/')
|
||||
|
||||
Write-Info "Configuration:"
|
||||
Write-Info " Base URL: $BaseUrl"
|
||||
$maskedKey = if ($ApiKey.Length -gt 12) {
|
||||
"$($ApiKey.Substring(0, 8))...$($ApiKey.Substring($ApiKey.Length - 4))"
|
||||
} else {
|
||||
"$($ApiKey.Substring(0, [Math]::Min(4, $ApiKey.Length)))..."
|
||||
}
|
||||
Write-Info " API Key: $maskedKey"
|
||||
Write-Info "API Key: $maskedKey"
|
||||
Write-Host ""
|
||||
|
||||
# Test API connection
|
||||
$connectionSuccess = Test-ApiConnection -BaseUrl $BaseUrl -ApiKey $ApiKey
|
||||
# Test API connection and get working base URL
|
||||
$BaseUrl = Test-ApiConnection -ApiKey $ApiKey
|
||||
|
||||
if (-not $connectionSuccess) {
|
||||
if ([string]::IsNullOrWhiteSpace($BaseUrl)) {
|
||||
if ($Test) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
$continue = Read-Host "API test failed. Continue anyway? (y/N)"
|
||||
$continue = Read-Host "All API tests failed. Continue anyway? (y/N)"
|
||||
if ($continue -notmatch '^[Yy]$') {
|
||||
Write-Info "Setup cancelled"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# If user chooses to continue anyway, use default URL
|
||||
$BaseUrl = $DefaultBaseUrl
|
||||
Write-Warning "Using default URL: $BaseUrl"
|
||||
} else {
|
||||
Write-Info "Selected working base URL: $BaseUrl"
|
||||
}
|
||||
|
||||
# Exit if test only
|
||||
|
||||
Reference in New Issue
Block a user