|
| 1 | +package methods |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/fatih/color" |
| 12 | + "github.com/urfave/cli" |
| 13 | +) |
| 14 | + |
| 15 | +//Colls hold the format of the basic `postwoman-collection.json` |
| 16 | +type Colls struct { |
| 17 | + Name string `json:"name"` |
| 18 | + Folders []string `json:"folders"` |
| 19 | + Request []Reqdata `json:"requests"` |
| 20 | +} |
| 21 | + |
| 22 | +//Reqdata hold the format of the request part in `postwoman-collection.json` |
| 23 | +type Reqdata struct { |
| 24 | + URL string `json:"url"` |
| 25 | + Path string `json:"path"` |
| 26 | + Method string `json:"method"` |
| 27 | + Auth string `json:"auth"` |
| 28 | + User string `json:"httpUser"` |
| 29 | + Pass string `json:"httpPassword"` |
| 30 | + Token string `json:"bearerToken"` |
| 31 | + Ctype string `json:"contentType"` |
| 32 | + Heads []string `json:"headers"` |
| 33 | + Params []string `json:"params"` |
| 34 | + Bparams []Bpardata `json:"bodyParams"` |
| 35 | +} |
| 36 | + |
| 37 | +//Bpardata hold the format of the bodyParams of `postwoman-collection.json` |
| 38 | +type Bpardata struct { |
| 39 | + Key string `json:"key"` |
| 40 | + Value string `json:"value"` |
| 41 | +} |
| 42 | + |
| 43 | +//ReadCollection reads the PostWoman Collection Json File and does the Magic Stuff |
| 44 | +func ReadCollection(c *cli.Context) { |
| 45 | + data, err := ioutil.ReadFile(c.String("pt")) |
| 46 | + if err != nil { |
| 47 | + fmt.Print(err) |
| 48 | + } |
| 49 | + //fmt.Print(string(data)) |
| 50 | + jsondat := []Colls{} |
| 51 | + |
| 52 | + err = json.Unmarshal([]byte(data), &jsondat) |
| 53 | + if err != nil { |
| 54 | + fmt.Println(err) |
| 55 | + } |
| 56 | + for i := 0; i < len(jsondat[0].Request); i++ { |
| 57 | + /* fmt.Printf(` |
| 58 | + URL: %s |
| 59 | + Method: %s |
| 60 | + Auth: %s |
| 61 | + Token:%s |
| 62 | + Headers: %s |
| 63 | + -------`, jsondat[0].Request[i].URL+jsondat[0].Request[i].Path, jsondat[0].Request[i].Method, jsondat[0].Request[i].Auth, jsondat[0].Request[i].Token, jsondat[0].Request[i].Heads) */ |
| 64 | + request(jsondat, i) |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +func request(c []Colls, i int) { |
| 69 | + colors := color.New(color.FgHiRed, color.Bold) |
| 70 | + if c[0].Request[i].Method == "GET" { |
| 71 | + out, err := getsend(c, i, "GET") |
| 72 | + if err != nil { |
| 73 | + fmt.Print(err) |
| 74 | + } |
| 75 | + methods := color.HiYellowString(c[0].Request[i].Method) |
| 76 | + fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path) |
| 77 | + fmt.Printf("%s \t%s\t%s", fURL, methods, out) |
| 78 | + } else { |
| 79 | + out, err := sendpopa(c, i, c[0].Request[i].Method) |
| 80 | + if err != nil { |
| 81 | + fmt.Print(err) |
| 82 | + } |
| 83 | + methods := color.HiYellowString(c[0].Request[i].Method) |
| 84 | + fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path) |
| 85 | + fmt.Printf("%s \t%s\t%s", fURL, methods, out) |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | +func getsend(c []Colls, ind int, method string) (string, error) { |
| 90 | + color := color.New(color.FgCyan, color.Bold) |
| 91 | + var url = c[0].Request[ind].URL + c[0].Request[ind].Path |
| 92 | + req, err := http.NewRequest(method, url, nil) |
| 93 | + if err != nil { |
| 94 | + return "", err |
| 95 | + } |
| 96 | + if c[0].Request[ind].Token != "" { |
| 97 | + var bearer = "Bearer " + c[0].Request[ind].Token |
| 98 | + req.Header.Add("Authorization", bearer) |
| 99 | + } |
| 100 | + if c[0].Request[ind].User != "" && c[0].Request[ind].Pass != "" { |
| 101 | + un := c[0].Request[ind].User |
| 102 | + pw := c[0].Request[ind].Pass |
| 103 | + req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) |
| 104 | + } |
| 105 | + client := &http.Client{} |
| 106 | + resp, err := client.Do(req) |
| 107 | + if err != nil { |
| 108 | + log.Println("Error on response.\n[ERRO] -", err) |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + //fmt.Print(resp.Header) |
| 112 | + s := color.Sprintf("Status: %s\tStatusCode:\t%d\n", resp.Status, resp.StatusCode) |
| 113 | + return s, nil |
| 114 | +} |
| 115 | + |
| 116 | +func sendpopa(c []Colls, ind int, method string) (string, error) { |
| 117 | + color := color.New(color.FgCyan, color.Bold) |
| 118 | + |
| 119 | + var url = c[0].Request[ind].URL + c[0].Request[ind].Path |
| 120 | + var jsonStr = []byte(string(c[0].Request[ind].Bparams[0].Key[0] + c[0].Request[ind].Bparams[0].Value[0])) |
| 121 | + |
| 122 | + req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonStr)) |
| 123 | + req.Header.Set("Content-Type", c[0].Request[ind].Ctype) |
| 124 | + if err != nil { |
| 125 | + return "", err |
| 126 | + } |
| 127 | + if c[0].Request[ind].Token != "" { |
| 128 | + var bearer = "Bearer " + c[0].Request[ind].Token |
| 129 | + req.Header.Add("Authorization", bearer) |
| 130 | + } |
| 131 | + if c[0].Request[ind].User != "" && c[0].Request[ind].Pass != "" { |
| 132 | + un := c[0].Request[ind].User |
| 133 | + pw := c[0].Request[ind].Pass |
| 134 | + req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) |
| 135 | + } |
| 136 | + client := &http.Client{} |
| 137 | + resp, err := client.Do(req) |
| 138 | + if err != nil { |
| 139 | + log.Println("Error on response.\n[ERRO] -", err) |
| 140 | + } |
| 141 | + defer resp.Body.Close() |
| 142 | + //fmt.Print(resp.Header) |
| 143 | + s := color.Sprintf("Status: %s\tStatusCode:\t%d\n", resp.Status, resp.StatusCode) |
| 144 | + return s, nil |
| 145 | + |
| 146 | +} |
0 commit comments