Skip to content

Commit

Permalink
Revise rest schema (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Oct 20, 2024
1 parent aa7e580 commit 51e3153
Show file tree
Hide file tree
Showing 121 changed files with 20,278 additions and 6,148 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ linters:
- noctx
- unparam
- forbidigo
- tagalign

issues:
exclude-files:
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
FROM golang:1.23 AS builder

WORKDIR /app
COPY go.mod go.sum ./
COPY ndc-rest-schema ./ndc-rest-schema
COPY go.mod go.sum go.work ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -v -o ndc-cli ./server
Expand Down
File renamed without changes.
24 changes: 13 additions & 11 deletions rest/connector.go → connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"fmt"
"os"

"github.com/hasura/ndc-rest/rest/internal"
"github.com/hasura/ndc-rest/connector/internal"
"github.com/hasura/ndc-rest/ndc-rest-schema/configuration"
rest "github.com/hasura/ndc-rest/ndc-rest-schema/schema"
"github.com/hasura/ndc-sdk-go/connector"
"github.com/hasura/ndc-sdk-go/schema"
"gopkg.in/yaml.v3"
)

// RESTConnector implements the SDK interface of NDC specification
type RESTConnector struct {
metadata RESTMetadataCollection
metadata internal.MetadataCollection
capabilities *schema.RawCapabilitiesResponse
rawSchema *schema.RawSchemaResponse
schema *schema.SchemaResponse
schema *rest.NDCRestSchema
client *internal.HTTPClient
}

