Documentation
¶
Overview ¶
Package api provides helpers for initializing and using the StackEye API client.
This package centralizes the pattern of creating an SDK client from the CLI configuration, providing consistent error handling across all commands that need API access.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrConfigNotLoaded is returned when GetClient is called before config is loaded. ErrConfigNotLoaded = errors.New("api: configuration not loaded") // ErrNoCurrentContext is returned when no context is set in the configuration. ErrNoCurrentContext = errors.New("api: no context configured") // ErrContextNotFound is returned when the current context doesn't exist. ErrContextNotFound = errors.New("api: context not found") // ErrNoAPIKey is returned when the context has no API key configured. ErrNoAPIKey = errors.New("api: no API key configured for context") )
Error types for API client initialization failures.
Functions ¶
func GetClient ¶
GetClient creates and returns an SDK client from the current configuration context.
The function:
- Gets the loaded configuration via the configured getter
- Retrieves the current context
- Validates that an API key is present
- Creates and returns an SDK client
Returns an error if:
- Configuration is not loaded (ErrConfigNotLoaded)
- No current context is set (ErrNoCurrentContext)
- Current context doesn't exist (ErrContextNotFound)
- No API key is configured (ErrNoAPIKey)
Example:
client, err := api.GetClient()
if err != nil {
return fmt.Errorf("failed to initialize API client: %w", err)
}
// Use client for API calls
func GetClientOptions ¶
GetClientOptions returns SDK client options for use by commands that create clients directly (e.g., login, config set-api-key). This allows them to use verbosity logging without going through GetClient().
Example:
opts := api.GetClientOptions() c := client.New(apiKey, apiURL, opts...)
func GetClientWithContext ¶
GetClientWithContext creates an SDK client for a specific named context. This is useful for commands that need to operate on a different context than the current one.
Returns the same errors as GetClient, but for the specified context name.
func RequireClient ¶
RequireClient creates and returns an SDK client, exiting on error.
This is a convenience function for commands that require authentication. If the client cannot be created, it prints an error message to stderr and exits with code 1.
Example:
func runMyCommand() error {
client := api.RequireClient()
// Use client for API calls
}
func SetConfigGetter ¶
SetConfigGetter sets the function used to get the loaded configuration. This should be called during CLI initialization to wire up the config loader.
Example usage in root.go init():
api.SetConfigGetter(GetConfig)
func SetVerbosityGetter ¶
func SetVerbosityGetter(getter func() int)
SetVerbosityGetter sets the function used to get the verbosity level. This should be called during CLI initialization to wire up the verbosity flag.
Example usage in root.go init():
api.SetVerbosityGetter(GetVerbosity)
Types ¶
This section is empty.