Add InvokeGet and Post, refactor Invoke. (#229)

This commit is contained in:
Vishal Saroopchand 2020-01-12 05:17:49 +00:00 committed by Yaron Schneider
parent 2a44631c0a
commit b327fdf118
4 changed files with 137 additions and 17 deletions

View File

@ -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

40
cmd/invokeGet.go Normal file
View File

@ -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)
}

49
cmd/invokePost.go Normal file
View File

@ -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)
}

View File

@ -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
}