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

Commit

Permalink
🛠Refactor: Reduced Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 24, 2020
1 parent c703f7b commit 8d552a5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 61 deletions.
11 changes: 1 addition & 10 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,7 @@ func main() {
Usage: "Send a GET request",
Flags: getFlags,
Action: func(c *cli.Context) error {
switch {
case c.String("token") != "":
mets.Authwtoken(c, "GET")
break
case c.String("u") != "":
mets.Authbasic(c, "GET")
default:
mets.Basicreq(c)
}

mets.Basicreq(c)
return nil
},
},
Expand Down
5 changes: 5 additions & 0 deletions methods/fns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package methods

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -25,3 +26,7 @@ func Formatresp(resp *http.Response) string {
}
return retbody
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
62 changes: 11 additions & 51 deletions methods/get.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package methods

import (
"encoding/base64"
"fmt"
"log"
"net/http"

"github.com/urfave/cli"
)

//Basicreq sends a simple GET request to the url
//Basicreq sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth
func Basicreq(c *cli.Context) error {
var url = c.String("url")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
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))
}
fmt.Print(req.Header)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand All @@ -28,52 +37,3 @@ func Basicreq(c *cli.Context) error {
fmt.Printf("\n\n %s", s)
return nil
}

//Authwtoken send a get request with the Token for Authorization Header
func Authwtoken(c *cli.Context, method string) error {
var url = c.String("url")
var bearer = "Bearer " + c.String("token")
req, err := http.NewRequest(method, url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
s := Formatresp(resp)
if s != "" {
fmt.Printf("%s", s)
} else {
fmt.Print(resp)
}
return nil
}

//Authbasic helps you send a request with Basic Auth as Authorization Method
func Authbasic(c *cli.Context, method string) error {
un := c.String("u")
pw := c.String("p")
url := c.String("url")
req, err := http.NewRequest(method, url, nil)
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 := Formatresp(resp)
if s != "" {
fmt.Printf("%s", s)
} else {
fmt.Print(resp)
}

return nil
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}

0 comments on commit 8d552a5

Please sign in to comment.