httpclient

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2024 License: MPL-2.0 Imports: 25 Imported by: 5

Documentation

Overview

httpclient/client.go

The `http_client` package provides a configurable HTTP client tailored for interacting with specific APIs.

It supports different authentication methods, including "bearer" and "oauth". The client is designed with a focus on concurrency management, structured error handling, and flexible configuration options. The package offers a default timeout, custom backoff strategies, dynamic rate limiting, and detailed logging capabilities. The main `Client` structure encapsulates all necessary components, like the baseURL, authentication details, and an embedded standard HTTP client.

httpclient/client_configuration.go Description: This file contains functions to load and validate configuration values from a JSON file or environment variables.

headers/headers.go

httpmethod/httpmethod.go

httpclient/integration.go

httpclient/request.go

httpclient/utility.go

Index

Constants

View Source
const (
	DefaultLogLevelString              = "LogLevelInfo"
	DefaultLogOutputFormatString       = "pretty"
	DefaultLogConsoleSeparator         = "	"
	DefaultLogExportPath               = "/defaultlogs"
	DefaultMaxRetryAttempts            = 3
	DefaultMaxConcurrentRequests       = 1
	DefaultExportLogs                  = false
	DefaultHideSensitiveData           = false
	DefaultEnableDynamicRateLimiting   = false
	DefaultCustomTimeout               = 5 * time.Second
	DefaultTokenRefreshBufferPeriod    = 2 * time.Minute
	DefaultTotalRetryDuration          = 5 * time.Minute
	DefaultFollowRedirects             = false
	DefaultMaxRedirects                = 5
	DefaultEnableConcurrencyManagement = false
)
View Source
const ConfigFileExtension = ".json"

Variables

This section is empty.

Functions

func IsIdempotentHTTPMethod added in v0.0.46

func IsIdempotentHTTPMethod(method string) bool

IsIdempotentHTTPMethod checks if the given HTTP method is idempotent.

Types

type APIIntegration added in v0.1.54

type APIIntegration interface {
	GetFQDN() string
	ConstructURL(endpoint string) string
	GetAuthMethodDescriptor() string
	CheckRefreshToken() error
	PrepRequestParamsAndAuth(req *http.Request) error
	PrepRequestBody(body interface{}, method string, endpoint string) ([]byte, error)
	MarshalMultipartRequest(fields map[string]string, files map[string]string) ([]byte, string, error)
	GetSessionCookies() ([]*http.Cookie, error)
}

APIIntegration is an interface that defines the methods required for an API integration. These are obtained from go-api-http-client-integrations. The methods defined in this interface are used by the HTTP client to authenticate and prepare requests for the API.

type Client

type Client struct {

	// Integration
	Integration *APIIntegration

	// Logger
	Sugar *zap.SugaredLogger

	// Concurrency Mananger
	Concurrency *concurrency.ConcurrencyHandler
	// contains filtered or unexported fields
}

Master struct/object

func (*Client) CheckDeprecationHeader added in v0.2.6

func (c *Client) CheckDeprecationHeader(resp *http.Response)

CheckDeprecationHeader checks the response headers for the Deprecation header and logs a warning if present.

func (*Client) DoMultiPartRequest added in v0.1.51

func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]string, formDataFields map[string]string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, out interface{}) (*http.Response, error)

Example: var result MyResponseType resp, err := client.DoMultiPartRequest("POST", "/api/upload", files, formDataFields, fileContentTypes, formDataPartHeaders, &result)

if err != nil {
    // Handle error
}

// Use `result` or `resp` as needed

func (*Client) DoRequest

func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*http.Response, error)

type ClientConfig added in v0.0.12

type ClientConfig struct {
	// Interface which implements the APIIntegration patterns. Integration handles all server/endpoint specific configuration, auth and vars.
	Integration APIIntegration

	// TODO
	Sugar *zap.SugaredLogger

	// Wether or not empty values will be set or an error thrown for missing items.
	PopulateDefaultValues bool

	// HideSenitiveData controls if sensitive data will be visible in logs. Debug option which should be True in production use.
	HideSensitiveData bool `json:"hide_sensitive_data"`

	// CustomCookies allows implementation of persistent, session wide cookies.
	CustomCookies []*http.Cookie

	// MaxRetry Attempts limits the amount of retries the client will perform on requests which are deemd retriable.
	MaxRetryAttempts int `json:"max_retry_attempts"`

	// MaxConcurrentRequests limits the amount of Semaphore tokens available to the client and therefor limits concurrent requests.
	MaxConcurrentRequests int `json:"max_concurrent_requests"`

	// EnableDynamicRateLimiting // TODO because I don't know.
	EnableDynamicRateLimiting bool `json:"enable_dynamic_rate_limiting"`

	// CustomTimeout // TODO also because I don't know.
	CustomTimeout time.Duration

	// TokenRefreshBufferPeriod is the duration of time before the token expires in which it's deemed
	// more sensible to replace the token rather then carry on using it.
	TokenRefreshBufferPeriod time.Duration

	// TotalRetryDuration // TODO maybe this should be called context?
	TotalRetryDuration time.Duration

	// FollowRedirects allows the client to follow redirections when they're returned from a request.
	FollowRedirects bool `json:"follow_redirects"`

	// MaxRedirects is the maximum amount of redirects the client will follow before throwing an error.
	MaxRedirects int `json:"max_redirects"`

	// EnableConcurrencyManagement when false bypasses any concurrency management to allow for a simpler request flow.
	EnableConcurrencyManagement bool `json:"enable_concurrency_management"`

	// MandatoryRequestDelay is a short, usually sub 0.5 second, delay after every request as to not overwhelm an endpoint.
	// Can be set to nothing if you want to be lightning fast!
	MandatoryRequestDelay time.Duration

	// RetryEligiableRequests when false bypasses any retry logic for a simpler request flow.
	RetryEligiableRequests bool `json:"retry_eligiable_requests"`
}

Options/Variables for Client

func LoadConfigFromEnv added in v0.0.75

func LoadConfigFromEnv() (*ClientConfig, error)

LoadConfigFromEnv loads HTTP client configuration settings from environment variables. If any environment variables are not set, the default values defined in the constants are used instead.

func LoadConfigFromFile added in v0.0.77

func LoadConfigFromFile(filepath string) (*ClientConfig, error)

LoadConfigFromFile loads http client configuration settings from a JSON file.

func (*ClientConfig) Build added in v0.2.6

func (c *ClientConfig) Build() (*Client, error)

BuildClient creates a new HTTP client with the provided configuration.

func (*ClientConfig) SetDefaultValuesClientConfig added in v0.2.6

func (c *ClientConfig) SetDefaultValuesClientConfig()

SetDefaultValuesClientConfig sets default values for the client configuration. Ensuring that all fields have a valid or minimum value.

type UploadState added in v0.1.53

type UploadState struct {
	LastUploadedByte int64
	sync.Mutex
}

UploadState represents the state of an upload operation, including the last uploaded byte. This struct is used to track the progress of file uploads for resumable uploads and to resume uploads from the last uploaded byte.

Jump to

Keyboard shortcuts

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