Skip to content

Commit

Permalink
Add mint api base path (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonywok authored Dec 5, 2023
1 parent 3a76866 commit 5d8e138
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
16 changes: 13 additions & 3 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/pkg/errors"

Expand All @@ -15,6 +16,7 @@ import (
// Client is an API Client for Mint
type Client struct {
RoundTrip func(*http.Request) (*http.Response, error)
Host string
}

func New(cfg Config) (Client, error) {
Expand All @@ -31,12 +33,12 @@ func New(cfg Config) (Client, error) {
return http.DefaultClient.Do(req)
}

return Client{roundTrip}, nil
return Client{roundTrip, cfg.Host}, nil
}

// InitiateRun sends a request to Mint for starting a new runn
// InitiateRun sends a request to Mint for starting a new run
func (c Client) InitiateRun(cfg InitiateRunConfig) (*InitiateRunResult, error) {
endpoint := "/api/runs"
endpoint := c.mintEndpoint("/api/runs")

if err := cfg.Validate(); err != nil {
return nil, errors.Wrap(err, "validation failed")
Expand Down Expand Up @@ -104,3 +106,11 @@ func extractErrorMessage(reader io.Reader) string {

return errorStruct.Result.Data.Error
}

// TODO(TS): Remove this once we're fully transitioned
func (c Client) mintEndpoint(path string) string {
if !strings.Contains(c.Host, "cloud") {
return path
}
return "/mint" + path
}
13 changes: 13 additions & 0 deletions internal/client/client_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Client Suite")
}
98 changes: 98 additions & 0 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package client_test

import (
"bytes"
"encoding/json"
"io"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"net/http"

"github.com/rwx-research/mint-cli/internal/client"
)

var _ = Describe("Client", func() {
Describe("InitiateRun", func() {
Context("without a mint base path", func() {
It("uses an mint.rwx.com style endpoint", func() {
body := struct {
RunID string `json:"runId"`
RunURL string `json:"runUrl"`
TargetedTaskKeys []string `json:"targetedTaskKeys"`
DefinitionPath string `json:"definitionPath"`
}{
RunID: "123",
RunURL: "https://mint.rwx.com/runs/123",
TargetedTaskKeys: []string{},
DefinitionPath: "foo",
}
bodyBytes, _ := json.Marshal(body)

roundTrip := func(req *http.Request) (*http.Response, error) {
Expect(req.URL.Path).To(Equal("/api/runs"))
return &http.Response{
Status: "201 Created",
StatusCode: 201,
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
}, nil
}

c := client.Client{roundTrip, "mint.rwx.com"}

initRunConfig := client.InitiateRunConfig{
InitializationParameters: map[string]string{},
TaskDefinitions: []client.TaskDefinition{
{Path: "foo", FileContents: "echo 'bar'"},
},
TargetedTaskKeys: []string{},
UseCache: false,
}

_, err := c.InitiateRun(initRunConfig)
Expect(err).To(BeNil())
})
})

Context("with a mint base path", func() {
It("prefixes the endpoint with the base path", func() {
body := struct {
RunID string `json:"runId"`
RunURL string `json:"runUrl"`
TargetedTaskKeys []string `json:"targetedTaskKeys"`
DefinitionPath string `json:"definitionPath"`
}{
RunID: "123",
RunURL: "https://mint.rwx.com/runs/123",
TargetedTaskKeys: []string{},
DefinitionPath: "foo",
}
bodyBytes, _ := json.Marshal(body)

roundTrip := func(req *http.Request) (*http.Response, error) {
Expect(req.URL.Path).To(Equal("/mint/api/runs"))
return &http.Response{
Status: "201 Created",
StatusCode: 201,
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
}, nil
}

c := client.Client{roundTrip, "cloud.rwx.com"}

initRunConfig := client.InitiateRunConfig{
InitializationParameters: map[string]string{},
TaskDefinitions: []client.TaskDefinition{
{Path: "foo", FileContents: "echo 'bar'"},
},
TargetedTaskKeys: []string{},
UseCache: false,
}

_, err := c.InitiateRun(initRunConfig)
Expect(err).To(BeNil())
})
})
})
})

0 comments on commit 5d8e138

Please sign in to comment.