http

package
v0.0.0-...-8a0997d Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents an HTTP client with customizable options

func NewClient

func NewClient(options ...ClientOption) *Client

NewClient creates a new HTTP client with the given options

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *Request) (*Response, error)

Do executes an HTTP request and returns the response with detailed timing information

type ClientOption

type ClientOption func(*Client)

ClientOption is a function that configures a Client

func WithBaseURL

func WithBaseURL(baseURL string) ClientOption

WithBaseURL sets the base URL for the client

func WithHeader

func WithHeader(key, value string) ClientOption

WithHeader adds a header to the client

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

WithTimeout sets the timeout for the client

type Request

type Request struct {
	Method      string
	Path        string
	QueryParams url.Values
	Headers     map[string]string
	Body        interface{}
}

Request represents an HTTP request

func NewRequest

func NewRequest(method, path string) *Request

NewRequest creates a new HTTP request

func (*Request) Build

func (r *Request) Build(baseURL string) (*http.Request, error)

Build constructs an http.Request from the Request

func (*Request) WithBody

func (r *Request) WithBody(body interface{}) *Request

WithBody sets the body of the request

func (*Request) WithHeader

func (r *Request) WithHeader(key, value string) *Request

WithHeader adds a header to the request

func (*Request) WithQueryParam

func (r *Request) WithQueryParam(key, value string) *Request

WithQueryParam adds a query parameter to the request

func (*Request) WithQueryParams

func (r *Request) WithQueryParams(params map[string]string) *Request

WithQueryParams adds multiple query parameters to the request

type Response

type Response struct {
	StatusCode   int
	Status       string
	Headers      http.Header
	Body         io.ReadCloser
	ResponseTime time.Duration
	Timing       TimingInfo
	// contains filtered or unexported fields
}

Response represents an HTTP response

func (*Response) GetBody

func (r *Response) GetBody() ([]byte, error)

GetBody returns the response body as a byte array

func (*Response) GetBodyAsJSON

func (r *Response) GetBodyAsJSON(v interface{}) error

GetBodyAsJSON unmarshals the response body into the provided interface

func (*Response) GetBodyAsString

func (r *Response) GetBodyAsString() (string, error)

GetBodyAsString returns the response body as a string

func (*Response) GetContentTransferTimeMillis

func (r *Response) GetContentTransferTimeMillis() int64

GetContentTransferTimeMillis returns the content transfer time in milliseconds

func (*Response) GetDNSLookupTimeMillis

func (r *Response) GetDNSLookupTimeMillis() int64

GetDNSLookupTimeMillis returns the DNS lookup time in milliseconds

func (*Response) GetHeader

func (r *Response) GetHeader(key string) string

GetHeader returns the value of the specified header

func (*Response) GetResponseTimeMillis

func (r *Response) GetResponseTimeMillis() int64

GetResponseTimeMillis returns the response time in milliseconds

func (*Response) GetTCPConnectTimeMillis

func (r *Response) GetTCPConnectTimeMillis() int64

GetTCPConnectTimeMillis returns the TCP connection time in milliseconds

func (*Response) GetTLSHandshakeTimeMillis

func (r *Response) GetTLSHandshakeTimeMillis() int64

GetTLSHandshakeTimeMillis returns the TLS handshake time in milliseconds

func (*Response) GetTimeToFirstByteMillis

func (r *Response) GetTimeToFirstByteMillis() int64

GetTimeToFirstByteMillis returns the time to first byte in milliseconds

func (*Response) GetTotalTimeMillis

func (r *Response) GetTotalTimeMillis() int64

GetTotalTimeMillis returns the total time in milliseconds

func (*Response) IsClientError

func (r *Response) IsClientError() bool

IsClientError returns true if the response status code is in the 4xx range

func (*Response) IsRedirect

func (r *Response) IsRedirect() bool

IsRedirect returns true if the response status code is in the 3xx range

func (*Response) IsServerError

func (r *Response) IsServerError() bool

IsServerError returns true if the response status code is in the 5xx range

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if the response status code is in the 2xx range

type TimingInfo

type TimingInfo struct {
	// Time when the request started
	StartTime time.Time

	// DNS lookup time
	DNSLookupTime time.Duration

	// TCP connection time
	TCPConnectTime time.Duration

	// TLS handshake time (for HTTPS)
	TLSHandshakeTime time.Duration

	// Time to first byte (TTFB)
	TimeToFirstByte time.Duration

	// Content transfer time
	ContentTransferTime time.Duration

	// Total response time
	TotalTime time.Duration
}

TimingInfo stores detailed timing information for an HTTP request

Jump to

Keyboard shortcuts

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