diff options
author | Andinus <andinus@inventati.org> | 2020-03-16 00:57:28 +0530 |
---|---|---|
committer | Andinus <andinus@inventati.org> | 2020-03-16 00:57:28 +0530 |
commit | 53ecbbc03b8cd8e54479c2d90cbdae9921eaa127 (patch) | |
tree | 5a4485c95d1b98ef54e0c8fb7e69b68f57f1e26a /pkg/cetus/req.go | |
parent | 14c085116e77ba8355b0002266ce46d0715256cd (diff) | |
download | cetus-53ecbbc03b8cd8e54479c2d90cbdae9921eaa127.tar.gz |
Add dump option & rewrite bpod response retrieval function
Diffstat (limited to 'pkg/cetus/req.go')
-rw-r--r-- | pkg/cetus/req.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/pkg/cetus/req.go b/pkg/cetus/req.go new file mode 100644 index 0000000..84e52a5 --- /dev/null +++ b/pkg/cetus/req.go @@ -0,0 +1,43 @@ +package cetus + +import ( + "fmt" + "io/ioutil" + "net/http" + "time" +) + +// GetRes returns api response +func GetRes(api string, params map[string]string, t time.Duration) (string, error) { + c := http.Client{ + Timeout: time.Second * t, + } + + 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 +} |