added --method flag for HTTP request method

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>

changed method to httpMethod, and uppercase to camelCase

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
This commit is contained in:
Vivek Singh
2018-03-07 22:53:27 +05:30
committed by Alex Ellis
parent 7997a467e0
commit c8bdaba114
3 changed files with 37 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ var (
query []string
headers []string
invokeAsync bool
httpMethod string
)
func init() {
@@ -29,12 +30,13 @@ func init() {
invokeCmd.Flags().StringArrayVar(&query, "query", []string{}, "pass query-string options")
invokeCmd.Flags().StringArrayVarP(&headers, "header", "H", []string{}, "pass HTTP request header")
invokeCmd.Flags().BoolVarP(&invokeAsync, "async", "a", false, "Invoke the function asynchronously")
invokeCmd.Flags().StringVarP(&httpMethod, "method", "m", "POST", "pass HTTP request method")
faasCmd.AddCommand(invokeCmd)
}
var invokeCmd = &cobra.Command{
Use: `invoke FUNCTION_NAME [--gateway GATEWAY_URL] [--content-type CONTENT_TYPE] [--query PARAM=VALUE] [--header PARAM=VALUE]`,
Use: `invoke FUNCTION_NAME [--gateway GATEWAY_URL] [--content-type CONTENT_TYPE] [--query PARAM=VALUE] [--header PARAM=VALUE] [--method HTTP_METHOD]`,
Short: "Invoke an OpenFaaS function",
Long: `Invokes an OpenFaaS function and reads from STDIN for the body of the request`,
Example: ` faas-cli invoke echo --gateway https://domain:port
@@ -42,7 +44,8 @@ var invokeCmd = &cobra.Command{
faas-cli invoke env --query repo=faas-cli --query org=openfaas
faas-cli invoke env --header X-Ping-Url=http://request.bin/etc
faas-cli invoke resize-img --async -H "X-Callback-Url=http://gateway:8080/function/send2slack" < image.png
faas-cli invoke env -H X-Ping-Url=http://request.bin/etc`,
faas-cli invoke env -H X-Ping-Url=http://request.bin/etc
faas-cli invoke flask --method GET`,
RunE: runInvoke,
}
@@ -79,7 +82,7 @@ func runInvoke(cmd *cobra.Command, args []string) error {
return fmt.Errorf("unable to read standard input: %s", err.Error())
}
response, err := proxy.InvokeFunction(gatewayAddress, functionName, &functionInput, contentType, query, headers, invokeAsync)
response, err := proxy.InvokeFunction(gatewayAddress, functionName, &functionInput, contentType, query, headers, invokeAsync, httpMethod)
if err != nil {
return err
}

View File

@@ -15,7 +15,7 @@ import (
)
// InvokeFunction a function
func InvokeFunction(gateway string, name string, bytesIn *[]byte, contentType string, query []string, headers []string, async bool) (*[]byte, error) {
func InvokeFunction(gateway string, name string, bytesIn *[]byte, contentType string, query []string, headers []string, async bool, httpMethod string) (*[]byte, error) {
var resBytes []byte
gateway = strings.TrimRight(gateway, "/")
@@ -40,9 +40,14 @@ func InvokeFunction(gateway string, name string, bytesIn *[]byte, contentType st
functionEndpoint = "/async-function/"
}
httpMethodErr := validateHTTPMethod(httpMethod)
if httpMethodErr != nil {
return nil, httpMethodErr
}
gatewayURL := gateway + functionEndpoint + name + qs
req, err := http.NewRequest(http.MethodPost, gatewayURL, reader)
req, err := http.NewRequest(httpMethod, gatewayURL, reader)
if err != nil {
fmt.Println()
fmt.Println(err)
@@ -133,3 +138,23 @@ func parseHeaders(headers []string) (map[string]string, error) {
}
return headerMap, nil
}
// validateMethod validates the HTTP request method
func validateHTTPMethod(httpMethod string) error {
var allowedMethods = []string{http.MethodGet, http.MethodPost}
helpString := strings.Join(allowedMethods, "/")
if !contains(allowedMethods, httpMethod) {
return fmt.Errorf("the --method or -m flag must take one of these values (%s)", helpString)
}
return nil
}
func contains(s []string, item string) bool {
for _, value := range s {
if value == item {
return true
}
}
return false
}

View File

@@ -27,6 +27,7 @@ func Test_InvokeFunction(t *testing.T) {
[]string{},
[]string{},
false,
http.MethodPost,
)
if err != nil {
@@ -47,6 +48,7 @@ func Test_InvokeFunction_Async(t *testing.T) {
[]string{},
[]string{},
true,
http.MethodPost,
)
if err != nil {
@@ -67,6 +69,7 @@ func Test_InvokeFunction_Not2xx(t *testing.T) {
[]string{},
[]string{},
false,
http.MethodPost,
)
if err == nil {
@@ -90,6 +93,7 @@ func Test_InvokeFunction_MissingURLPrefix(t *testing.T) {
[]string{},
[]string{},
false,
http.MethodPost,
)
if err == nil {