callx

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 8 Imported by: 3

README

CallX

CallX HTTP Client easy call API for Golang

Build Status Codecov Go Report Card

Install
go get github.com/prongbang/callx
Benchmark
Benchmark_CallXRequests/GET
Benchmark_CallXRequests/GET-10         	   35043	     28867 ns/op
Benchmark_CallXRequests/POST
Benchmark_CallXRequests/POST-10        	   36608	     29991 ns/op
Benchmark_CallXRequests/POST-ENCODE
Benchmark_CallXRequests/POST-ENCODE-10 	   33936	     37955 ns/op
Benchmark_CallXRequests/PUT
Benchmark_CallXRequests/PUT-10         	   39570	     31499 ns/op
Benchmark_CallXRequests/PATCH
Benchmark_CallXRequests/PATCH-10       	   39568	     30819 ns/op
Benchmark_CallXRequests/DELETE
Benchmark_CallXRequests/DELETE-10      	   40272	     30470 ns/op
How to use
  • Using base URL
c := callx.Config{
    BaseURL: "https://jsonplaceholder.typicode.com",
    Timeout: 60,
}
req := callx.New(c)

data := req.Get("/todos/1")
fmt.Println(string(data.Data))
  • Custom request
c := callx.Config{
    Timeout: 60,
}
req := callx.New(c)

custom := callx.Custom{
    URL:    "https://httpbin.org/post",
    Method: http.MethodPost,
    Header: callx.Header{
        callx.Authorization: fmt.Sprintf("%s %s", callx.Bearer, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.Et9HFtf9R3GEMA0IICOfFMVXY7kkTX1wr4qCyhIf58U"),
    },
    Body: callx.Body{
        "username": "root",
        "password": "pass",
        "address": []string{
            "087654321",
            "089786756",
        },
    },
}
data := req.Req(custom)
fmt.Println(string(data.Data))
  • Custom request form encoded
c := callx.Config{
    Timeout: 60,
}
req := callx.New(c)

form := url.Values{}
form.Set("message", "Test")

custom := callx.Custom{
    URL:    "https://httpbin.org/post",
    Method: http.MethodPost,
    Header: callx.Header{
        callx.Authorization: "Bearer XTZ",
        callx.ContentType:   "application/x-www-form-urlencoded",
    },
    Form: strings.NewReader(form.Encode()),
}
data := req.Req(custom)
fmt.Println(string(data.Data))

Documentation

Index

Constants

View Source
const (
	Authorization = "Authorization"
	ContentType   = "Content-Type"
	Accept        = "Accept"
	Basic         = "Basic"
	Bearer        = "Bearer"
)

Constant Header

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body map[string]any

Body custom type

type CallX

type CallX interface {
	Get(url string) Response
	Post(url string, body interface{}) Response
	Patch(url string, body interface{}) Response
	Put(url string, body interface{}) Response
	Delete(url string) Response
	Req(custom Custom) Response
	AddInterceptor(intercept ...Interceptor)
	// contains filtered or unexported methods
}

CallX the interface

func New

func New(config Config) CallX

New callx

type Config

type Config struct {
	BaseURL string

	// Client name. Used in User-Agent request header.
	//
	// Default client name is used if not set.
	Name string

	// Maximum duration for full request writing and response reading (including body).
	//
	Timeout time.Duration

	// Maximum duration for full response reading (including body).
	//
	// By default response read timeout is unlimited.
	ReadTimeout time.Duration

	// Maximum duration for full request writing (including body).
	//
	// By default request write timeout is unlimited.
	WriteTimeout time.Duration

	// Idle keep-alive connections are closed after this duration.
	//
	// By default idle connections are closed after DefaultMaxIdleConnDuration.
	MaxIdleConnDuration time.Duration

	Interceptor []Interceptor

	// TLS config for https connections.
	//
	// Default TLS config is used if not set.
	TLSConfig *tls.Config

	// InsecureSkipVerify controls whether a client verifies the server's certificate chain and host name.
	InsecureSkipVerify bool

	// TCPDialer contains options to control a group of Dial calls.
	TCPDialer *fasthttp.TCPDialer

	// Maximum number of connections per each host which may be established.
	//
	// DefaultMaxConnsPerHost is used if not set.
	MaxConnsPerHost int

	// Per-connection buffer size for responses' reading.
	// This also limits the maximum header size.
	//
	// Default buffer size is used if 0.
	ReadBufferSize int

	// Per-connection buffer size for requests' writing.
	//
	// Default buffer size is used if 0.
	WriteBufferSize int

	// RetryIf controls whether a retry should be attempted after an error.
	//
	// By default will use isIdempotent function.
	RetryIf fasthttp.RetryIfFunc

	// StreamResponseBody enables response body streaming.
	StreamResponseBody bool
}

Config callx model

type Custom

type Custom struct {
	URL    string
	Method string
	Header Header
	Body   interface{}
	Form   Form
}

Custom callx request model

type Form added in v1.3.0

type Form io.Reader

Form custom type

type Header map[string]string

Header custom type

type Interceptor

type Interceptor interface {
	Request(req *fasthttp.Request)
	Response(res *fasthttp.Response)
}

Interceptor the interface

func HeaderInterceptor

func HeaderInterceptor(header Header) Interceptor

HeaderInterceptor provide a instance

func JSONContentTypeInterceptor

func JSONContentTypeInterceptor() Interceptor

JSONContentTypeInterceptor provide a instance

func LoggerInterceptor

func LoggerInterceptor() Interceptor

LoggerInterceptor provide a instance

type Response

type Response struct {
	Code int
	Data []byte
}

Response callx model

Jump to

Keyboard shortcuts

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