Skip to content

Commit

Permalink
add include option to print response headers in invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Aug 16, 2021
1 parent 0dcc590 commit f8dff31
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
7 changes: 6 additions & 1 deletion cmd/mantil/cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ var invokeCmd = &cobra.Command{
log.Fatalf("api URL for the project does not exist")
}
data := cmd.Flag("data").Value.String()
includeHeaders, err := cmd.Flags().GetBool("include")
if err != nil {
includeHeaders = false
}
endpoint := fmt.Sprintf("%s/%s", p.ApiURL, args[0])

if err := invoke.Endpoint(endpoint, data); err != nil {
if err := invoke.Endpoint(endpoint, data, includeHeaders); err != nil {
log.Fatal(err)
}
},
}

func init() {
invokeCmd.Flags().StringP("data", "d", "", "Data for the request")
invokeCmd.Flags().BoolP("include", "i", false, "Include response headers in the output.")
rootCmd.AddCommand(invokeCmd)
}
4 changes: 2 additions & 2 deletions internal/commands/invoke/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/atoz-technology/mantil-cli/internal/commands"
)

func Endpoint(endpoint string, data string) error {
if err := commands.PrintProjectRequest(endpoint, data); err != nil {
func Endpoint(endpoint string, data string, includeHeaders bool) error {
if err := commands.PrintProjectRequest(endpoint, data, includeHeaders); err != nil {
return err
}
return nil
Expand Down
38 changes: 27 additions & 11 deletions internal/commands/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func BackendRequest(method string, req interface{}, rsp interface{}) error {
return nil
}

func PrintProjectRequest(url string, req string) error {
func PrintProjectRequest(url string, req string, includeHeaders bool) error {
buf := []byte(req)
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(buf))
if err != nil {
Expand All @@ -76,17 +76,19 @@ func PrintProjectRequest(url string, req string) error {
defer httpRsp.Body.Close()

fmt.Println(httpRsp.Status)
if isSuccessfulResponse(httpRsp) {
buf, err = ioutil.ReadAll(httpRsp.Body)
if err != nil {
return err
}
if includeHeaders {
printRspHeaders(httpRsp)
fmt.Println()
} else if !isSuccessfulResponse(httpRsp) {
printApiErrorHeader(httpRsp)
}

buf, err = ioutil.ReadAll(httpRsp.Body)
if err != nil {
return err
}
if string(buf) != "" {
fmt.Printf("%s\n", string(buf))
} else {
apiErr := httpRsp.Header.Get("X-Api-Error")
if apiErr != "" {
fmt.Printf("X-Api-Error: %s\n", apiErr)
}
}
return nil
}
Expand All @@ -95,6 +97,20 @@ func isSuccessfulResponse(rsp *http.Response) bool {
return strings.HasPrefix(rsp.Status, "2")
}

func printRspHeaders(rsp *http.Response) {
for k, v := range rsp.Header {
fmt.Printf("%s: %s\n", k, strings.Join(v, ","))
}
}

func printApiErrorHeader(rsp *http.Response) {
header := "X-Api-Error"
apiErr := rsp.Header.Get(header)
if apiErr != "" {
fmt.Printf("%s: %s\n", header, apiErr)
}
}

type Credentials struct {
AccessKeyID string
SecretAccessKey string
Expand Down

0 comments on commit f8dff31

Please sign in to comment.