|
| 1 | +// cmd/validate-api/main.go |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/openai/openai-go" |
| 13 | + "github.com/openai/openai-go/internal/apierror" |
| 14 | + "github.com/openai/openai-go/option" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + // Command line options |
| 19 | + var ( |
| 20 | + apiKey = flag.String("api-key", "", "OpenAI API key (overrides OPENAI_API_KEY)") |
| 21 | + timeout = flag.Duration("timeout", 10*time.Second, "Request timeout") |
| 22 | + verbose = flag.Bool("verbose", false, "Show request details") |
| 23 | + ) |
| 24 | + flag.Parse() |
| 25 | + |
| 26 | + // Get API key |
| 27 | + key := *apiKey |
| 28 | + if key == "" { |
| 29 | + key = os.Getenv("OPENAI_API_KEY") |
| 30 | + } |
| 31 | + if key == "" { |
| 32 | + fmt.Fprintln(os.Stderr, "No API key provided") |
| 33 | + fmt.Fprintln(os.Stderr, " Use:") |
| 34 | + fmt.Fprintln(os.Stderr, " • --api-key 'sk-...'") |
| 35 | + fmt.Fprintln(os.Stderr, " • or export OPENAI_API_KEY='sk-...'") |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + |
| 39 | + // Create client with timeout |
| 40 | + ctx, cancel := context.WithTimeout(context.Background(), *timeout) |
| 41 | + defer cancel() |
| 42 | + |
| 43 | + client := openai.NewClient( |
| 44 | + option.WithAPIKey(key), |
| 45 | + option.WithRequestTimeout(*timeout), |
| 46 | + ) |
| 47 | + |
| 48 | + // Verbose mode |
| 49 | + if *verbose { |
| 50 | + fmt.Printf("Testing OpenAI API connection...\n") |
| 51 | + fmt.Printf("Timeout: %v\n", *timeout) |
| 52 | + displayKey := key |
| 53 | + if len(key) > 7 { |
| 54 | + displayKey = key[:7] |
| 55 | + } |
| 56 | + fmt.Printf("🔑 API Key: %s...\n", displayKey) |
| 57 | + fmt.Println() |
| 58 | + } |
| 59 | + |
| 60 | + // Test connection |
| 61 | + if err := validateAPI(ctx, client); err != nil { |
| 62 | + handleError(err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Println("✅ API connection successful") |
| 67 | + if *verbose { |
| 68 | + fmt.Println("🎉 Your OpenAI configuration is working correctly!") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func validateAPI(ctx context.Context, client openai.Client) error { |
| 73 | + // Simple test: list models |
| 74 | + _, err := client.Models.List(ctx) |
| 75 | + return err |
| 76 | +} |
| 77 | + |
| 78 | +func handleError(err error) { |
| 79 | + fmt.Fprintf(os.Stderr, "❌ API Error: %v\n\n", err) |
| 80 | + |
| 81 | + if apiErr, ok := err.(*apierror.Error); ok { |
| 82 | + switch apiErr.StatusCode { |
| 83 | + case 401: |
| 84 | + fmt.Fprintln(os.Stderr, "🔐 Authentication issue:") |
| 85 | + fmt.Fprintln(os.Stderr, " • Verify your API key is correct") |
| 86 | + fmt.Fprintln(os.Stderr, " • Check at https://platform.openai.com/api-keys") |
| 87 | + fmt.Fprintln(os.Stderr, " • Ensure your account has available credits") |
| 88 | + case 403: |
| 89 | + fmt.Fprintln(os.Stderr, "🚫 Access denied:") |
| 90 | + fmt.Fprintln(os.Stderr, " • Check your API key permissions") |
| 91 | + fmt.Fprintln(os.Stderr, " • Verify your subscription plan") |
| 92 | + case 429: |
| 93 | + fmt.Fprintln(os.Stderr, "⏱️ Rate limit exceeded:") |
| 94 | + fmt.Fprintln(os.Stderr, " • Wait a few minutes") |
| 95 | + fmt.Fprintln(os.Stderr, " • Check your quota at https://platform.openai.com/usage") |
| 96 | + case 500, 502, 503, 504: |
| 97 | + fmt.Fprintln(os.Stderr, "🌐 OpenAI server issue:") |
| 98 | + fmt.Fprintln(os.Stderr, " • OpenAI service is temporarily unavailable") |
| 99 | + fmt.Fprintln(os.Stderr, " • Try again in a few minutes") |
| 100 | + default: |
| 101 | + fmt.Fprintf(os.Stderr, "�� Error code: %d\n", apiErr.StatusCode) |
| 102 | + fmt.Fprintf(os.Stderr, "📝 Message: %s\n", apiErr.Message) |
| 103 | + } |
| 104 | + } else { |
| 105 | + errStr := err.Error() |
| 106 | + if strings.Contains(errStr, "connection refused") { |
| 107 | + fmt.Fprintln(os.Stderr, "🌐 Connection issue:") |
| 108 | + fmt.Fprintln(os.Stderr, " • Check your internet connection") |
| 109 | + fmt.Fprintln(os.Stderr, " • Verify your proxy/firewall settings") |
| 110 | + } else if strings.Contains(errStr, "timeout") { |
| 111 | + fmt.Fprintln(os.Stderr, "⏰ Connection timeout:") |
| 112 | + fmt.Fprintln(os.Stderr, " • Check your internet connection") |
| 113 | + fmt.Fprintln(os.Stderr, " • Increase timeout with --timeout 30s") |
| 114 | + } else { |
| 115 | + fmt.Fprintf(os.Stderr, "❓ Unknown error: %s\n", errStr) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fmt.Fprintln(os.Stderr, "\n💡 For more help: https://platform.openai.com/docs/guides/error-codes") |
| 120 | +} |
0 commit comments