Skip to content

Commit

Permalink
Set headers with an option
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 10, 2024
1 parent 126da7d commit dc653d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 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(), nil, req)
res, err := client.Run(context.Background(), 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(), nil, dispatch.NewRequest("not_found", dispatch.Input(dispatch.Int(22))))
res, err = client.Run(context.Background(), 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(), nil, req)
_, err = nonSigningClient.Run(context.Background(), req)
if err == nil || connect.CodeOf(err) != connect.CodePermissionDenied {
t.Fatalf("expected a permission denied error, got %v", err)
}
Expand Down
16 changes: 11 additions & 5 deletions dispatchserver/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type EndpointClient struct {
httpClient connect.HTTPClient
signingKey ed25519.PrivateKey
header http.Header
opts []connect.ClientOption

client sdkv1connect.FunctionServiceClient
Expand Down Expand Up @@ -74,18 +75,23 @@ func HTTPClient(client connect.HTTPClient) EndpointClientOption {
return func(c *EndpointClient) { c.httpClient = client }
}

// ClientOptions sets options on the underlying connect (gRPC) client.
// RequestHeaders sets headers on the request to the endpoint.
func RequestHeaders(header http.Header) EndpointClientOption {
return func(c *EndpointClient) { c.header = header }
}

// ClientOptions adds options for 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, header http.Header, req dispatch.Request) (dispatch.Response, error) {
func (c *EndpointClient) Run(ctx context.Context, req dispatch.Request) (dispatch.Response, error) {
connectReq := connect.NewRequest(requestProto(req))

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

res, err := c.client.Run(ctx, connectReq)
Expand Down

0 comments on commit dc653d6

Please sign in to comment.