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

feat(integration-tests): adds go client for interacting with web GQL #14124

Closed
wants to merge 1 commit into from
Closed
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
119 changes: 119 additions & 0 deletions integration-tests/web/sdk/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package client

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

"github.com/Khan/genqlient/graphql"

"github.com/smartcontractkit/chainlink/integration-tests/web/sdk/client/internal/doer"
"github.com/smartcontractkit/chainlink/integration-tests/web/sdk/internal/generated"
)

type client struct {
gqlClient graphql.Client
credentials Credentials
endpoints endpoints
cookie string
}

type endpoints struct {
Sessions string
Query string
}

type Credentials struct {
Email string `json:"email"`
Password string `json:"password"`
}

func New(baseURI string, creds Credentials) (*client, error) { //nolint:revive
endpoints := endpoints{
Sessions: baseURI + "/sessions",
Query: baseURI + "/query",
}
c := &client{
endpoints: endpoints,
credentials: creds,
}

if err := c.login(); err != nil {
return nil, fmt.Errorf("failed to login to node: %w", err)
}

c.gqlClient = graphql.NewClient(
c.endpoints.Query,
doer.NewAuthed(c.cookie),
)

return c, nil
}

func (c *client) GetCSAKeys(ctx context.Context) (*generated.GetCSAKeysResponse, error) {
return generated.GetCSAKeys(ctx, c.gqlClient)
}

func (c *client) GetJob(ctx context.Context, id string) (*generated.GetJobResponse, error) {
return generated.GetJob(ctx, c.gqlClient, id)
}

func (c *client) ListJobs(ctx context.Context, offset, limit int) (*generated.ListJobsResponse, error) {
return generated.ListJobs(ctx, c.gqlClient, offset, limit)
}

func (c *client) GetJobProposal(ctx context.Context, id string) (*generated.GetJobProposalResponse, error) {
return generated.GetJobProposal(ctx, c.gqlClient, id)
}

func (c *client) GetBridge(ctx context.Context, id string) (*generated.GetBridgeResponse, error) {
return generated.GetBridge(ctx, c.gqlClient, id)
}

func (c *client) ListBridges(ctx context.Context, offset, limit int) (*generated.ListBridgesResponse, error) {
return generated.ListBridges(ctx, c.gqlClient, offset, limit)
}

func (c *client) GetFeedsManager(ctx context.Context, id string) (*generated.GetFeedsManagerResponse, error) {
return generated.GetFeedsManager(ctx, c.gqlClient, id)
}

func (c *client) ListFeedsManagers(ctx context.Context) (*generated.ListFeedsManagersResponse, error) {
return generated.ListFeedsManagers(ctx, c.gqlClient)
}

func (c *client) CreateFeedsManager(ctx context.Context, cmd generated.CreateFeedsManagerInput) (*generated.CreateFeedsManagerResponse, error) {
return generated.CreateFeedsManager(ctx, c.gqlClient, cmd)
}

func (c *client) login() error {
b, err := json.Marshal(c.credentials)
if err != nil {
return fmt.Errorf("failed to marshal credentials: %w", err)
}

payload := strings.NewReader(string(b))

req, err := http.NewRequest("POST", c.endpoints.Sessions, payload)
if err != nil {
return err
}

req.Header.Add("Content-Type", "application/json")

res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()

cookieHeader := res.Header.Get("Set-Cookie")
if cookieHeader == "" {
return fmt.Errorf("no cookie found in header")
}

c.cookie = strings.Split(cookieHeader, ";")[0]
return nil
}
20 changes: 20 additions & 0 deletions integration-tests/web/sdk/client/internal/doer/doer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package doer

import "net/http"

type authed struct {
cookie string
wrapped *http.Client
}

func NewAuthed(cookie string) *authed {
return &authed{
cookie: cookie,
wrapped: http.DefaultClient,
}
}

func (a *authed) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("cookie", a.cookie)
return a.wrapped.Do(req)
}
15 changes: 15 additions & 0 deletions integration-tests/web/sdk/genqlient.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Default genqlient config; for full documentation see:
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml
schema: ./internal/schema.graphql

operations:
- ./internal/genqlient.graphql

generated: ./internal/generated/generated.go

bindings:
Time:
type: string

package_bindings:
- package: github.com/smartcontractkit/chainlink/core/web/gqlscalar
19 changes: 19 additions & 0 deletions integration-tests/web/sdk/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/smartcontractkit/chainlink/integration-tests/web/sdk

go 1.21.7

require (
github.com/Khan/genqlient v0.7.0
github.com/smartcontractkit/chainlink v1.13.3
)

require (
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/alexflint/go-arg v1.4.2 // indirect
github.com/alexflint/go-scalar v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/vektah/gqlparser/v2 v2.5.11 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
43 changes: 43 additions & 0 deletions integration-tests/web/sdk/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
github.com/Khan/genqlient v0.7.0 h1:GZ1meyRnzcDTK48EjqB8t3bcfYvHArCUUvgOwpz1D4w=
github.com/Khan/genqlient v0.7.0/go.mod h1:HNyy3wZvuYwmW3Y7mkoQLZsa/R5n5yIRajS1kPBvSFM=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0=
github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0 h1:knToPYa2xtfg42U3I6punFEjaGFKWQRXJwj0JTv4mTs=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/smartcontractkit/chainlink v1.13.3 h1:jMu/eyEUZvoYG+VhA20AI3TvQwiWisThg8ivYZRj2jk=
github.com/smartcontractkit/chainlink v1.13.3/go.mod h1:crv7u9WKBmwmTNXrof0grHA+tYwvVk7X3JQuaRyet/o=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vektah/gqlparser/v2 v2.5.11 h1:JJxLtXIoN7+3x6MBdtIP59TP1RANnY7pXOaDnADQSf8=
github.com/vektah/gqlparser/v2 v2.5.11/go.mod h1:1rCcfwB2ekJofmluGWXMSEnPMZgbxzwj6FaZ/4OT8Cc=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading
Loading