From 50900c5e18bd5aa1a2329ca3d53ca3713ec5d137 Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 19 Mar 2020 00:13:54 +0530 Subject: Remove package cetus Moved req.go to request package & version to main.go --- pkg/request/req.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pkg/request/req.go (limited to 'pkg/request') diff --git a/pkg/request/req.go b/pkg/request/req.go new file mode 100644 index 0000000..350f5f6 --- /dev/null +++ b/pkg/request/req.go @@ -0,0 +1,44 @@ +package request + +import ( + "fmt" + "io/ioutil" + "net/http" + "time" +) + +// GetRes returns api response +func GetRes(api string, params map[string]string) (string, error) { + c := http.Client{ + // TODO: timeout should be configurable by the user + Timeout: time.Second * 64, + } + + req, err := http.NewRequest(http.MethodGet, api, nil) + if err != nil { + return "", err + } + + q := req.URL.Query() + for k, v := range params { + q.Add(k, v) + } + req.URL.RawQuery = q.Encode() + + res, err := c.Do(req) + if err != nil { + return "", err + } + defer res.Body.Close() + + if res.StatusCode != 200 { + return "", fmt.Errorf("Unexpected response status code received: %d %s", + res.StatusCode, http.StatusText(res.StatusCode)) + } + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return "", err + } + return string(body), err +} -- cgit 1.4.1-2-gfad0