Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAPPL-316] implement FetchFunc #15424

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/services/gateway/handlers/capabilities/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (

const (
// NOTE: more methods will go here. HTTP trigger/action/target; etc.
MethodWebAPITarget = "web_api_target"
MethodWebAPITrigger = "web_api_trigger"
MethodComputeAction = "compute_action"
MethodWebAPITarget = "web_api_target"
MethodWebAPITrigger = "web_api_trigger"
MethodComputeAction = "compute_action"
MethodWorkflowSyncer = "workflow_syncer"
)

type handler struct {
Expand Down
43 changes: 43 additions & 0 deletions core/services/workflows/syncer/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package syncer

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi"
"github.com/smartcontractkit/chainlink/v2/core/logger"
ghcapabilities "github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/capabilities"
)

func NewFetcherFunc(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agparadiso Does this need to be plugged in somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this will be passed to the NewWorkflowRegistry method, but this is not used anywhere yet.

ctx context.Context,
lggr logger.Logger,
och *webapi.OutgoingConnectorHandler) FetcherFunc {
return func(ctx context.Context, url string) ([]byte, error) {
payloadBytes, err := json.Marshal(ghcapabilities.Request{
URL: url,
Method: http.MethodGet,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal fetch request: %w", err)
}

messageID := strings.Join([]string{ghcapabilities.MethodWorkflowSyncer, url}, "/")
resp, err := och.HandleSingleNodeRequest(ctx, messageID, payloadBytes)
if err != nil {
return nil, err
}

lggr.Debugw("received gateway response", "resp", resp)
var payload ghcapabilities.Response
err = json.Unmarshal(resp.Body.Payload, &payload)
if err != nil {
return nil, err
}

return payload.Body, nil
}
}
76 changes: 76 additions & 0 deletions core/services/workflows/syncer/fetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package syncer

import (
"context"
"encoding/json"
"strings"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api"
gcmocks "github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector/mocks"
ghcapabilities "github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/capabilities"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/common"
)

func TestNewFetcherFunc(t *testing.T) {
ctx := context.Background()
lggr := logger.TestLogger(t)

config := webapi.ServiceConfig{
RateLimiter: common.RateLimiterConfig{
GlobalRPS: 100.0,
GlobalBurst: 100,
PerSenderRPS: 100.0,
PerSenderBurst: 100,
},
}

connector := gcmocks.NewGatewayConnector(t)
och, err := webapi.NewOutgoingConnectorHandler(connector, config, ghcapabilities.MethodComputeAction, lggr)
require.NoError(t, err)

url := "http://example.com"

msgID := strings.Join([]string{ghcapabilities.MethodWorkflowSyncer, url}, "/")

t.Run("OK-valid_request", func(t *testing.T) {
gatewayResp := gatewayResponse(t, msgID)
connector.EXPECT().SignAndSendToGateway(mock.Anything, "gateway1", mock.Anything).Run(func(ctx context.Context, gatewayID string, msg *api.MessageBody) {
och.HandleGatewayMessage(ctx, "gateway1", gatewayResp)
}).Return(nil).Times(1)
connector.EXPECT().DonID().Return("don-id")
connector.EXPECT().GatewayIDs().Return([]string{"gateway1", "gateway2"})

fetcher := NewFetcherFunc(ctx, lggr, och)

payload, err := fetcher(ctx, url)
require.NoError(t, err)

expectedPayload := []byte("response body")
require.Equal(t, expectedPayload, payload)
})
}

func gatewayResponse(t *testing.T, msgID string) *api.Message {
headers := map[string]string{"Content-Type": "application/json"}
body := []byte("response body")
responsePayload, err := json.Marshal(ghcapabilities.Response{
StatusCode: 200,
Headers: headers,
Body: body,
ExecutionError: false,
})
require.NoError(t, err)
return &api.Message{
Body: api.MessageBody{
MessageId: msgID,
Method: ghcapabilities.MethodWebAPITarget,
Payload: responsePayload,
},
}
}
Loading