Skip to content

Commit

Permalink
setup http client interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticrabbit committed Nov 10, 2021
1 parent 9a6b486 commit 0e96812
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions internal/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,38 @@ import (
"github.com/spf13/viper"
)

var (
Client HTTPClient
)

//Requester interface needs an API path to request JSON data
type Requester interface {
BuildPath() string
GetJSON(string) []byte
WriteOut([]byte)
}

// HTTPClient interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

func init() {
Client = &http.Client{}
}

//Gets JSON data for any struct that implements Requester interface
func getJSON(apiPath string) []byte {
token := viper.GetString("ctoken")
client := &http.Client{}

req, _ := http.NewRequest(http.MethodGet, apiPath, nil)

req.Header.Add("Authorization", token)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
resp, err := Client.Do(req)
if err != nil {
log.Fatalln("Errored when sending request to the server")
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)

return resp_body
// fileFlag := viper.GetBool("file")
// if !fileFlag {
// fmt.Println(string(resp_body))
// }
}

func (t TaskRequest) GetJSON(apiPath string) []byte {
return getJSON(apiPath)
}

0 comments on commit 0e96812

Please sign in to comment.