Documentation
¶
Overview ¶
Package prest provides a generic HTTP client for pREST APIs that authenticates automatically via OAuth2 client credentials.
Client[T] fetches and caches an access token, refreshing it before expiry, and unmarshals JSON responses directly into T.
Basic usage ¶
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
}
client, err := prest.NewClient[[]Product](
"client-id",
"client-secret",
"client_credentials",
"api:read",
"https://auth.example.com/oauth/token",
)
if err != nil {
log.Fatal(err)
}
products, err := client.Get(ctx, "https://api.example.com/products", nil)
Pagination ¶
page, err := client.GetPaginated(ctx, "https://api.example.com/products", 50, 0)
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalidCredentials = errors.New("invalid credentials") ErrInvalidEndpoints = errors.New("invalid endpoints") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client[T any] struct { // contains filtered or unexported fields }
Client is a generic pREST API client that authenticates via OAuth2 client credentials and unmarshals responses into T.
func NewClient ¶
func NewClient[T any]( clientID string, clientSecret string, grantType string, scope string, authEndpoint string, client ...*stdhttp.Client, ) (*Client[T], error)
NewClient creates a new pREST Client.
func (*Client[T]) Get ¶
Get sends a GET request to endpoint, appending the given query params, and unmarshals the JSON response body into T.
Click to show internal directories.
Click to hide internal directories.