-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: main
Are you sure you want to change the base?
Added Callback cli #446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
|
||
return c.Setup(server) | ||
}, | ||
} | ||
|
||
// 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 | ||
} |
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 { | ||
RequestId string `json:"request-id"` | ||
}{RequestId: ""}) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
paramsStr := string(paramsByte) | ||
params := &v1.CreateCallbackParams{ | ||
RequestId: ¶msStr, | ||
} | ||
|
||
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 | ||
} | ||
|
||
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 | ||
} | ||
|
||
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)) | ||
} | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&promiseId, "promise-id", "", "promise id") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can put default (literally) as the default for recv |
||
|
||
return cmd | ||
} |
There was a problem hiding this comment.
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.