Skip to content

Commit

Permalink
Allow client options and request headers to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 9, 2024
1 parent b34a758 commit dd43359
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDispatchEndpoint(t *testing.T) {
// Send a request for the identity function, and check that the
// input was echoed back.
req := dispatch.NewRequest("identity", dispatch.Input(dispatch.Int(11)))
res, err := client.Run(context.Background(), req)
res, err := client.Run(context.Background(), nil, req)
if err != nil {
t.Fatal(err)
} else if res.Status() != dispatch.OKStatus {
Expand All @@ -52,7 +52,7 @@ func TestDispatchEndpoint(t *testing.T) {
}

// Try running a function that has not been registered.
res, err = client.Run(context.Background(), dispatch.NewRequest("not_found", dispatch.Input(dispatch.Int(22))))
res, err = client.Run(context.Background(), nil, dispatch.NewRequest("not_found", dispatch.Input(dispatch.Int(22))))
if err != nil {
t.Fatal(err)
} else if res.Status() != dispatch.NotFoundStatus {
Expand All @@ -65,7 +65,7 @@ func TestDispatchEndpoint(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = nonSigningClient.Run(context.Background(), req)
_, err = nonSigningClient.Run(context.Background(), nil, req)
if err == nil || connect.CodeOf(err) != connect.CodePermissionDenied {
t.Fatalf("expected a permission denied error, got %v", err)
}
Expand Down
20 changes: 17 additions & 3 deletions dispatchserver/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
type EndpointClient struct {
httpClient connect.HTTPClient
signingKey ed25519.PrivateKey
opts []connect.ClientOption

client sdkv1connect.FunctionServiceClient
}
Expand All @@ -48,7 +49,8 @@ func NewEndpointClient(endpointUrl string, opts ...EndpointClientOption) (*Endpo
if err != nil {
return nil, err
}
c.client = sdkv1connect.NewFunctionServiceClient(c.httpClient, endpointUrl, connect.WithInterceptors(validator))
c.opts = append(c.opts, connect.WithInterceptors(validator))
c.client = sdkv1connect.NewFunctionServiceClient(c.httpClient, endpointUrl, c.opts...)

return c, nil
}
Expand All @@ -71,9 +73,21 @@ func HTTPClient(client connect.HTTPClient) EndpointClientOption {
return func(c *EndpointClient) { c.httpClient = client }
}

// ClientOptions sets options on the underlying connect (gRPC) client.
func ClientOptions(opts ...connect.ClientOption) EndpointClientOption {
return func(c *EndpointClient) { c.opts = append(c.opts, opts...) }
}

// Run sends a RunRequest and returns a RunResponse.
func (c *EndpointClient) Run(ctx context.Context, req dispatch.Request) (dispatch.Response, error) {
res, err := c.client.Run(ctx, connect.NewRequest(requestProto(req)))
func (c *EndpointClient) Run(ctx context.Context, header http.Header, req dispatch.Request) (dispatch.Response, error) {
connectReq := connect.NewRequest(requestProto(req))

connectReqHeader := connectReq.Header()
for name, values := range header {
connectReqHeader[name] = values
}

res, err := c.client.Run(ctx, connectReq)
if err != nil {
return dispatch.Response{}, err
}
Expand Down

0 comments on commit dd43359

Please sign in to comment.