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
- func AddQuery(req *http.Request, query map[string]string) *http.Request
- func AddRegcodeAuth(request *http.Request, regcode string)
- func AddSystemAuth(request *http.Request, login string, password string)
- func NewMockConnectionWithCredentials() (*MockConnection, *MockCredentials)
- type ApiConnection
- func (conn ApiConnection) BuildRequest(verb string, path string, body any) (*http.Request, error)
- func (conn ApiConnection) BuildRequestRaw(verb string, path string, body io.Reader) (*http.Request, error)
- func (conn ApiConnection) Do(request *http.Request) ([]byte, error)
- func (conn ApiConnection) GetCredentials() Credentials
- type ApiError
- type Connection
- type Credentials
- type MockConnection
- func (m *MockConnection) BuildRequest(verb, path string, body any) (*http.Request, error)
- func (m *MockConnection) BuildRequestRaw(verb, path string, body io.Reader) (*http.Request, error)
- func (m *MockConnection) Do(request *http.Request) ([]byte, error)
- func (m *MockConnection) GetCredentials() Credentials
- type MockCredentials
- type NoCredentials
- type Options
- type ProfileCache
- type ProxyCallbackFunc
Constants ¶
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 )
const (
DefaultAPIVersion = "application/json,application/vnd.scc.suse.com.v4+json"
)
Variables ¶
This section is empty.
Functions ¶
func AddRegcodeAuth ¶
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 (ApiConnection) BuildRequestRaw ¶
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 ¶
Returns a new ApiError from the given response if it contained an API error response. Otherwise it just returns nil.
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
func NewMockConnection ¶ added in v1.20.0
func NewMockConnection(creds Credentials, hostname string) *MockConnection
func (*MockConnection) BuildRequest ¶ added in v1.20.0
func (*MockConnection) BuildRequestRaw ¶ added in v1.20.0
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
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) 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 ¶
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