utils

package
v3.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SerializationMethodToContentType = map[string]string{
		"json":      "application/json",
		"form":      "application/x-www-form-urlencoded",
		"multipart": "multipart/form-data",
		"raw":       "application/octet-stream",
		"string":    "text/plain",
	}
)

Functions

func AsSecuritySource

func AsSecuritySource(security interface{}) func(context.Context) (interface{}, error)

func ConsumeRawBody added in v3.1.0

func ConsumeRawBody(res *http.Response) ([]byte, error)

func Contains

func Contains(slice []string, item string) bool

func DrainBody added in v3.8.0

func DrainBody(res *http.Response)

func GenerateURL

func GenerateURL(_ context.Context, serverURL, path string, pathParams interface{}, globals interface{}) (string, error)

func GetEnv added in v3.3.0

func GetEnv(name, defaultValue string) string

GetEnv returns the value of the environment variable named by the key or the defaultValue if the environment variable is not set.

func MarshalJSON

func MarshalJSON(v interface{}, tag reflect.StructTag, topLevel bool) ([]byte, error)

func MatchContentType

func MatchContentType(contentType string, pattern string) bool

func MatchStatusCodes

func MatchStatusCodes(expectedCodes []string, statusCode int) bool

func NewOAuth2ClientCredentials added in v3.5.0

func NewOAuth2ClientCredentials(tokenEndpoint string, clientID string, clientSecret string) oAuth2ClientCredentials

Creates a new OAuth2 Client Credentials grant.

func PopulateHeaders

func PopulateHeaders(_ context.Context, req *http.Request, headers interface{}, globals interface{})

func PopulateQueryParams

func PopulateQueryParams(_ context.Context, req *http.Request, queryParams interface{}, globals interface{}, allowEmptyValue map[string]struct{}) error

func PopulateSecurity

func PopulateSecurity(ctx context.Context, req *http.Request, securitySource func(context.Context) (interface{}, error)) error

func ReplaceParameters

func ReplaceParameters(stringWithParams string, params map[string]string) string

func Retry

func Retry(ctx context.Context, r Retries, operation func() (*http.Response, error)) (*http.Response, error)

func SerializeRequestBody

func SerializeRequestBody(_ context.Context, request interface{}, nullable, optional bool, requestFieldName, serializationMethod, tag string) (io.Reader, string, error)

func UnmarshalJSON

func UnmarshalJSON(b []byte, v interface{}, tag reflect.StructTag, topLevel bool, requiredFields []string) error

func UnmarshalJsonFromResponseBody

func UnmarshalJsonFromResponseBody(body io.Reader, out interface{}, tag string) error

func UnmarshalJsonFromString added in v3.8.0

func UnmarshalJsonFromString(json string, out interface{}, tag string) error

Types

type BackoffStrategy deprecated

type BackoffStrategy = retry.BackoffStrategy

Deprecated: Use retry.BackoffStrategy instead.

type HTTPClient added in v3.5.0

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient provides an interface for supplying the SDK with a custom HTTP client

type OAuth2AccessTokenCredentials added in v3.5.0

type OAuth2AccessTokenCredentials struct {
	// Pre-authorized OAuth2 access token.
	AccessToken string
}

Describes an OAuth2 authorization grant implementation that contains a pre-authorized OAuth2 access token. Using this grant implementation will bypass the OAuth2 session store, as there is no need to call the OAuth2 token endpoint.

func NewOAuth2AccessTokenCredentials added in v3.5.0

func NewOAuth2AccessTokenCredentials(accessToken string) OAuth2AccessTokenCredentials

Creates a new OAuth2 authorization grant with pre-authorized access token.

func (OAuth2AccessTokenCredentials) GrantType added in v3.5.0

func (c OAuth2AccessTokenCredentials) GrantType() string

Returns the authorization grant type. This implementation is custom and returns an unspecified "access_token" value rather than conflating this grant type with the differing "implicit" grant type.

func (OAuth2AccessTokenCredentials) Hash added in v3.5.0

Unused, however required to implement OAuth2AuthorizationGrantor.

func (OAuth2AccessTokenCredentials) NewSession added in v3.5.0

func (c OAuth2AccessTokenCredentials) NewSession(_ context.Context, _ HTTPClient, _ []string) (*oAuth2Session, error)

Creates a new OAuth2 Session.

func (OAuth2AccessTokenCredentials) TokenEndpoint added in v3.5.0

func (c OAuth2AccessTokenCredentials) TokenEndpoint() string

Unused, however required to implement OAuth2AuthorizationGrantor.

type OAuth2AuthorizationGrantor added in v3.5.0

type OAuth2AuthorizationGrantor interface {
	// Returns the authorization grant type, such as "client_credentials" or
	// "password".
	GrantType() string

	// Returns the authorization grant as a hash suitable for mapping keys.
	Hash() string

	// Returns a new session for the authorization grant.
	NewSession(ctx context.Context, httpClient HTTPClient, scopes []string) (*oAuth2Session, error)

	// Returns the token endpoint for the authorization grant.
	TokenEndpoint() string
}

Describes an interface for OAuth2 authorization grants, such as pre-authorized access token, client credentials, or resource owner password credentials.

type OAuth2SessionStore added in v3.5.0

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

Describes storage for OAuth2Session.

func NewOAuth2SessionStore added in v3.5.0

func NewOAuth2SessionStore() *OAuth2SessionStore

Creates an initialized OAuth2 session store.

func (OAuth2SessionStore) DeleteCredentialsSession added in v3.5.0

func (s OAuth2SessionStore) DeleteCredentialsSession(authorizationGrantor OAuth2AuthorizationGrantor)

Deletes a stored credentials session.

func (OAuth2SessionStore) GetSession added in v3.5.0

func (s OAuth2SessionStore) GetSession(ctx context.Context, httpClient HTTPClient, authorizationGrantor OAuth2AuthorizationGrantor, scopes []string) (*oAuth2Session, error)

Returns a stored session, or creates a new session, for the given scopes.

type Retries

type Retries struct {
	Config      *retry.Config
	StatusCodes []string
}

type RetryConfig deprecated

type RetryConfig = retry.Config

Deprecated: Use retry.Config instead.

type Timer added in v3.2.0

type Timer interface {
	Start(duration time.Duration)
	Stop()
	C() <-chan time.Time
}

type UnionCandidate added in v3.8.0

type UnionCandidate struct {
	Matched              int // Count of matched fields (includes inexact)
	Inexact              int // Count of fields with unknown/unrecognized enum values
	Unmatched            int // Count of struct fields not found in raw JSON
	AdditionalProperties int // Count of fields captured by additionalProperties
	Type                 any // The union type enum value
	Value                any // The unmarshaled value
}

UnionCandidate represents a candidate type during union deserialization

func PickBestUnionCandidate added in v3.8.0

func PickBestUnionCandidate(candidates []UnionCandidate, rawJSON []byte) *UnionCandidate

PickBestUnionCandidate selects the best union type candidate according to `betterCandidate`