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

Added Callback cli #446

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
41 changes: 41 additions & 0 deletions cmd/callbacks/callbacks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package callbacks

import (
"github.com/resonatehq/resonate/pkg/client"
"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
var (
c = client.New()
server string
username string
password string
)

cmd := &cobra.Command{
Use: "callbacks",
Aliases: []string{"callback"},
Short: "Resonate callbacks",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if username != "" || password != "" {
c.SetBasicAuth(username, password)
}

Check warning on line 26 in cmd/callbacks/callbacks.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/callbacks.go#L8-L26

Added lines #L8 - L26 were not covered by tests

return c.Setup(server)

Check warning on line 28 in cmd/callbacks/callbacks.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/callbacks.go#L28

Added line #L28 was not covered by tests
},
}

// Add subcommands
cmd.AddCommand(CreateCallbackCmd(c))

// Flags
cmd.PersistentFlags().StringVarP(&server, "server", "", "http://localhost:8001", "resonate url")
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "basic auth username")
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "basic auth password")

return cmd

Check warning on line 40 in cmd/callbacks/callbacks.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/callbacks.go#L33-L40

Added lines #L33 - L40 were not covered by tests
}
104 changes: 104 additions & 0 deletions cmd/callbacks/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package callbacks

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

"github.com/resonatehq/resonate/pkg/client"
v1 "github.com/resonatehq/resonate/pkg/client/v1"
"github.com/spf13/cobra"
)

var createCallbacksExample = `
# Create a callback
resonate callback create --promise-id foo --root-promise-id bar --timeout 1h --recv default

# Create a callback with url
resonate callback create --promise-id foo --root-promise-id bar --timeout 1h --recv poll://default/6fa89b7e-4a56-40e8-ba4e-78864caa3278

# Create a callback with object
resonate callback create --promise-id foo --root-promise-id bar --timeout 1h --recv {"type": "poll", "data": {"group": "default", "id": "6fa89b7e-4a56-40e8-ba4e-78864caa3278"}}
`

func CreateCallbackCmd(c client.Client) *cobra.Command {
var (
promiseId string
rootPromiseId string
timeout time.Duration
recv string
)
cmd := &cobra.Command{
Use: "create ",
Short: "Create callbacks",
Example: createCallbacksExample,
RunE: func(cmd *cobra.Command, args []string) error {

// What should requestID be?
paramsByte, err := json.Marshal(struct {
Copy link
Member

Choose a reason for hiding this comment

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

We can remove requestId from the CLI, its an optional parameter that is more handy for web applications, you can pass nil to CreateCallbackWithResponse for the params.

RequestId string `json:"request-id"`
}{RequestId: ""})

if err != nil {
return nil
}

Check warning on line 45 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L25-L45

Added lines #L25 - L45 were not covered by tests

paramsStr := string(paramsByte)
params := &v1.CreateCallbackParams{
RequestId: &paramsStr,
}

decoder := json.NewDecoder(strings.NewReader(recv))
var recvJson v1.Recv

if err := decoder.Decode(&recvJson); err == nil {
body := v1.CreateCallbackJSONRequestBody{
PromiseId: promiseId,
RootPromiseId: rootPromiseId,
Timeout: time.Now().Add(timeout).UnixMilli(),
Recv: recvJson,
}

res, err := c.V1().CreateCallbackWithResponse(context.TODO(), params, body)

if err != nil {
return err
}

Check warning on line 67 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L47-L67

Added lines #L47 - L67 were not covered by tests

if res.StatusCode() == 201 {
cmd.Printf("Created callback for promise: %s\n", promiseId)
} else if res.StatusCode() == 200 {
cmd.Printf("Callback exists for the promise: %s\n", promiseId)
} else {
cmd.PrintErrln(res.Status(), string(res.Body))
}
} else {
// Either input is string/url or bad json input
// Need to check for the above before making request
// This does not work
res, err := c.V1().CreateCallbackWithBodyWithResponse(context.TODO(), params, "application/text", strings.NewReader(recv))

if err != nil {
return err
}

Check warning on line 84 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L69-L84

Added lines #L69 - L84 were not covered by tests

if res.StatusCode() == 201 {
cmd.Printf("Created callback for promise: %s\n", promiseId)
} else if res.StatusCode() == 200 {
cmd.Printf("Callback exists for the promise: %s\n", promiseId)
} else {
cmd.PrintErrln(res.Status(), string(res.Body))
}

Check warning on line 92 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L86-L92

Added lines #L86 - L92 were not covered by tests
}
return nil

Check warning on line 94 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L94

Added line #L94 was not covered by tests
},
}

cmd.Flags().StringVar(&promiseId, "promise-id", "", "promise id")
Copy link
Member

Choose a reason for hiding this comment

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

We can mark this one as required

cmd.Flags().StringVar(&rootPromiseId, "root-promise-id", "", "root promise id")
Copy link
Member

Choose a reason for hiding this comment

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

We can also mark this one as required

cmd.Flags().DurationVar(&timeout, "timeout", 0, "callback timeout")
Copy link
Member

Choose a reason for hiding this comment

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

I think we can put a sensible default here, maybe 1m?

cmd.Flags().StringVar(&recv, "recv", "", "receive object")
Copy link
Member

Choose a reason for hiding this comment

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

We can put default (literally) as the default for recv


return cmd

Check warning on line 103 in cmd/callbacks/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/callbacks/create.go#L98-L103

Added lines #L98 - L103 were not covered by tests
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/resonatehq/resonate/cmd/callbacks"
"github.com/resonatehq/resonate/cmd/dst"
"github.com/resonatehq/resonate/cmd/promises"
"github.com/resonatehq/resonate/cmd/quickstart"
Expand Down Expand Up @@ -39,6 +40,7 @@ func init() {
rootCmd.AddCommand(serve.ServeCmd())
rootCmd.AddCommand(quickstart.NewCmd())
rootCmd.AddCommand(tasks.NewCmd())
rootCmd.AddCommand(callbacks.NewCmd())

// Set default output
rootCmd.SetOut(os.Stdout)
Expand Down