callx

package module
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2025 License: MIT Imports: 8 Imported by: 4

README

CallX 🚀

Codecov Go Report Card Go Reference

A lightweight, fast, and easy-to-use HTTP client for Go. Make API calls with just a few lines of code!

✨ Features

  • 🚀 Ultra-fast performance - Optimized for speed
  • 🛠 Simple API - Easy to learn and use
  • 🔧 Highly customizable - Full control over requests
  • 🧪 Well tested - High test coverage

⚡️ Performance

CallX is designed for optimal performance. Here are the benchmark results:

HTTP Method Operations Time per Operation
GET 41,756 31,823 ns/op
POST 38,692 35,787 ns/op
POST-ENCODE 28,848 39,314 ns/op
PUT 31,401 35,046 ns/op
PATCH 38,923 30,094 ns/op
DELETE 41,100 29,195 ns/op

📦 Installation

go get github.com/prongbang/callx

🚀 Quick Start

Basic Usage
// Create a client with base URL
c := callx.Config{
    BaseURL: "https://jsonplaceholder.typicode.com",
    Timeout: 60,
}
req := callx.New(c)

// Make a GET request
data := req.Get("/todos/1")
fmt.Println(string(data.Data))

🔥 Advanced Features

Custom Request with Authentication
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, "your-token"),
    },
    Body: callx.Body{
        "username": "root",
        "password": "pass",
        "address": []string{
            "087654321",
            "089786756",
        },
    },
}

data := req.Req(custom)
fmt.Println(string(data.Data))
Form-encoded Requests
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

	Cookies 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(maxBodyLog ...int) Interceptor

LoggerInterceptor provide a instance

type Response

type Response struct {
	Code    int
	Data    []byte
	Cookies map[string]string
}

Response callx model

Jump to

Keyboard shortcuts

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