Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from athul/put
Browse files Browse the repository at this point in the history
Put, Patch and Delete Methods Added
  • Loading branch information
athul authored Jan 28, 2020
2 parents 2a84927 + e62d345 commit 8a00220
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 4 deletions.
28 changes: 28 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func main() {
app := cli.NewApp()
app.Name = "Postwoman CLI"
app.Version = "0.0.1"
app.Usage = "Test API endpoints without the hassle"
app.Description = "Made with <3 by Postwoman Team"

Expand Down Expand Up @@ -83,6 +84,33 @@ func main() {
return nil
},
},
{
Name: "put",
Usage: "Send a PUT Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Putbasic(c)
return nil
},
},
{
Name: "patch",
Usage: "Send a PATCH Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Patchbasic(c)
return nil
},
},
{
Name: "delete",
Usage: "Send a DELETE Request",
Flags: postFlags,
Action: func(c *cli.Context) error {
mets.Deletebasic(c)
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/TylerBrock/colorjson v0.0.0-20180527164720-95ec53f28296
github.com/fatih/color v1.9.0 // indirect
github.com/fatih/color v1.9.0
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e // indirect
github.com/urfave/cli v1.22.2
)
35 changes: 35 additions & 0 deletions methods/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package methods

import (
"bytes"
"fmt"
"net/http"

"github.com/urfave/cli"
)

//Deletebasic sends a basic DELETE request
func Deletebasic(c *cli.Context) {
url := c.String("url")
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", c.String("ctype"))
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
}
if c.String("u") != "" && c.String("p") != "" {
un := c.String("u")
pw := c.String("p")
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
s := formatresp(resp)
fmt.Println(s)
}
11 changes: 10 additions & 1 deletion methods/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@ import (
"net/http"

"github.com/TylerBrock/colorjson"
"github.com/fatih/color"
)

// Formatresp formats the Response with Indents and Colors
func formatresp(resp *http.Response) string {
c := color.New(color.FgCyan, color.Bold)
magenta := color.New(color.FgHiMagenta)
yellow := color.New(color.FgHiYellow)
body, err := ioutil.ReadAll(resp.Body)
str := string(body)
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)
f := colorjson.NewFormatter()
f.Indent = 6
s, _ := f.Marshal(obj)
retbody := fmt.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n\n%s\n", resp.Status, resp.StatusCode, string(s))
for key, value := range resp.Header {
c.Print(key, " : ")
magenta.Print(value, "\n")
}
retbody := yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(s))
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
return retbody
}

func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
Expand Down
2 changes: 1 addition & 1 deletion methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func Getbasic(c *cli.Context) error {
}
defer resp.Body.Close()
s := formatresp(resp)
fmt.Printf("\n%s", s)
fmt.Println(s)
return nil
}
35 changes: 35 additions & 0 deletions methods/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package methods

import (
"bytes"
"fmt"
"net/http"

"github.com/urfave/cli"
)

//Patchbasic sends a basic PATCH request
func Patchbasic(c *cli.Context) {
url := c.String("url")
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", c.String("ctype"))
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
}
if c.String("u") != "" && c.String("p") != "" {
un := c.String("u")
pw := c.String("p")
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
s := formatresp(resp)
fmt.Println(s)
}
2 changes: 1 addition & 1 deletion methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func Postbasic(c *cli.Context) {
}
defer resp.Body.Close()
s := formatresp(resp)
fmt.Println("response Body:", s)
fmt.Println(s)
}
36 changes: 36 additions & 0 deletions methods/put.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package methods

import (
"bytes"
"fmt"
"net/http"

"github.com/urfave/cli"
)

//Putbasic sends a basic PUT request
func Putbasic(c *cli.Context) {
url := c.String("url")
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", c.String("ctype"))
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
}
if c.String("u") != "" && c.String("p") != "" {
un := c.String("u")
pw := c.String("p")
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

s := formatresp(resp)
fmt.Println(s)
}

0 comments on commit 8a00220

Please sign in to comment.