Documentation
¶
Index ¶
- Constants
- func DoRequest[T any](payload any, config *HttpClientManager) result.Result[T]
- func GetContentTypeFromResponse(resp *http.Response) types.ContentType
- func GetRequestID(ctx context.Context) string
- func RequestIDMiddleware(next http.Handler) http.Handler
- type DecoderFunc
- type HTTPClient
- type HttpClientManager
- func (c *HttpClientManager) AddCertFile(certFile string)
- func (c *HttpClientManager) AddContentType(contentType types.ContentType)
- func (c *HttpClientManager) AddFastHTTP()
- func (c *HttpClientManager) AddFiles(files map[string]string)
- func (c *HttpClientManager) AddFormValues(values map[string]string)
- func (c *HttpClientManager) AddHeaders(headers map[string]string)
- func (c *HttpClientManager) AddIsTLS(isTLS bool)
- func (c *HttpClientManager) AddKeyFile(keyFile string)
- func (c *HttpClientManager) AddQueryParams(params map[string]any)
- func (c *HttpClientManager) AddSkipVerify(skipVerify bool)
- func (c *HttpClientManager) AddTimeout(timeout time.Duration)
- func (c *HttpClientManager) Clear()
- type RequestOption
- func WithContentType(contentType types.ContentType) RequestOption
- func WithFastHTTP() RequestOption
- func WithFile(fieldName, filePath string) RequestOption
- func WithFormValue(key, value string) RequestOption
- func WithHeader(key, value string) RequestOption
- func WithLogger(log *log.Log) RequestOption
- func WithMethod(method string) RequestOption
- func WithQueryParams(params map[string]any) RequestOption
- func WithTLSConfig(certFile, keyFile string, skipVerify bool) RequestOption
- func WithTimeout(duration time.Duration) RequestOption
- func WithURL(requestURL string) RequestOption
Constants ¶
const ( MethodGet = http.MethodGet MethodPost = http.MethodPost MethodPut = http.MethodPut MethodDelete = http.MethodDelete MethodPatch = http.MethodPatch MethodOptions = http.MethodOptions MethodHead = http.MethodHead )
HTTP method constants
const ( ContentTypeJSON types.ContentType = "application/json" ContentTypeXML types.ContentType = "application/xml" ContentTypeFormURLEncoded types.ContentType = "application/x-www-form-urlencoded" ContentTypeMultipartFormData types.ContentType = "multipart/form-data" ContentTypeTextPlain types.ContentType = "text/plain" ContentTypeOctetStream types.ContentType = "application/octet-stream" ContentTypeHTML types.ContentType = "text/html" ContentTypeJavaScript types.ContentType = "application/javascript" ContentTypeCSV types.ContentType = "text/csv" ContentTypePDF types.ContentType = "application/pdf" ContentTypeZIP types.ContentType = "application/zip" ContentTypeGZIP types.ContentType = "application/gzip" ContentTypeYAML types.ContentType = "application/x-yaml" ContentTypeMsgPack types.ContentType = "application/msgpack" ContentTypePlainText types.ContentType = "text/plain" )
ContentType constants
Variables ¶
This section is empty.
Functions ¶
func DoRequest ¶
func DoRequest[T any](payload any, config *HttpClientManager) result.Result[T]
DoRequest executes a complete HTTP request with automatic encoding/decoding. It validates URLs, constructs query parameters, handles TLS, and decodes responses.
func GetContentTypeFromResponse ¶
func GetContentTypeFromResponse(resp *http.Response) types.ContentType
func GetRequestID ¶
GetRequestID extracts the UUID from the context.
Types ¶
type DecoderFunc ¶
Decoder function type
func GetDecoder ¶
func GetDecoder(contentType types.ContentType) (DecoderFunc, error)
type HTTPClient ¶
type HTTPClient interface {
Do(config *HttpClientManager, body []byte, contentType types.ContentType) ([]byte, error)
}
HTTPClient defines the interface for HTTP client implementations. It abstracts the HTTP client to support both standard and FastHTTP clients.
type HttpClientManager ¶
type HttpClientManager struct {
URL string
Method string
Headers map[string]string
QueryParams map[string]any
Timeout time.Duration
ContentType types.ContentType
Files map[string]string // Field name -> File path (for multipart/form-data)
FormValues map[string]string // Key-Value for form-urlencoded or multipart
IsTLS bool
CertFile string
KeyFile string
SkipVerify bool
Log *log.Log
UseFastHTTP bool // New flag to enable fastHTTP
}
HttpClientManager holds the HTTP request configurations
func NewHttpClientManager ¶
func NewHttpClientManager(requestURL string, opts ...RequestOption) *HttpClientManager
NewHttpClientManager initializes a new HttpClientManager with default values
func (*HttpClientManager) AddCertFile ¶
func (c *HttpClientManager) AddCertFile(certFile string)
func (*HttpClientManager) AddContentType ¶
func (c *HttpClientManager) AddContentType(contentType types.ContentType)
func (*HttpClientManager) AddFastHTTP ¶
func (c *HttpClientManager) AddFastHTTP()
func (*HttpClientManager) AddFiles ¶
func (c *HttpClientManager) AddFiles(files map[string]string)
func (*HttpClientManager) AddFormValues ¶
func (c *HttpClientManager) AddFormValues(values map[string]string)
func (*HttpClientManager) AddHeaders ¶
func (c *HttpClientManager) AddHeaders(headers map[string]string)
func (*HttpClientManager) AddIsTLS ¶
func (c *HttpClientManager) AddIsTLS(isTLS bool)
func (*HttpClientManager) AddKeyFile ¶
func (c *HttpClientManager) AddKeyFile(keyFile string)
func (*HttpClientManager) AddQueryParams ¶
func (c *HttpClientManager) AddQueryParams(params map[string]any)
func (*HttpClientManager) AddSkipVerify ¶
func (c *HttpClientManager) AddSkipVerify(skipVerify bool)
func (*HttpClientManager) AddTimeout ¶
func (c *HttpClientManager) AddTimeout(timeout time.Duration)
func (*HttpClientManager) Clear ¶
func (c *HttpClientManager) Clear()
type RequestOption ¶
type RequestOption func(*HttpClientManager)
RequestOption defines a functional option for configuring the HTTP client
func WithContentType ¶
func WithContentType(contentType types.ContentType) RequestOption
WithContentType sets the content type
func WithFile ¶
func WithFile(fieldName, filePath string) RequestOption
WithFile adds a file to a multipart/form-data request
func WithFormValue ¶
func WithFormValue(key, value string) RequestOption
WithFormValue adds form data (for x-www-form-urlencoded or multipart)
func WithQueryParams ¶
func WithQueryParams(params map[string]any) RequestOption
**✅ WithQueryParams - Adds Query Parameters**
func WithTLSConfig ¶
func WithTLSConfig(certFile, keyFile string, skipVerify bool) RequestOption
WithTLSConfig sets up TLS options
func WithTimeout ¶
func WithTimeout(duration time.Duration) RequestOption
WithTimeout sets a timeout