client

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 9 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 provides a fluent wrapper around net/http.

func New

func New() *Client

New creates a new HTTP client.

func (*Client) Delete

func (c *Client) Delete(url string) (*Response, error)

Delete makes a DELETE request.

func (*Client) Fake

func (c *Client) Fake(fake *Fake) *Client

Fake replaces the client with a fake for testing.

func (*Client) Get

func (c *Client) Get(url string) (*Response, error)

Get makes a GET request.

func (*Client) Head

func (c *Client) Head(url string) (*Response, error)

Head makes a HEAD request.

func (*Client) Options

func (c *Client) Options(url string) (*Response, error)

Options makes an OPTIONS request.

func (*Client) Patch

func (c *Client) Patch(url string, body any) (*Response, error)

Patch makes a PATCH request.

func (*Client) Post

func (c *Client) Post(url string, body any) (*Response, error)

Post makes a POST request.

func (*Client) Put

func (c *Client) Put(url string, body any) (*Response, error)

Put makes a PUT request.

func (*Client) WithBasicAuth

func (c *Client) WithBasicAuth(username, password string) *Client

WithBasicAuth sets Basic Auth headers.

func (*Client) WithBearerToken

func (c *Client) WithBearerToken(token string) *Client

WithBearerToken sets a Bearer token in the Authorization header.

func (*Client) WithContext

func (c *Client) WithContext(ctx context.Context) *Client

WithContext returns a new Client that uses the given context for requests.

func (*Client) WithHeader

func (c *Client) WithHeader(key, value string) *Client

WithHeader adds a header to the request.

func (*Client) WithRetry

func (c *Client) WithRetry(attempts int, delay time.Duration) *Client

WithRetry configures retry with exponential backoff.

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

WithTimeout sets the request timeout.

type Client2

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

Client2 provides HTTP client functionality

func NewClient

func NewClient() *Client2

NewClient creates a new HTTP client

func NewWithTimeout

func NewWithTimeout(timeout time.Duration) *Client2

NewWithTimeout creates a new HTTP client with a custom timeout

func (*Client2) Async

func (c *Client2) Async(method, url string, body any, result chan<- *Response2, errChan chan<- error, headers ...map[string]string)

Async sends a request asynchronously

func (*Client2) Delete

func (c *Client2) Delete(url string, headers ...map[string]string) (*Response2, error)

Delete sends a DELETE request

func (*Client2) Do

func (c *Client2) Do(method, url string, body any, headers ...map[string]string) (*Response2, error)

Do sends an HTTP request

func (*Client2) Get

func (c *Client2) Get(url string, headers ...map[string]string) (*Response2, error)

Get sends a GET request

func (*Client2) Head

func (c *Client2) Head(url string, headers ...map[string]string) (*Response2, error)

Head sends a HEAD request

func (*Client2) Options

func (c *Client2) Options(url string, headers ...map[string]string) (*Response2, error)

Options sends an OPTIONS request

func (*Client2) Patch

func (c *Client2) Patch(url string, body any, headers ...map[string]string) (*Response2, error)

Patch sends a PATCH request

func (*Client2) Post

func (c *Client2) Post(url string, body any, headers ...map[string]string) (*Response2, error)

Post sends a POST request

func (*Client2) Put

func (c *Client2) Put(url string, body any, headers ...map[string]string) (*Response2, error)

Put sends a PUT request

func (*Client2) Retry

func (c *Client2) Retry(method, url string, body any, maxRetries int, delay time.Duration, headers ...map[string]string) (*Response2, error)

Retry sends a request with retries

func (*Client2) SetBaseURL

func (c *Client2) SetBaseURL(baseURL string) *Client2

SetBaseURL sets the base URL for all requests

func (*Client2) SetHeader

func (c *Client2) SetHeader(key, value string) *Client2

SetHeader sets a default header for all requests

func (*Client2) SetHeaders

func (c *Client2) SetHeaders(headers map[string]string) *Client2

SetHeaders sets multiple default headers

func (*Client2) WithBearerToken

func (c *Client2) WithBearerToken(token string) *Client2

WithBearerToken sets bearer token authentication for the request

func (*Client2) WithTimeout

func (c *Client2) WithTimeout(timeout time.Duration) *Client2

WithTimeout sets the timeout for the client

type Fake

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

Fake is a fake HTTP client for testing.

func NewFake

func NewFake() *Fake

NewFake creates a new fake HTTP client.

func (*Fake) AssertNotSent

func (f *Fake) AssertNotSent(method, url string) bool

