mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-03-31 15:07:49 +01:00
37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
package ionscale
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"github.com/bufbuild/connect-go"
|
|
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1/ionscalev1connect"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func NewClient(clientAuth ClientAuth, serverURL string, insecureSkipVerify bool) (api.IonscaleServiceClient, io.Closer, error) {
|
|
tlsConfig := &tls.Config{
|
|
InsecureSkipVerify: insecureSkipVerify,
|
|
}
|
|
|
|
client := &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: tlsConfig,
|
|
},
|
|
}
|
|
|
|
interceptors := connect.WithInterceptors(NewAuthenticationInterceptor(clientAuth))
|
|
return api.NewIonscaleServiceClient(client, serverURL, interceptors), nil, nil
|
|
}
|
|
|
|
func NewAuthenticationInterceptor(clientAuth ClientAuth) connect.UnaryInterceptorFunc {
|
|
return func(next connect.UnaryFunc) connect.UnaryFunc {
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
|
token, _ := clientAuth.GetToken()
|
|
req.Header().Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
return next(ctx, req)
|
|
}
|
|
}
|
|
}
|