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
go get github.com/not-empty/grit-requester
import "github.com/not-empty/grit-requester"
type LoginInput struct {
Email string `json:"email"`
Pass string `json:"password"`
}
type LoginOutput struct {
Token string `json:"token"`
Name string `json:"name"`
}
conf := gritrequester.StaticConfig{
"auth": {
Token: "your-integration-token",
Secret: "your-integration-secret",
Context: "app-test",
BaseUrl: "https://auth.microservice.local",
},
}
client := gritrequester.NewRequestObj(conf)
msReq := gritrequester.MsRequest{
MSName: "auth",
Method: "POST",
Path: "/auth/login",
Body: LoginInput{
Email: "test@example.com",
Pass: "123456",
},
}
resp, err := gritrequester.DoMsRequest[LoginOutput](client, msReq, true)
if err != nil {
log.Fatal("Request failed:", err)
}
fmt.Println("Received token:", resp.Name)
Test coverage: 100%
Run tests:
go test -v -cover ./...
Visualize coverage:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
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