From d910db375a197709b735717950eeb47f2f981fb6 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 11 Nov 2019 00:46:47 +0000 Subject: [PATCH] more rewriting --- api.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/api.go b/api.go index bc34c87..61ff3f0 100755 --- a/api.go +++ b/api.go @@ -20,40 +20,31 @@ type HTTPError struct { Error error } -// HTTPErr HTTP Error -func HTTPErr(StatusCode int, err error) *HTTPError { - return &HTTPError{ - StatusCode: StatusCode, - Error: err} -} - // Get sends GET request to path func Get(r *Request, path string) ([]byte, *HTTPError) { req, err := http.NewRequest("GET", r.URL+"/"+path, nil) if err != nil { - return nil, HTTPErr(http.StatusInternalServerError, err) + return nil, &HTTPError{StatusCode: http.StatusInternalServerError, Error: err} } - for i := 0; i < len(r.Headers); i++ { header := strings.Split(r.Headers[i], ",") req.Header.Set(header[0], header[1]) } - client := &http.Client{Timeout: time.Second * 10} resp, err := client.Do(req) if err != nil { - return nil, HTTPErr(http.StatusInternalServerError, err) + return nil, &HTTPError{StatusCode: http.StatusInternalServerError, Error: err} } body, err := ioutil.ReadAll(resp.Body) if err != nil { - return nil, HTTPErr(http.StatusInternalServerError, err) + return nil, &HTTPError{StatusCode: http.StatusInternalServerError, Error: err} } defer resp.Body.Close() if resp.StatusCode == 200 { return body, nil } - return nil, HTTPErr(resp.StatusCode, nil) + return nil, &HTTPError{StatusCode: resp.StatusCode, Error: nil} } // JSONParse parses json data