package main
import (
"context"
"fmt"
"time"
"github.com/wundergraph/cosmo/router-plugin/httpclient"
)
func main() {
// Create HTTP client with configuration options
client := httpclient.New(
httpclient.WithBaseURL("https://api.example.com"),
httpclient.WithTimeout(10 * time.Second),
httpclient.WithHeader("Accept", "application/json"),
)
// Create a context
ctx := context.Background()
// Make a GET request
resp, err := client.Get(ctx, "/users/1")
if err != nil {
panic(err)
}
// Check if the request was successful
if !resp.IsSuccess() {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
return
}
// Parse the response into a struct using generics
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
user, err := httpclient.UnmarshalTo[User](resp)
if err != nil {
panic(err)
}
fmt.Printf("User: %s (Email: %s)\n", user.Name, user.Email)
}