http

package
v0.0.1-20260114-103824... Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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

func GetRequestID(ctx context.Context) string

GetRequestID extracts the UUID from the context.

func RequestIDMiddleware

func RequestIDMiddleware(next http.Handler) http.Handler

Middleware adds a unique UUID to the context for each request.

Types

type DecoderFunc

type DecoderFunc func(io.Reader, any) error

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 WithFastHTTP

func WithFastHTTP() RequestOption

WithFastHTTP sets the flag to use fastHTTP

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 WithHeader

func WithHeader(key, value string) RequestOption

WithHeader sets a custom header

func WithLogger

func WithLogger(log *log.Log) RequestOption

WithLogger sets the log

func WithMethod

func WithMethod(method string) RequestOption

WithMethod sets the HTTP method

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

func WithURL

func WithURL(requestURL string) RequestOption

**✅ WithURL - Validates & Sets URL**

Jump to

Keyboard shortcuts

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