grit-requester is a Go library to abstract requests to microservices built using Grit.
Features:
- 🔁 Automatic retry on
401 Unauthorized
- 🔐 Per-service token cache with concurrency safety
- 💉 Config and HTTP client injection (perfect for testing)
- 📦 Full support for generics (
any
) in request/response - 🧠 Context-aware: all requests support context.Context for cancellation, timeouts, and APM tracing
go get github.com/not-empty/grit-requester
import "github.com/not-empty/grit-requester"
type RequestPayload struct {
Example string `json:"example"`
}
type ResponseData struct {
ID string `json:"id"`
}
conf := gritrequester.StaticConfig{}
conf.Set("example", gritrequester.MSAuthConf{
Token: "your-integration-token",
Secret: "your-integration-secret",
Context: "example-test",
BaseUrl: "https://example.microservice.local",
})
client := gritrequester.NewRequestObj(conf)
msReq := gritrequester.MsRequest{
MSName: "example",
Method: "POST",
Path: "/example/add",
Body: RequestPayload{
Example: "Example test",
},
}
resp, err := gritrequester.DoMsRequest[ResponseData](context.TODO(), client, msReq, true)
if err != nil {
log.Fatal("Request failed:", err)
}
fmt.Println("Received ID:", resp.Data.ID)
Test coverage: 100%
Run tests:
./test.sh
Visualize coverage:
open ./coverage/coverage-unit.html
MsRequest
: generic structure for describing a requestResponseData[T]
: generic expected response wrapperRequesterObj
: manages tokens, configs, and the HTTP clientTokenCache
: thread-safe in-memory token cacheDoMsRequest
: core function to execute the request
MIT © Not Empty
Not Empty Foundation - Free codes, full minds