Skip to content

MetaDiv-AI/http_caller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http_caller

HTTP client with interface + builder pattern, generic request/response types, and streaming support.

Installation

go get github.com/MetaDiv-AI/http_caller

Quick Start

resp, err := http_caller.New[struct{}, User]("https://api.example.com/users/1").Get(ctx)

Builder Options

Method Description
New(baseURL) Create builder (required)
Param(key, value) Add URL query param
Params(map) Add multiple params
Header(key, value) Add request header
Headers(map) Add multiple headers
Body(ptr) Set JSON body (POST/PUT/PATCH)
WithClient(client) Custom HTTP client
WithDebugLogger(log) Enable debug logging

Usage Examples

GET with params and headers

resp, err := http_caller.New[struct{}, []User]("https://api.example.com/users").
    Param("page", "1").
    Param("limit", "10").
    Header("Authorization", "Bearer token").
    Get(ctx)

POST with body

type CreateReq struct { Name string `json:"name"` }
type User struct { ID int `json:"id"` }

req := &CreateReq{Name: "Alice"}
resp, err := http_caller.New[CreateReq, User]("https://api.example.com/users").
    Body(req).
    Post(ctx)

Access response (RawBody, Body, StatusCode)

raw := resp.RawBody
user := resp.Body
if resp.StatusCode == 200 { ... }

Stream (GET, SSE)

err := http_caller.New[struct{}, any]("https://api.example.com/events").
    Header("Accept", "text/event-stream").
    Stream(ctx, func(chunk []byte) error {
        fmt.Println(string(chunk))
        return nil
    })

StreamPost (POST with body, e.g. OpenAI-style)

err := http_caller.New[ChatReq, any]("https://api.example.com/chat").
    Body(&chatReq).
    StreamPost(ctx, func(chunk []byte) error {
        // process SSE chunk
        return nil
    })

Debug logging

log := logger.New().Development().Build()
defer log.Sync()
resp, _ := http_caller.New[Req, Resp]("https://api.example.com").
    WithDebugLogger(log).
    Get(ctx)

Custom client

client := &http.Client{Timeout: 10 * time.Second}
resp, _ := http_caller.New[Req, Resp]("https://api.example.com").
    WithClient(client).
    Get(ctx)

Response Type

Response[T] contains:

  • StatusCode – HTTP status code
  • Headers – Response headers
  • RawBody – Raw response body as string
  • Body – Parsed JSON into generic type T

Caller Interface

Builder implements Caller[TReq, TResp] for dependency injection and testing:

var caller http_caller.Caller[CreateReq, User] = builder
resp, err := caller.Post(ctx)

Dependencies

  • github.com/MetaDiv-AI/logger (for WithDebugLogger)
  • go.uber.org/zap (transitive)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages