httpclient

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: MIT Imports: 11 Imported by: 0

README

HTTP Client

This package provides a resilient HTTP client with built-in fault tolerance patterns for GET, POST, PUT, and DELETE requests.

Features

  • Circuit Breaker: Prevents cascading failures by monitoring request success/failure rates
  • Bulkhead: Isolates resources to prevent one failing service from affecting others
  • Timeout Handling: 15-second default timeout for all requests
  • JSON Support: Built-in JSON serialization/deserialization
  • Request Logging: Interface for transaction logging (bring your own logger)

Resilience Patterns

The client implements enterprise-grade resilience patterns automatically:

  • Circuit breaker monitors request patterns and fails fast when services are down
  • Bulkhead pattern isolates different service calls to prevent resource exhaustion
  • Configurable timeouts prevent hanging requests

Dependencies

  • github.com/jarcoal/httpmock (for testing)

Usage

Setup
import "github.com/mariusfa/golf/httpclient"

client := httpclient.NewHttpClient()
GET Requests
var dto MyDto
requestId := "req-123" // For transaction logging
url := "http://api.example.com/users"
headers := map[string]string{"Accept": "application/json"}

getRequest := httpclient.NewGetRequest(requestId, headers, url)
err := client.GetJson(getRequest, &dto)
if err != nil {
    // Handle error (circuit breaker, timeout, etc.)
}
POST Requests
requestDto := MyDto{Name: "John Doe"}
requestId := "req-124"
url := "http://api.example.com/users"
headers := map[string]string{"Content-Type": "application/json"}

postRequest := httpclient.NewPostRequest(requestId, headers, url, requestDto)
err := client.PostJson(postRequest, nil)
if err != nil {
    // Handle error
}
PUT Requests
updateDto := MyDto{Name: "Jane Doe"}
requestId := "req-125"
url := "http://api.example.com/users/1"
headers := map[string]string{"Content-Type": "application/json"}

putRequest := httpclient.NewPutRequest(requestId, headers, url, updateDto)
err := client.PutJson(putRequest, nil)
if err != nil {
    // Handle error
}
DELETE Requests
requestId := "req-126"
url := "http://api.example.com/users/1"
headers := map[string]string{}

deleteRequest := httpclient.NewDeleteRequest(requestId, headers, url)
err := client.Delete(deleteRequest)
if err != nil {
    // Handle error
}

Error Handling

The client returns errors for various failure scenarios:

  • Network timeouts (after 15 seconds)
  • Circuit breaker open (service unavailable)
  • Bulkhead capacity exceeded
  • HTTP error status codes
  • JSON serialization/deserialization errors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SafeURL added in v0.5.0

func SafeURL(baseURL string, pathParams []string, queryParams map[string]string) (string, error)

Types

type DeleteRequest added in v0.5.0

type DeleteRequest struct {
	Headers map[string]string
	Url     string
}

func NewDeleteRequest added in v0.5.0

func NewDeleteRequest(headers map[string]string, url string) *DeleteRequest

type GetRequest added in v0.4.0

type GetRequest struct {
	UserId  string
	Headers map[string]string
	Url     string
}

func NewGetRequest added in v0.4.0

func NewGetRequest(headers map[string]string, url string) *GetRequest

type HttpClient

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

func NewHttpClient

func NewHttpClient() *HttpClient

func (*HttpClient) Delete added in v0.5.0

func (c *HttpClient) Delete(ctx context.Context, request *DeleteRequest) error

func (*HttpClient) GetJson added in v0.4.0

func (c *HttpClient) GetJson(ctx context.Context, request *GetRequest, dto any) error

func (*HttpClient) PostJson added in v0.4.0

func (c *HttpClient) PostJson(ctx context.Context, request *PostRequest, responseDto any) error

func (*HttpClient) PutJson added in v0.5.0

func (c *HttpClient) PutJson(ctx context.Context, request *PutRequest, responseDto any) error

type PostRequest added in v0.4.0

type PostRequest struct {
	Headers map[string]string
	Url     string
	Body    any
}

func NewPostRequest added in v0.4.0

func NewPostRequest(headers map[string]string, url string, body any) *PostRequest

type PutRequest added in v0.5.0

type PutRequest struct {
	Headers map[string]string
	Url     string
	Body    any
}

func NewPutRequest added in v0.5.0

func NewPutRequest(headers map[string]string, url string, body any) *PutRequest

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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