httpclient

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2024 License: MPL-2.0 Imports: 26 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 CheckDeprecationHeader

func CheckDeprecationHeader(resp *http.Response, log logger.Logger)

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

func IsIdempotentHTTPMethod added in v0.0.46

func IsIdempotentHTTPMethod(method string) bool

IsIdempotentHTTPMethod checks if the given HTTP method is idempotent.

func SetDefaultValuesClientConfig added in v0.1.54

func SetDefaultValuesClientConfig(config *ClientConfig)

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

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 {
	AuthToken       string
	AuthTokenExpiry time.Time
	Logger          logger.Logger
	Concurrency     *concurrency.ConcurrencyHandler
	Integration     *APIIntegration
	// contains filtered or unexported fields
}

Master struct/object

func BuildClient

func BuildClient(config ClientConfig, populateDefaultValues bool, log logger.Logger) (*Client, error)

BuildClient creates a new HTTP client with the provided configuration.

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 {
	Integration                 APIIntegration
	HideSensitiveData           bool `json:"hide_sensitive_data"`
	CustomCookies               []*http.Cookie
	MaxRetryAttempts            int  `json:"max_retry_attempts"`
	MaxConcurrentRequests       int  `json:"max_concurrent_requests"`
	EnableDynamicRateLimiting   bool `json:"enable_dynamic_rate_limiting"`
	CustomTimeout               time.Duration
	TokenRefreshBufferPeriod    time.Duration
	TotalRetryDuration          time.Duration
	FollowRedirects             bool `json:"follow_redirects"`
	MaxRedirects                int  `json:"max_redirects"`
	EnableConcurrencyManagement bool `json:"enable_concurrency_management"`
	MandatoryRequestDelay       time.Duration
	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.

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