AssertNotSent checks that no request was made to the given URL.

func (*Fake) AssertSent

func (f *Fake) AssertSent(method, url string) bool

AssertSent checks if a request was made to the given URL.

func (*Fake) AssertSentCount

func (f *Fake) AssertSentCount(count int) bool

AssertSentCount checks the exact number of requests made.

func (*Fake) AssertSentJson

func (f *Fake) AssertSentJson(method, url string, body any) bool

AssertSentJson checks if a request was made with the given JSON body.

func (*Fake) Clear

func (f *Fake) Clear()

Clear resets all captured requests.

func (*Fake) Do

func (f *Fake) Do(method, url string, body io.Reader) (*Response, error)

Do executes the fake request and returns the canned response.

func (*Fake) Flush

func (f *Fake) Flush()

Flush removes all configured responses.

func (*Fake) GetLastRequest

func (f *Fake) GetLastRequest() *FakeRequest

GetLastRequest returns the last captured request, or nil if none.

func (*Fake) GetRequestCount

func (f *Fake) GetRequestCount() int

GetRequestCount returns the number of requests made.

func (*Fake) GetRequests

func (f *Fake) GetRequests() []*FakeRequest

GetRequests returns all captured requests.

func (*Fake) Sequence

func (f *Fake) Sequence(method, url string, responses ...*FakeResponse) *Fake

Sequence returns a different response for each sequential call to the same URL.

func (*Fake) When

func (f *Fake) When(method, url string, response *FakeResponse) *Fake

When sets up a canned response for a URL pattern.

func (*Fake) WhenDelete

func (f *Fake) WhenDelete(url string, statusCode int) *Fake

WhenDelete is a convenience method for DELETE requests.

func (*Fake) WhenGet

func (f *Fake) WhenGet(url string, statusCode int, body any) *Fake

WhenGet is a convenience method for GET requests.

func (*Fake) WhenPost

func (f *Fake) WhenPost(url string, statusCode int, body any) *Fake

WhenPost is a convenience method for POST requests.

func (*Fake) WhenPut

func (f *Fake) WhenPut(url string, statusCode int, body any) *Fake

WhenPut is a convenience method for PUT requests.

type FakeRequest

type FakeRequest struct {
	Method  string
	URL     string
	Body    []byte
	Headers map[string]string
}

FakeRequest captures a request made to the fake.

type FakeResponse

type FakeResponse struct {
	StatusCode int
	Body       any
	Headers    map[string]string
	Error      error
}

FakeResponse represents a canned response.

type Pool

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

Pool represents a pool of HTTP clients

func NewPool

func NewPool(size int) *Pool

NewPool creates a new pool of HTTP clients

func (*Pool) Get

func (p *Pool) Get() *Client2

Get retrieves a client from the pool

func (*Pool) Put

func (p *Pool) Put(client *Client2)

Put returns a client to the pool

func (*Pool) Size

func (p *Pool) Size() int

Size returns the pool size

type Request2

type Request2 struct {
	Method      string
	URL         string
	Headers     map[string]string
	Body        any
	Timeout     time.Duration
	BasicUser   string
	BasicPass   string
	BearerToken string
	QueryParams map[string]string
}

Request2 represents an HTTP request

type Response

type Response struct {
	Raw *http.Response
	// contains filtered or unexported fields
}

Response wraps an http.Response for easier consumption.

func (*Response) Body

func (r *Response) Body() string

Body returns the raw response bytes.

func (*Response) JSON

func (r *Response) JSON(target any) error

JSON unmarshals the response body into the given target.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the HTTP status code.

type Response2

type Response2 struct {
	StatusCode int
	Body       []byte
	Headers    http.Header
	Request    *http.Request
}

Response2 represents an HTTP response

func (*Response2) Header2

func (r *Response2) Header2(key string) string

Header2 returns a header value

func (*Response2) IsClientError

func (r *Response2) IsClientError() bool

IsClientError returns true if the status code is 4xx

func (*Response2) IsRedirect

func (r *Response2) IsRedirect() bool

IsRedirect returns true if the status code is 3xx

func (*Response2) IsServerError

func (r *Response2) IsServerError() bool

IsServerError returns true if the status code is 5xx

func (*Response2) IsSuccessful

func (r *Response2) IsSuccessful() bool

IsSuccessful returns true if the status code is 2xx

func (*Response2) JSON

func (r *Response2) JSON(v any) error

JSON unmarshals the response body into the provided interface

func (*Response2) String

func (r *Response2) String() string

String returns the response body as a string

Jump to

Keyboard shortcuts

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