Expand All @@ -34,7 +36,7 @@ func NewRESTConnector(opts ...Option) *RESTConnector {

// ParseConfiguration validates the configuration files provided by the user, returning a validated 'Configuration',
// or throwing an error to prevents Connector startup.
func (c *RESTConnector) ParseConfiguration(ctx context.Context, configurationDir string) (*Configuration, error) {
func (c *RESTConnector) ParseConfiguration(ctx context.Context, configurationDir string) (*configuration.Configuration, error) {
restCapabilities := schema.CapabilitiesResponse{
Version: "0.1.6",
Capabilities: schema.Capabilities{
Expand Down Expand Up @@ -78,7 +80,7 @@ func (c *RESTConnector) ParseConfiguration(ctx context.Context, configurationDir
//
// In addition, this function should register any
// connector-specific metrics with the metrics registry.
func (c *RESTConnector) TryInitState(ctx context.Context, configuration *Configuration, metrics *connector.TelemetryState) (*State, error) {
func (c *RESTConnector) TryInitState(ctx context.Context, configuration *configuration.Configuration, metrics *connector.TelemetryState) (*State, error) {
c.client.SetTracer(metrics.Tracer)
return &State{}, nil
}
Expand All @@ -89,27 +91,27 @@ func (c *RESTConnector) TryInitState(ctx context.Context, configuration *Configu
// is able to reach its data source over the network.
//
// Should throw if the check fails, else resolve.
func (c *RESTConnector) HealthCheck(ctx context.Context, configuration *Configuration, state *State) error {
func (c *RESTConnector) HealthCheck(ctx context.Context, configuration *configuration.Configuration, state *State) error {
return nil
}

// GetCapabilities get the connector's capabilities.
func (c *RESTConnector) GetCapabilities(configuration *Configuration) schema.CapabilitiesResponseMarshaler {
func (c *RESTConnector) GetCapabilities(configuration *configuration.Configuration) schema.CapabilitiesResponseMarshaler {
return c.capabilities
}

// QueryExplain explains a query by creating an execution plan.
func (c *RESTConnector) QueryExplain(ctx context.Context, configuration *Configuration, state *State, request *schema.QueryRequest) (*schema.ExplainResponse, error) {
func (c *RESTConnector) QueryExplain(ctx context.Context, configuration *configuration.Configuration, state *State, request *schema.QueryRequest) (*schema.ExplainResponse, error) {
return nil, schema.NotSupportedError("query explain has not been supported yet", nil)
}

// MutationExplain explains a mutation by creating an execution plan.
func (c *RESTConnector) MutationExplain(ctx context.Context, configuration *Configuration, state *State, request *schema.MutationRequest) (*schema.ExplainResponse, error) {
func (c *RESTConnector) MutationExplain(ctx context.Context, configuration *configuration.Configuration, state *State, request *schema.MutationRequest) (*schema.ExplainResponse, error) {
return nil, schema.NotSupportedError("mutation explain has not been supported yet", nil)
}

func parseConfiguration(configurationDir string) (*Configuration, error) {
var config Configuration
func parseConfiguration(configurationDir string) (*configuration.Configuration, error) {
var config configuration.Configuration
jsonBytes, err := os.ReadFile(configurationDir + "/config.json")
if err == nil {
if err = json.Unmarshal(jsonBytes, &config); err != nil {
Expand Down
23 changes: 14 additions & 9 deletions rest/connector_test.go → connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

"github.com/hasura/ndc-rest/ndc-rest-schema/configuration"
"github.com/hasura/ndc-sdk-go/connector"
"github.com/hasura/ndc-sdk-go/schema"
"gotest.tools/v3/assert"
Expand Down Expand Up @@ -274,35 +275,35 @@ func TestRESTConnector_distribution(t *testing.T) {
}, connector.WithoutRecovery())
assert.NilError(t, err)

timeout, err := rc.metadata[0].settings.Servers[0].Timeout.Value()
timeout, err := rc.metadata[0].Settings.Servers[0].Timeout.Value()
assert.NilError(t, err)
assert.Equal(t, int64(30), *timeout)

retryTimes, err := rc.metadata[0].settings.Servers[0].Retry.Times.Value()
retryTimes, err := rc.metadata[0].Settings.Servers[0].Retry.Times.Value()
assert.NilError(t, err)
assert.Equal(t, int64(2), *retryTimes)

retryDelay, err := rc.metadata[0].settings.Servers[0].Retry.Delay.Value()
retryDelay, err := rc.metadata[0].Settings.Servers[0].Retry.Delay.Value()
assert.NilError(t, err)
assert.Equal(t, int64(1000), *retryDelay)

retryStatus, err := rc.metadata[0].settings.Servers[0].Retry.HTTPStatus.Value()
retryStatus, err := rc.metadata[0].Settings.Servers[0].Retry.HTTPStatus.Value()
assert.NilError(t, err)
assert.DeepEqual(t, []int64{429, 500}, retryStatus)

timeout1, err := rc.metadata[0].settings.Servers[1].Timeout.Value()
timeout1, err := rc.metadata[0].Settings.Servers[1].Timeout.Value()
assert.NilError(t, err)
assert.Equal(t, int64(10), *timeout1)

retryTimes1, err := rc.metadata[0].settings.Servers[1].Retry.Times.Value()
retryTimes1, err := rc.metadata[0].Settings.Servers[1].Retry.Times.Value()
assert.NilError(t, err)
assert.Equal(t, int64(1), *retryTimes1)

retryDelay1, err := rc.metadata[0].settings.Servers[1].Retry.Delay.Value()
retryDelay1, err := rc.metadata[0].Settings.Servers[1].Retry.Delay.Value()
assert.NilError(t, err)
assert.Equal(t, int64(500), *retryDelay1)

retryStatus1, err := rc.metadata[0].settings.Servers[1].Retry.HTTPStatus.Value()
retryStatus1, err := rc.metadata[0].Settings.Servers[1].Retry.HTTPStatus.Value()
assert.NilError(t, err)
assert.DeepEqual(t, []int64{429, 500, 501, 502}, retryStatus1)

Expand Down Expand Up @@ -543,6 +544,7 @@ func TestRESTConnector_multiSchemas(t *testing.T) {
}

func createMockServer(t *testing.T, apiKey string, bearerToken string) *httptest.Server {
t.Helper()
mux := http.NewServeMux()

writeResponse := func(w http.ResponseWriter, body string) {
Expand Down Expand Up @@ -709,6 +711,7 @@ func (mds *mockMultiSchemaServer) createServer() *httptest.Server {
}

func assertNdcOperations(t *testing.T, dir string, targetURL string) {
t.Helper()
queryFiles, err := os.ReadDir(dir)
if err != nil {
if !os.IsNotExist(err) {
Expand Down Expand Up @@ -736,7 +739,8 @@ func assertNdcOperations(t *testing.T, dir string, targetURL string) {
}
}

func test_createServer(t *testing.T, dir string) *connector.Server[Configuration, State] {
func test_createServer(t *testing.T, dir string) *connector.Server[configuration.Configuration, State] {
t.Helper()
c := NewRESTConnector()
server, err := connector.NewServer(c, &connector.ServerOptions{
Configuration: dir,
Expand All @@ -750,6 +754,7 @@ func test_createServer(t *testing.T, dir string) *connector.Server[Configuration
}

func assertHTTPResponse[B any](t *testing.T, res *http.Response, statusCode int, expectedBody B) {
t.Helper()
defer res.Body.Close()

bodyBytes, err := io.ReadAll(res.Body)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions connector/internal/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package internal

import (
rest "github.com/hasura/ndc-rest/ndc-rest-schema/schema"
"github.com/hasura/ndc-sdk-go/schema"
)

// MetadataCollection stores list of REST metadata with helper methods
type MetadataCollection []rest.NDCRestSchema

// GetFunction gets the NDC function by name
func (rms MetadataCollection) GetFunction(name string) (*rest.OperationInfo, *rest.NDCRestSettings, error) {
for _, rm := range rms {
fn := rm.GetFunction(name)
if fn != nil {
return fn, rm.Settings, nil
}
}
return nil, nil, schema.UnprocessableContentError("unsupported query: "+name, nil)
}

// GetProcedure gets the NDC procedure by name
func (rms MetadataCollection) GetProcedure(name string) (*rest.OperationInfo, *rest.NDCRestSettings, error) {
for _, rm := range rms {
fn := rm.GetProcedure(name)
if fn != nil {
return fn, rm.Settings, nil
}
}
return nil, nil, schema.UnprocessableContentError("unsupported query: "+name, nil)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 51e3153

Please sign in to comment.