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

Commit

Permalink
Add Support for Tabular Data and Headers (#22)
Browse files Browse the repository at this point in the history
* Add Support for Tabular Data and Headers

* [Refactor] Minor Fixes
  • Loading branch information
athul authored Oct 18, 2020
1 parent b848064 commit f1b75a1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 44 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/fatih/color v1.9.0
github.com/olekukonko/tablewriter v0.0.4
github.com/stretchr/testify v1.4.0 // indirect
github.com/tidwall/pretty v1.0.1
github.com/urfave/cli v1.22.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
114 changes: 70 additions & 44 deletions methods/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"go.uber.org/multierr"
)

Expand All @@ -34,57 +37,61 @@ func ReadCollection(filename string) ([]Collection, error) {
// ProcessCollection parses the Collection struct and execute the said requests
func ProcessCollection(jsonArr []Collection) (string, error) {
var (
out string
errs error
out string
errs error
tabData [][]string
)
for _, jsondat := range jsonArr {
fmt.Printf("\n------------\nName:\t%s\n", color.HiMagentaString(jsondat.Name))
fmt.Printf("\nName:\t%s\n", color.HiMagentaString(jsondat.Name))
if len(jsondat.Folders) > 0 {
if err := jsondat.getDatafromFolders(); err != nil {
return "", err
}
} else {
request := jsondat.Requests
for _, req := range request {
err := jsondat.request(req)

tabdata, err := jsondat.request(req)
tabData = append(tabData, tabdata)
if err != nil {
errs = multierr.Append(errs, err)
}
}
genTables(tabData)
}
}
return out, errs
}

// request process the Requests type and executes the Requests synchronously
func (c *Collection) request(req Requests) error {
func (c *Collection) request(req Requests) ([]string, error) {
var (
colored = color.New(color.FgHiRed, color.Bold) // Red string color
fURL = req.URL + req.Path // Full URL
err error
out, paramURL string
colored = color.New(color.FgHiRed, color.Bold) // Red string color
fURL = req.URL + req.Path // Full URL
err error
paramURL, status, code string
tabData []string
)

if req.Method == "GET" {
out, paramURL, err = c.sendGET(req)
status, code, paramURL, err = c.sendGET(req)
} else {
out, err = c.sendPOST(req, req.Method)
status, code, err = c.sendPOST(req, req.Method)
}
if err != nil {
return err
return nil, err
}
if paramURL != "" {
fmt.Printf("%s |\t%s | %s |\t%s\n", color.HiGreenString(req.Name), colored.Sprintf(paramURL), color.HiYellowString(req.Method), out)
tabData = []string{color.HiGreenString(req.Name), colored.Sprintf(paramURL), color.HiYellowString(req.Method), status, code}
} else {
fmt.Printf("%s |\t%s | %s |\t%s\n", color.HiGreenString(req.Name), colored.Sprintf(fURL), color.HiYellowString(req.Method), out)
tabData = []string{color.HiGreenString(req.Name), colored.Sprintf(fURL), color.HiYellowString(req.Method), status, code}
}

return nil
return tabData, nil
}

// sendGet sends a GET request to the said URL and returns a Response string and if it contains a
// params in the URL
func (c *Collection) sendGET(req Requests) (string, string, error) {
func (c *Collection) sendGET(req Requests) (string, string, string, error) {
var (
url = req.URL + req.Path
bearer string
Expand All @@ -103,8 +110,13 @@ func (c *Collection) sendGET(req Requests) (string, string, error) {
url += paramstr
}
reqHTTP, err := http.NewRequest("GET", url, nil)
if len(req.Headers) > 0 {
for _, head := range req.Headers {
reqHTTP.Header.Set(head.Key, head.Value)
}
}
if err != nil {
return "", "", fmt.Errorf("Error creating request: %s", err.Error())
return "", "", "", fmt.Errorf("Error creating request: %s", err.Error())
}

if req.Token != "" {
Expand All @@ -121,24 +133,25 @@ func (c *Collection) sendGET(req Requests) (string, string, error) {
client := getHTTPClient()
resp, err := client.Do(reqHTTP)
if err != nil {
return "", "", fmt.Errorf("Error sending request: %s", err.Error())
return "", "", "", fmt.Errorf("Error sending request: %s", err.Error())
}
defer resp.Body.Close()
// paramstr is suffixed with a `?` to append to the URL
if paramstr != "?" {
s := fmt.Sprintf("Status: %s\tStatusCode:\t%s \n", color.HiBlueString(resp.Status), color.New(color.FgHiBlue).Sprintln(resp.StatusCode))
return s, url, nil
status := fmt.Sprintf("%s", color.HiBlueString(resp.Status))
code := fmt.Sprintf("%s", color.New(color.FgHiBlue).Sprintln(resp.StatusCode))
return status, code, url, nil
}
s := fmt.Sprintf("Status: %s\tStatusCode:\t%s\n", color.HiBlueString(resp.Status), color.New(color.FgHiBlue).Sprintln(resp.StatusCode))
return s, "", nil
status := fmt.Sprintf("%s", color.HiBlueString(resp.Status))
code := fmt.Sprintf("%s", color.New(color.FgHiBlue).Sprintln(resp.StatusCode))
return status, code, "", nil
}

// sendPOST sends all request other than GET requests since
// it's the only one which has BodyParams and RawParams
func (c *Collection) sendPOST(req Requests, method string) (string, error) {
func (c *Collection) sendPOST(req Requests, method string) (string, string, error) {

var (
// colorcy = color.New(color.FgCyan, color.Bold)
jsonStr []byte
url = req.URL + req.Path
reqData = req
Expand All @@ -164,11 +177,16 @@ func (c *Collection) sendPOST(req Requests, method string) (string, error) {

finalBytes, err := json.RawMessage(jsonStr).MarshalJSON() // Marshal to JSON from strings
if err != nil {
return "", fmt.Errorf("Error Marhsaling JSON: %s", err.Error())
return "", "", fmt.Errorf("Error Marhsaling JSON: %s", err.Error())
}
reqHTTP, err := http.NewRequest(method, url, bytes.NewBuffer(finalBytes))
if len(req.Headers) > 0 {
for _, head := range req.Headers {
reqHTTP.Header.Set(head.Key, head.Value)
}
}
if err != nil {
return "", fmt.Errorf("Error creating request: %s", err.Error())
return "", "", fmt.Errorf("Error creating request: %s", err.Error())
}

reqHTTP.Header.Set("Content-Type", req.Ctype) // Set Content type to said Ctype in Collection
Expand All @@ -179,50 +197,58 @@ func (c *Collection) sendPOST(req Requests, method string) (string, error) {
}
if req.User != "" && req.Pass != "" {
// Basic Auth
un := req.User
pw := req.Pass
reqHTTP.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
reqHTTP.Header.Add("Authorization", "Basic "+basicAuth(req.User, req.Pass))
}

client := getHTTPClient()
resp, err := client.Do(reqHTTP)
if err != nil {
return "", fmt.Errorf("Error sending request: %s", err.Error())
return "", "", fmt.Errorf("Error sending request: %s", err.Error())
}

defer resp.Body.Close()

s := fmt.Sprintf("Status: %s\tStatusCode:\t%s\n", color.HiBlueString(resp.Status), color.New(color.FgHiBlue).Sprintln(resp.StatusCode))
return s, nil
status := fmt.Sprintf("%s", color.HiBlueString(resp.Status))
code := fmt.Sprintf("%s", color.New(color.FgHiBlue).Sprintln(resp.StatusCode))
return status, code, nil
}

// getDatafromFolders handle edge cases when requests are saved inside folders
// from hoppscotch itself
// This adds another loop to check for folders and requests
func (c *Collection) getDatafromFolders() error {
var (
err error
out, paramURL string
err error
paramURL, status, code string
tabData [][]string
)
for _, Folder := range c.Folders {
for j := range Folder.Requests {
fmt.Printf("Folder Name:\t%s\n", color.HiMagentaString(Folder.Name))
fURL := fmt.Sprintf(Folder.Requests[j].URL + Folder.Requests[j].Path)
method := Folder.Requests[j].Method
for _, fRequest := range Folder.Requests {
fURL := fmt.Sprintf(fRequest.URL + fRequest.Path)
method := fRequest.Method
if method == "GET" {
out, paramURL, err = c.sendGET(Folder.Requests[j])
status, code, paramURL, err = c.sendGET(fRequest)
} else {
out, err = c.sendPOST(Folder.Requests[j], method)
status, code, err = c.sendPOST(fRequest, method)
}
if err != nil {
return err
log.Println(err)
}
if paramURL != "" {
fmt.Printf("%s |\t%s | %s |\t%s\n", color.HiGreenString(Folder.Requests[j].Name), color.HiRedString(paramURL), color.HiYellowString(method), out)
tabData = append(tabData, []string{color.HiGreenString(fRequest.Name), color.HiRedString(paramURL), color.HiYellowString(method), status, code})
} else {
fmt.Printf("%s |\t%s | %s |\t%s\n", color.HiGreenString(Folder.Requests[j].Name), color.HiRedString(fURL), color.HiYellowString(method), out)
tabData = append(tabData, []string{color.HiGreenString(fRequest.Name), color.HiRedString(fURL), color.HiYellowString(method), status, code})
}
}
}
genTables(tabData)
return nil
}

//genTables generate the output in Tabular Form
func genTables(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "URL", "Method", "Status", "Code"})
table.AppendBulk(data)
table.Render()
}

0 comments on commit f1b75a1

Please sign in to comment.