@@ -15,37 +15,44 @@ import (
15
15
)
16
16
17
17
func main () {
18
- // Command line options
18
+ // --- Command line flags ---
19
+ // These allow the user to override the API key, set a custom timeout, and enable verbose output.
19
20
var (
20
21
apiKey = flag .String ("api-key" , "" , "OpenAI API key (overrides OPENAI_API_KEY)" )
21
22
timeout = flag .Duration ("timeout" , 10 * time .Second , "Request timeout" )
22
23
verbose = flag .Bool ("verbose" , false , "Show request details" )
23
24
)
24
25
flag .Parse ()
25
26
26
- // Get API key
27
+ // --- API Key Resolution ---
28
+ // Priority: --api-key flag > OPENAI_API_KEY env var.
27
29
key := * apiKey
28
30
if key == "" {
29
31
key = os .Getenv ("OPENAI_API_KEY" )
30
32
}
31
33
if key == "" {
34
+ // Fail fast if no API key is provided.
32
35
fmt .Fprintln (os .Stderr , "No API key provided" )
33
36
fmt .Fprintln (os .Stderr , " Use:" )
34
37
fmt .Fprintln (os .Stderr , " • --api-key 'sk-...'" )
35
38
fmt .Fprintln (os .Stderr , " • or export OPENAI_API_KEY='sk-...'" )
36
39
os .Exit (1 )
37
40
}
38
41
39
- // Create client with timeout
42
+ // --- Context with Timeout ---
43
+ // Ensures the request does not hang indefinitely.
40
44
ctx , cancel := context .WithTimeout (context .Background (), * timeout )
41
45
defer cancel ()
42
46
47
+ // --- Client Initialization ---
48
+ // The client is configured with the resolved API key and timeout.
43
49
client := openai .NewClient (
44
50
option .WithAPIKey (key ),
45
51
option .WithRequestTimeout (* timeout ),
46
52
)
47
53
48
- // Verbose mode
54
+ // --- Verbose Logging ---
55
+ // Show diagnostic info if requested.
49
56
if * verbose {
50
57
fmt .Printf ("Testing OpenAI API connection...\n " )
51
58
fmt .Printf ("Timeout: %v\n " , * timeout )
@@ -57,7 +64,8 @@ func main() {
57
64
fmt .Println ()
58
65
}
59
66
60
- // Test connection
67
+ // --- API Validation ---
68
+ // The core check: attempt to list models. This is a lightweight endpoint and a good proxy for API health.
61
69
if err := validateAPI (ctx , client ); err != nil {
62
70
handleError (err )
63
71
os .Exit (1 )
@@ -69,16 +77,21 @@ func main() {
69
77
}
70
78
}
71
79
80
+ // validateAPI attempts a simple API call to verify connectivity and authentication.
81
+ // Returns an error if the call fails for any reason.
72
82
func validateAPI (ctx context.Context , client openai.Client ) error {
73
- // Simple test: list models
83
+ // Simple test: list models (minimal permissions required, fast response)
74
84
_ , err := client .Models .List (ctx )
75
85
return err
76
86
}
77
87
88
+ // handleError provides structured, actionable error messages for common API issues.
89
+ // This function distinguishes between API errors (with status codes) and generic/network errors.
78
90
func handleError (err error ) {
79
91
fmt .Fprintf (os .Stderr , "API Error: %v\n \n " , err )
80
92
81
93
if apiErr , ok := err .(* apierror.Error ); ok {
94
+ // Handle known API error codes with specific guidance.
82
95
switch apiErr .StatusCode {
83
96
case 401 :
84
97
fmt .Fprintln (os .Stderr , " Authentication issue:" )
@@ -98,10 +111,12 @@ func handleError(err error) {
98
111
fmt .Fprintln (os .Stderr , " • OpenAI service is temporarily unavailable" )
99
112
fmt .Fprintln (os .Stderr , " • Try again in a few minutes" )
100
113
default :
114
+ // For unhandled status codes, print the code and message for debugging.
101
115
fmt .Fprintf (os .Stderr , " Error code: %d\n " , apiErr .StatusCode )
102
116
fmt .Fprintf (os .Stderr , " Message: %s\n " , apiErr .Message )
103
117
}
104
118
} else {
119
+ // Handle network and unknown errors.
105
120
errStr := err .Error ()
106
121
if strings .Contains (errStr , "Connection refused" ) {
107
122
fmt .Fprintln (os .Stderr , " Connection issue:" )
@@ -112,9 +127,18 @@ func handleError(err error) {
112
127
fmt .Fprintln (os .Stderr , " • Check your internet connection" )
113
128
fmt .Fprintln (os .Stderr , " • Increase timeout with --timeout 30s" )
114
129
} else {
130
+ // Catch-all for unexpected errors.
115
131
fmt .Fprintf (os .Stderr , "Unknown error: %s\n " , errStr )
116
132
}
117
133
}
118
134
135
+ // Always provide a pointer to the official error documentation for further troubleshooting.
119
136
fmt .Fprintln (os .Stderr , "\n For more help: https://platform.openai.com/docs/guides/error-codes" )
120
137
}
138
+
139
+ // ---
140
+ // TODO (future improvements):
141
+ // - Add support for multiple API keys
142
+ // - Add support for multiple models
143
+ // - Add support for multiple endpoints
144
+ // - Add support for multiple API versions
0 commit comments