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

Commit

Permalink
Made GET requests Working
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 30, 2020
1 parent ebcb9f0 commit c75a2a7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
17 changes: 16 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ func main() {
Usage: "Body of the Post Request",
},
}

sendFlag := []cli.Flag{
cli.StringFlag{
Name: "pt",
Usage: "The Path of Postwoman Collection.json",
Required: true,
},
}
app.Commands = []cli.Command{
{
Name: "get",
Expand Down Expand Up @@ -111,6 +117,15 @@ func main() {
return nil
},
},
{
Name: "send",
Usage: "Test all the Endpoints in the Postwoman Collection.json",
Flags: sendFlag,
Action: func(c *cli.Context) error {
mets.ReadCollection(c)
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,27 @@ func Getbasic(c *cli.Context) error {
fmt.Println(s)
return nil
}
func getsend(c []Colls, ind int, method string) (string, error) {
var url = c[0].Request[ind].URL + c[0].Request[ind].Path
req, err := http.NewRequest(method, url, nil)
if err != nil {
return "", err
}
if c[0].Request[ind].Token != "" {
var bearer = "Bearer " + c[0].Request[ind].Token
req.Header.Add("Authorization", bearer)
}
if c[0].Request[ind].User != "" && c[0].Request[ind].Pass != "" {
un := c[0].Request[ind].User
pw := c[0].Request[ind].Pass
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
s := fmt.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode)
return s, nil
}
39 changes: 28 additions & 11 deletions methods/json.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package methods

import (
"encoding/json"
"fmt"
"io/ioutil"

"github.com/urfave/cli"
)

//Colls hold the format of the basic `postwoman-collection.json`
type Colls struct {
Name string `json:"name"`
Expand Down Expand Up @@ -26,8 +36,9 @@ type Bpardata struct {
Value string `json:"value"`
}

func main() {
data, err := ioutil.ReadFile("/Users/athul/Downloads/pwclc.json")
//ReadCollection reads the PostWoman Collection Json File and does the Magic Stuff
func ReadCollection(c *cli.Context) {
data, err := ioutil.ReadFile(c.String("pt"))
if err != nil {
fmt.Print(err)
}
Expand All @@ -38,19 +49,25 @@ func main() {
if err != nil {
fmt.Println(err)
}
//fmt.Println(jsondat)
//fmt.Print(jsondat[0].Name + "\n")
//fmt.Println(jsondat[0].Request[4].URL)
//fmt.Println(jsondat[0].Request[0].Method)
for i := 0; i < len(jsondat[0].Request); i++ {
fmt.Printf(`
URL: %s
URL: %s%s
Method: %s
Auth: %s
-------`, jsondat[0].Request[i].URL, jsondat[0].Request[i].Method, jsondat[0].Request[i].Auth)
-------`, jsondat[0].Request[i].URL, jsondat[0].Request[i].Path, jsondat[0].Request[i].Method, jsondat[0].Request[i].Auth)
request(jsondat, i)
}

}
func request(c []Colls, i int) {
if c[0].Request[i].Method == "GET" || c[0].Request[i].Method == "POST" {
//fmt.Print(c[0].Request[i].Method)
out, err := getsend(c, i, c[0].Request[i].Method)
if err != nil {
fmt.Print(err)
}

fmt.Printf("%s %d", out, i)
}

}
func request(){

}

0 comments on commit c75a2a7

Please sign in to comment.