connection

package
v1.21.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: LGPL-3.0 Imports: 10 Imported by: 1

Documentation

Overview

Package connection handles communication with the SCC API.

This package provides functionality for establishing a connection to the SUSE Customer Center (SCC) API, authenticating with the server, and performing HTTP requests.

It offers a high-level interface for interacting with the SCC API, abstracting away the underlying HTTP client and authentication details.

Index

Constants

View Source
const (
	// Default URL to be used for Options, which simply points to the reference
	// SCC server.
	DefaultBaseURL = "https://scc.suse.com"
	DefaultTimeout = 60 * time.Second
)
View Source
const (
	DefaultAPIVersion = "application/json,application/vnd.scc.suse.com.v4+json"
)

Variables

This section is empty.

Functions

func AddQuery added in v1.20.0

func AddQuery(req *http.Request, query map[string]string) *http.Request

Adds the given request query to the existing HTTP request object.

func AddRegcodeAuth

func AddRegcodeAuth(request *http.Request, regcode string)

func AddSystemAuth

func AddSystemAuth(request *http.Request, login string, password string)

func NewMockConnectionWithCredentials added in v1.20.0

func NewMockConnectionWithCredentials() (*MockConnection, *MockCredentials)

Types

type ApiConnection

type ApiConnection struct {
	Options      Options
	Credentials  Credentials
	ProfileCache ProfileCache
}

