diff --git a/cmd/invoke.go b/cmd/invoke.go index c6cfbe39..bd04b88d 100644 --- a/cmd/invoke.go +++ b/cmd/invoke.go @@ -20,9 +20,9 @@ var invokePayload string var InvokeCmd = &cobra.Command{ Use: "invoke", - Short: "Invokes a Dapr app with an optional payload", + Short: "Invokes a Dapr app with an optional payload (deprecated, use invokePost)", Run: func(cmd *cobra.Command, args []string) { - response, err := invoke.InvokeApp(invokeAppID, invokeAppMethod, invokePayload) + response, err := invoke.Post(invokeAppID, invokeAppMethod, invokePayload) if err != nil { print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error invoking app %s: %s", invokeAppID, err)) return diff --git a/cmd/invokeGet.go b/cmd/invokeGet.go new file mode 100644 index 00000000..140d21ef --- /dev/null +++ b/cmd/invokeGet.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/dapr/cli/pkg/invoke" + "github.com/dapr/cli/pkg/print" + "github.com/spf13/cobra" +) + +// invokeGetCmd represents the invokeGet command +var invokeGetCmd = &cobra.Command{ + Use: "invokeGet", + Short: "Issue HTTP GET to Dapr app", + Run: func(cmd *cobra.Command, args []string) { + response, err := invoke.Get(invokeAppID, invokeAppMethod) + if err != nil { + print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error invoking app %s: %s", invokeAppID, err)) + + return + } + + if response != "" { + fmt.Println(response) + } + + print.SuccessStatusEvent(os.Stdout, fmt.Sprintf("HTTP Get to method %s invoked successfully", invokeAppMethod)) + }, +} + +func init() { + invokeGetCmd.Flags().StringVarP(&invokeAppID, "app-id", "a", "", "the app id to invoke") + invokeGetCmd.Flags().StringVarP(&invokeAppMethod, "method", "m", "", "the method to invoke") + + invokeGetCmd.MarkFlagRequired("app-id") + invokeGetCmd.MarkFlagRequired("method") + + RootCmd.AddCommand(invokeGetCmd) +} diff --git a/cmd/invokePost.go b/cmd/invokePost.go new file mode 100644 index 00000000..a6d34e13 --- /dev/null +++ b/cmd/invokePost.go @@ -0,0 +1,49 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. +// ------------------------------------------------------------ + +package cmd + +import ( + "fmt" + "os" + + "github.com/dapr/cli/pkg/invoke" + "github.com/dapr/cli/pkg/print" + "github.com/spf13/cobra" +) + +var ( + invokePostCmd = &cobra.Command{ + Use: "invokePost", + Short: "Issue HTTP POST to Dapr app with an optional payload", + Run: func(cmd *cobra.Command, args []string) { + + response, err := invoke.Post(invokeAppID, invokeAppMethod, invokePayload) + if err != nil { + print.FailureStatusEvent(os.Stdout, fmt.Sprintf("Error invoking app %s: %s", invokeAppID, err)) + + return + } + + if response != "" { + fmt.Println(response) + } + + print.SuccessStatusEvent(os.Stdout, fmt.Sprintf("HTTP Post to method %s invoked successfully", invokeAppMethod)) + + }, + } +) + +func init() { + invokePostCmd.Flags().StringVarP(&invokeAppID, "app-id", "a", "", "the app id to invoke") + invokePostCmd.Flags().StringVarP(&invokeAppMethod, "method", "m", "", "the method to invoke") + invokePostCmd.Flags().StringVarP(&invokePayload, "payload", "p", "", "(optional) a json payload") + + invokePostCmd.MarkFlagRequired("app-id") + invokePostCmd.MarkFlagRequired("method") + + RootCmd.AddCommand(invokePostCmd) +} diff --git a/pkg/invoke/invoke.go b/pkg/invoke/invoke.go index 91f19869..69ca6b2e 100644 --- a/pkg/invoke/invoke.go +++ b/pkg/invoke/invoke.go @@ -12,12 +12,33 @@ import ( "net/http" "github.com/dapr/cli/pkg/api" - "github.com/dapr/cli/pkg/standalone" ) -// InvokeApp invokes the application. -func InvokeApp(appID, method, payload string) (string, error) { +// Get invokes the application via HTTP GET. +func Get(appID, method string) (string, error) { + list, err := standalone.List() + if err != nil { + return "", err + } + for _, lo := range list { + if lo.AppID == appID { + url := makeEndpoint(lo, method) + r, err := http.Get(url) + if err != nil { + return "", err + } + + defer r.Body.Close() + return handleResponse(r) + } + } + + return "", fmt.Errorf("App ID %s not found", appID) +} + +// Post invokes the application via HTTP POST. +func Post(appID, method, payload string) (string, error) { list, err := standalone.List() if err != nil { return "", err @@ -25,23 +46,33 @@ func InvokeApp(appID, method, payload string) (string, error) { for _, lo := range list { if lo.AppID == appID { - r, err := http.Post(fmt.Sprintf("http://localhost:%s/v%s/invoke/%s/method/%s", fmt.Sprintf("%v", lo.HTTPPort), api.RuntimeAPIVersion, lo.AppID, method), "application/json", bytes.NewBuffer([]byte(payload))) + url := makeEndpoint(lo, method) + r, err := http.Post(url, "application/json", bytes.NewBuffer([]byte(payload))) if err != nil { return "", err } - rb, err := ioutil.ReadAll(r.Body) - if err != nil { - return "", err - } - - if len(rb) > 0 { - return string(rb), nil - } - - return "", nil + defer r.Body.Close() + return handleResponse(r) } } - return "", fmt.Errorf("App ID %s not found.", appID) + return "", fmt.Errorf("App ID %s not found", appID) +} + +func makeEndpoint(lo standalone.ListOutput, method string) string { + return fmt.Sprintf("http://localhost:%s/v%s/invoke/%s/method/%s", fmt.Sprintf("%v", lo.HTTPPort), api.RuntimeAPIVersion, lo.AppID, method) +} + +func handleResponse(response *http.Response) (string, error) { + rb, err := ioutil.ReadAll(response.Body) + if err != nil { + return "", err + } + + if len(rb) > 0 { + return string(rb), nil + } + + return "", nil }