HTTP client with interface + builder pattern, generic request/response types, and streaming support.
go get github.com/MetaDiv-AI/http_callerresp, err := http_caller.New[struct{}, User]("https://api.example.com/users/1").Get(ctx)| 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 |
resp, err := http_caller.New[struct{}, []User]("https://api.example.com/users").
Param("page", "1").
Param("limit", "10").
Header("Authorization", "Bearer token").
Get(ctx)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)raw := resp.RawBody
user := resp.Body
if resp.StatusCode == 200 { ... }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
})err := http_caller.New[ChatReq, any]("https://api.example.com/chat").
Body(&chatReq).
StreamPost(ctx, func(chunk []byte) error {
// process SSE chunk
return nil
})log := logger.New().Development().Build()
defer log.Sync()
resp, _ := http_caller.New[Req, Resp]("https://api.example.com").
WithDebugLogger(log).
Get(ctx)client := &http.Client{Timeout: 10 * time.Second}
resp, _ := http_caller.New[Req, Resp]("https://api.example.com").
WithClient(client).
Get(ctx)Response[T] contains:
StatusCode– HTTP status codeHeaders– Response headersRawBody– Raw response body as stringBody– Parsed JSON into generic type T
Builder implements Caller[TReq, TResp] for dependency injection and testing:
var caller http_caller.Caller[CreateReq, User] = builder
resp, err := caller.Post(ctx)- github.com/MetaDiv-AI/logger (for WithDebugLogger)
- go.uber.org/zap (transitive)