ApiConnection implements the 'Connection' interface, providing access to any server implementing the /connect API (see https://scc.suse.com/connect/v4/documentation for more info).

func New

func New(opts Options, creds Credentials) *ApiConnection

Returns an ApiConnection object initialized with the given Options and Credentials.

func (ApiConnection) BuildRequest

func (conn ApiConnection) BuildRequest(verb string, path string, body any) (*http.Request, error)

func (ApiConnection) BuildRequestRaw

func (conn ApiConnection) BuildRequestRaw(verb string, path string, body io.Reader) (*http.Request, error)

func (ApiConnection) Do

func (conn ApiConnection) Do(request *http.Request) ([]byte, error)

func (ApiConnection) GetCredentials

func (conn ApiConnection) GetCredentials() Credentials

type ApiError

type ApiError struct {
	Code             int
	Message          string `json:"error"`
	LocalizedMessage string `json:"localized_error"`
}

ApiError contains all the information for any given API error response. Don't build it directly, but use `ErrorFromResponse` instead.

func ErrorFromResponse

func ErrorFromResponse(resp *http.Response) *ApiError

Returns a new ApiError from the given response if it contained an API error response. Otherwise it just returns nil.

func (*ApiError) Error

func (ae *ApiError) Error() string

type Connection

type Connection interface {
	// Builds a http.Request and setup up headers. The body can provided as json marshable object
	// The created request can be used in a subsequent `Do` call.
	BuildRequest(verb string, path string, body any) (*http.Request, error)

	// Builds a http.Request and setup up headers. The body can provided as io.Reader
	BuildRequestRaw(verb string, path string, body io.Reader) (*http.Request, error)

	// Performs an HTTP request to the remote API. Returns the response body or
	// an error object.
	Do(*http.Request) ([]byte, error)

	// Returns the credentials object to be used for authenticated requests.
	GetCredentials() Credentials
}

Connection is to be implemented by any struct that attempts to perform requests against a remote resource that implements the /connect API.

type Credentials

type Credentials interface {
	// Returns true if we can authenticate at all, false otherwise.
	HasAuthentication() bool

	// Token() returns the last system token recorded by this client.
	//
	// The system token is rotated after each non-read operation and updated
	// through UpdateToken(). If no token has been recorded yet, the method
	// may return an empty string, indicating that the system has not been
	// registered.
	Token() (string, error)

	// UpdateToken() is a hook used by the transport layer to persist a new
	// system token received after a non-read request.
	//
	// This allows the client to store the updated token so that it can be
	// provided in subsequent requests through the Token() method.
	UpdateToken(string) error

	// Login() returns the username and password associated with the system.
	Login() (string, string, error)

	// SetLogin is a hook used by the transport layer to persist the system’s
	// login and password credentials.
	//
	// The system token is managed separately through UpdateToken().
	SetLogin(string, string) error
}

Credentials interface is to be implemented by any struct that knows how to save/restore credentials for a given system.

In SCC each system is identified by a triple of: login, password, and system token. The login and password are the standard credentials as expected on any service, and the system token is a mechanism that we have introduced in order to detect system duplicates.

Every time a client sends a non-read request to the SCC server, the server returns an updated system token. Clients must store this token and include it in subsequent requests.

In this library, client–server communication is handled at the transport layer. This interface defines two hooks that the transport layer uses to manage the system token:

  • Token() // returns the latest stored system token
  • UpdateToken() // updates/sets the stored system token with the latest value

type MockConnection added in v1.20.0

type MockConnection struct {
	mock.Mock
	// contains filtered or unexported fields
}

func NewMockConnection added in v1.20.0

func NewMockConnection(creds Credentials, hostname string) *MockConnection

func (*MockConnection) BuildRequest added in v1.20.0

func (m *MockConnection) BuildRequest(verb, path string, body any) (*http.Request, error)

func (*MockConnection) BuildRequestRaw added in v1.20.0

func (m *MockConnection) BuildRequestRaw(verb, path string, body io.Reader) (*http.Request, error)

func (*MockConnection) Do added in v1.20.0

func (m *MockConnection) Do(request *http.Request) ([]byte, error)

func (*MockConnection) GetCredentials added in v1.20.0

func (m *MockConnection) GetCredentials() Credentials

type MockCredentials added in v1.20.0

type MockCredentials struct {
	mock.Mock
}

func NewMockCredentials added in v1.20.0

func NewMockCredentials() *MockCredentials

func (*MockCredentials) HasAuthentication added in v1.20.0

func (m *MockCredentials) HasAuthentication() bool

func (*MockCredentials) Login added in v1.20.0

func (m *MockCredentials) Login() (string, string, error)

func (*MockCredentials) SetLogin added in v1.20.0

func (m *MockCredentials) SetLogin(login, password string) error

func (*MockCredentials) Token added in v1.20.0

func (m *MockCredentials) Token() (string, error)

func (*MockCredentials) UpdateToken added in v1.20.0

func (m *MockCredentials) UpdateToken(token string) error

type NoCredentials

type NoCredentials struct{}

NoCredentials is an empty implementation of the Credentials interface which simply returns false to the `HasAuthentication` function. Useful for building up connections to API resources which don't require authentication.

func (NoCredentials) HasAuthentication

func (NoCredentials) HasAuthentication() bool

func (NoCredentials) Login

func (NoCredentials) Login() (string, string, error)

func (NoCredentials) SetLogin

func (NoCredentials) SetLogin(string, string) error

func (NoCredentials) Token

func (NoCredentials) Token() (string, error)

func (NoCredentials) UpdateToken

func (NoCredentials) UpdateToken(string) error

type Options

type Options struct {
	// URL to be used for connections.
	URL string

	// Optional callback that enables connections to establish configuration for
	// HTTP proxies.
	Proxy ProxyCallbackFunc

	// Set a certificate to allow connections to TLS enabled API hosts with self
	// signed certificates.
	Certificate *x509.Certificate

	// True if a secure connection is required.
	Secure bool

	// Name of the application utilizing the library. This will added to the user
	// agent string.
	AppName string

	// Version of the application utilizing the library. Will be added to the user
	// agent string.
	Version string

	// Language used to display API error messages. Empty string means no specific
	// language settings.
	PreferedLanguage string

	// Timeout on how long to wait for an API response
	Timeout time.Duration
}

Options that are needed in order to form connections to the SCC API. See the `ApiConnection` struct.

func DefaultOptions

func DefaultOptions(appName, version, language string) Options

Returns the Options suitable for targeting the SCC reference server.

type ProfileCache added in v1.21.0

type ProfileCache interface {
	Clear()
	ResetClearCount()
}

Interface to Clear Profile Cache

type ProxyCallbackFunc

type ProxyCallbackFunc func(*http.Request) (*url.URL, error)

Function to provide custom proxy configuration on API connections (e.g. read proxy credentials from environment variables, or read proxy credentials from a $HOME/.curlrc file).

Jump to

Keyboard shortcuts

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