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

Commit

Permalink
Added Command for Basic Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 20, 2020
1 parent 00aacb3 commit 6238bb1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 31 deletions.
47 changes: 35 additions & 12 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,42 @@ func main() {
Usage: "Send the Request with Bearer Token",
},
}
app.Commands = []cli.Command{{
Name: "get",
Usage: "Send a GET request",
Flags: myFlags,
Action: func(c *cli.Context) error {
if c.String("token") != "" {
mets.Getwtoken(c)
} else {
mets.Getreq(c)
}
return nil
bauthFlags := []cli.Flag{
cli.StringFlag{
Name: "u",
Value: "Username",
Usage: "Add the Username",
Required: true,
},
cli.StringFlag{
Name: "p",
Value: "Password",
Usage: "Add the Password",
Required: true,
},
}
app.Commands = []cli.Command{
{
Name: "get",
Usage: "Send a GET request",
Flags: myFlags,
Action: func(c *cli.Context) error {
if c.String("token") != "" {
mets.Getwtoken(c)
} else {
mets.Getreq(c)
}
return nil
},
},
{
Name: "bauth",
Usage: "Send Request with Basic Auth",
Flags: bauthFlags,
Action: func(c *cli.Context) error {
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions methods/fns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package methods

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/TylerBrock/colorjson"
)

// Formatresp formats the Response with Indents and Colors
func Formatresp(resp *http.Response) string {
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))
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
return retbody
}
46 changes: 27 additions & 19 deletions methods/get.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package methods

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/TylerBrock/colorjson"
"github.com/urfave/cli"
)

Expand All @@ -24,8 +21,8 @@ func Getreq(c *cli.Context) error {
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
body, err := ioutil.ReadAll(resp.Body)
s := formatresp(body)
defer resp.Body.Close()
s := Formatresp(resp)
//log.Println()
fmt.Print(resp)
fmt.Printf("\n\n %s", s)
Expand All @@ -39,25 +36,36 @@ func Getwtoken(c *cli.Context) error {
req, err := http.NewRequest("GET", url, nil)

req.Header.Add("Authorization", bearer)

client := &http.Client{}
resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
s := formatresp(body)
//log.Println()
fmt.Print(resp)
fmt.Printf("\n\n %s", s)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
s := Formatresp(resp)
fmt.Print(resp)
fmt.Printf("\n\n %s", s)

return nil
}
func formatresp(body []byte) string {
str := string(body)
var obj map[string]interface{}
json.Unmarshal([]byte(str), &obj)
f := colorjson.NewFormatter()
f.Indent = 6
s, _ := f.Marshal(obj)
return string(s)

//Getbasic send a request with Baic Auth
func Getbasic(c *cli.Context) error {
return nil
}

// Dummy Code
/* func basicAuth() string {
var username string = "foo"
var passwd string = "bar"
client := &http.Client{}
req, err := http.NewRequest("GET", "mydomain.com", nil)
req.SetBasicAuth(username, passwd)
resp, err := client.Do(req)
if err != nil{
log.Fatal(err)
}
bodyText, err := ioutil.ReadAll(resp.Body)
s := string(bodyText)
return s
} */

0 comments on commit 6238bb1

Please sign in to comment.