client

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package client provides a comprehensive API client for Snyk with rate limiting, retry logic, and integration with go-application-framework.

Package client provides API versioning support for Snyk REST API.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthentication = errors.New("authentication failed")
	ErrNotFound       = errors.New("resource not found")
	ErrRateLimited    = errors.New("rate limited by server")
	ErrServerError    = errors.New("server error")
	ErrTimeout        = errors.New("request timeout")
	ErrMaxRetries     = errors.New("maximum retries exceeded")
)

Sentinel errors for common API error conditions.

View Source
var ErrInvalidVersion = &APIError{
	Message: "invalid API version format (expected YYYY-MM-DD or YYYY-MM-DD~stability)",
}

ErrInvalidVersion is returned when an API version string is invalid.

Functions

func IsAuthError

func IsAuthError(err error) bool

IsAuthError returns true if the error is an authentication error.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the error is a 404 Not Found.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true if the error can be retried.

Types

type APIError

type APIError struct {
	StatusCode    int
	Message       string
	RequestID     string
	SnykRequestID string
	Retryable     bool
	RetryAfter    time.Duration
	Cause         error
}

APIError provides detailed error information from API responses.

func NewAPIError

func NewAPIError(statusCode int, message, requestID string, headers http.Header) *APIError

NewAPIError creates a new APIError from an HTTP response.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap returns the underlying cause for errors.Is/As support.

type APIVersion

type APIVersion string

APIVersion represents a Snyk REST API version.

const (
	Version20251105 APIVersion = "2025-11-05" // Latest GA (current spec)
	Version20240422 APIVersion = "2024-04-22" // go-application-framework default
	Version20231127 APIVersion = "2023-11-27" // Common stable version
)

Common stable API versions.

const DefaultAPIVersion APIVersion = "2025-11-05"

DefaultAPIVersion is the latest stable GA version used by default.

func ParseVersion

func ParseVersion(s string) (APIVersion, error)

ParseVersion parses a string into an APIVersion, validating the format. Returns an error if the format is invalid.

func (APIVersion) Beta

func (v APIVersion) Beta() APIVersion

Beta returns the beta version of this API version.

func (APIVersion) Experimental

func (v APIVersion) Experimental() APIVersion

Experimental returns the experimental version of this API version.

func (APIVersion) String

func (v APIVersion) String() string

String returns the string representation of the API version.

func (APIVersion) WithStability

func (v APIVersion) WithStability(stability string) APIVersion

WithStability adds a stability suffix to the version.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the main API client that handles authentication, rate limiting, and request execution using go-application-framework.

func New

func New(ctx context.Context, opts ...Option) (*Client, error)

New creates a new API client with the given options.

func (*Client) APIVersion

func (c *Client) APIVersion() APIVersion

APIVersion returns the configured API version.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the v1 API base URL.

func (*Client) Close

func (c *Client) Close() error

Close gracefully shuts down the client.

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, req *http.Request) (*http.Response, error)

Execute performs an HTTP request with rate limiting and retries.

func (*Client) GetConfiguration

func (c *Client) GetConfiguration() configuration.Configuration

GetConfiguration returns the framework configuration.

func (*Client) GetLogger

func (c *Client) GetLogger() *zerolog.Logger

GetLogger returns the client logger.

func (*Client) GetNetworkAccess

func (c *Client) GetNetworkAccess() networking.NetworkAccess

GetNetworkAccess returns the underlying network access for direct requests.

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient returns the underlying HTTP client for direct requests.

func (*Client) RestBaseURL

func (c *Client) RestBaseURL() string

RestBaseURL returns the REST API base URL.

func (*Client) Token

func (c *Client) Token() string

Token returns the authentication token.

type Option

type Option func(*config)

Option is a functional option for configuring the Client.

func WithBaseURL

func WithBaseURL(v1URL, restURL string) Option

WithBaseURL sets custom API base URLs. This is useful for regional endpoints or testing.

func WithEngine

func WithEngine(engine workflow.Engine) Option

WithEngine sets the workflow engine to use. If not provided, a standalone engine will be created.

func WithLogger

func WithLogger(logger *zerolog.Logger) Option

WithLogger sets the logger for the client.

func WithRateLimit

func WithRateLimit(burstSize int, period time.Duration) Option

WithRateLimit sets rate limiting configuration.

func WithRetryPolicy

func WithRetryPolicy(maxRetries int, baseDelay, maxDelay time.Duration) Option

WithRetryPolicy sets retry configuration.

func WithUserAgent

func WithUserAgent(ua string) Option

WithUserAgent sets a custom user agent prefix.

func WithVersion

func WithVersion(version APIVersion) Option

WithVersion sets a specific API version for requests. If not specified, DefaultAPIVersion is used.

Example:

client.WithVersion(client.Version20240422)
client.WithVersion(client.DefaultAPIVersion.Beta())
client.WithVersion("2024-01-01")

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL