rustic

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 17 Imported by: 0

README

Rustic - A minimalistic HTTP client in Go

Yet another HTTP Client in go with very simple yet essential features

Go Reference

Features:

  • http client with type safety
  • Different http configurations support
  • Configure timeouts, circuitBreaker etc
  • supports open telemetry, currently with stdOut exporter(can be changed to http exported in order to collect with any telemetry backend)
  • Add metrics either via open telemetry or prometheus metrics

NOTE: For circuit breaker https://github.com/sony/gobreaker is used.

Usage

go get github.com/rag594/rustic

How to use it

// UserPost represents the post/blog by a specific user 
type UserPost struct {
	UserId int    `json:"userId"`
	Id     int    `json:"id"`
	Title  string `json:"title"`
	Body   string `json:"body"`
}
// configure your http client
client := httpClient.NewHTTPClient(httpClient.WithTraceEnabled(true))
url := "https://jsonplaceholder.typicode.com/posts"

// define your query params
params := url2.Values{}
params.Add("userId", "1")

// configure your circuit breaker(currently only sony circuit breaker is supported)
st := &gobreaker.Settings{}
st.Name = "HTTP GET"

st.ReadyToTrip = func(counts gobreaker.Counts) bool {
        failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
        return counts.Requests >= 3 && failureRatio >= 0.6
}

cb := gobreaker.NewCircuitBreaker[any](*st)
Trigger HTTP client
post, err := rustic.GET[[]UserPost](context.Background(),
        url,
        rustic.WithQueryParams(params),
        rustic.WithHttpClient(client),
        rustic.WithTimeout(time.Duration(1)*time.Second),
        rustic.WithCircuitBreaker(cb), 
			)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(post)
Important information wrt context
  1. If you pass the context as nil, then context is set as Background
  2. If you pass the timeout, new context is derived from parent context
  3. If you wish to maintain context timeout at the parent level, do not pass timeout

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GET

func GET[Res any](ctx context.Context, url string, opts ...HTTPConfigOptions) (*Res, error)

GET http method with Res as response type

func POST

func POST[Req, Res any](ctx context.Context, url string, req *Req, opts ...HTTPConfigOptions) (*Res, error)

POST http method with Req as request type and Res as response type

func POSTFormData

func POSTFormData[Res any](ctx context.Context, url string, opts ...HTTPConfigOptions) (*Res, error)

POSTFormData with Res as response type and allows application/x-www-form-urlencoded -> formData

func POSTMultiPartFormData

func POSTMultiPartFormData[Res any](ctx context.Context, url string, files map[string]string, opts ...HTTPConfigOptions) (*Res, error)

POSTMultiPartFormData with Res as response type, map of files with key as fieldName and value as filePath

func PUT

func PUT[Req, Res any](ctx context.Context, url string, req *Req, opts ...HTTPConfigOptions) (*Res, error)

PUT http method with Req as request type and Res as response type

Types

type HTTPConfig

type HTTPConfig struct {
	HttpClient          *httpClient.HTTPClient
	Timeout             time.Duration
	Headers             http.Header
	QueryParams         netUrl.Values
	FormParams          netUrl.Values
	MultipartFormParams map[string]string
	CircuitBreaker      *gobreaker.CircuitBreaker[any] // currently only github.com/sony/gobreaker/v2 is supported
}

HTTPConfig different http configurations

type HTTPConfigOptions

type HTTPConfigOptions func(*HTTPConfig)

func WithFormParams

func WithFormParams(c netUrl.Values) HTTPConfigOptions

func WithHeaders

func WithHeaders(c http.Header) HTTPConfigOptions

func WithMultiPartFormParams

func WithMultiPartFormParams(c map[string]string) HTTPConfigOptions

func WithQueryParams

func WithQueryParams(c netUrl.Values) HTTPConfigOptions

func WithTimeout

func WithTimeout(t time.Duration) HTTPConfigOptions

Directories

Path Synopsis
example/httpGet command
example/httpPut command

Jump to

Keyboard shortcuts

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