Files
ionscale/pkg/client/ionscale/client.go
T
2022-06-03 14:25:31 +02:00

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)
}
}
}