Display call ID for sync/async calls in CLI

This commit is contained in:
Denis Makogon
2017-08-03 12:35:34 +03:00
parent e47ab56ff0
commit 6fcc16fe3b

View File

@@ -1,6 +1,7 @@
package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
@@ -21,6 +22,10 @@ func EnvAsHeader(req *http.Request, selectedEnv []string) {
}
}
type callID struct {
CallID string `json:"call_id"`
}
func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error {
if method == "" {
if content == nil {
@@ -45,8 +50,14 @@ func CallFN(u string, content io.Reader, output io.Writer, method string, env []
if err != nil {
return fmt.Errorf("error running route: %s", err)
}
io.Copy(output, resp.Body)
if call_id, found := resp.Header["Fn_call_id"]; found {
fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", call_id[0]))
io.Copy(output, resp.Body)
} else {
c := &callID{}
json.NewDecoder(resp.Body).Decode(c)
fmt.Fprint(output, fmt.Sprintf("Call ID: %v\n", c.CallID))
}
if resp.StatusCode >= 400 {
// TODO: parse out error message