rewrite of async code to fix bug with single url requests

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2020-05-13 14:35:30 +01:00
parent 6660f5e20d
commit bd87185014

41
api.go
View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"sync"
"time" "time"
) )
@ -16,6 +17,7 @@ type Response struct {
StatusCode int StatusCode int
URL *url.URL URL *url.URL
Body []byte Body []byte
Error error
} }
// Options Request Options // Options Request Options
@ -25,24 +27,21 @@ type Options struct {
} }
// Req HTTP Request // Req HTTP Request
func response(method string, statuscode int, url *url.URL, body []byte) *Response { func response(method string, statuscode int, url *url.URL, body []byte, err error) *Response {
return &Response{ return &Response{
Method: method, Method: method,
StatusCode: statuscode, StatusCode: statuscode,
URL: url, URL: url,
Body: body, Body: body,
Error: err,
} }
} }
func apiError(url string, path string, statuscode int) error {
return fmt.Errorf("api: %s/%s - %d %s", url, path, statuscode, http.StatusText(statuscode))
}
// API sends RESTful API requests // API sends RESTful API requests
func API(method string, r *Options, path string, data []byte) (*Response, error) { func API(method string, r *Options, path string, data []byte) (*Response, error) {
req, err := http.NewRequest(method, r.URL+"/"+path, bytes.NewBuffer(data)) req, err := http.NewRequest(method, r.URL+"/"+path, bytes.NewBuffer(data))
if err != nil { if err != nil {
return response(method, http.StatusInternalServerError, req.URL, nil), err return response(method, http.StatusInternalServerError, req.URL, nil, err), err
} }
for k, v := range r.Headers { for k, v := range r.Headers {
req.Header.Set(k, v) req.Header.Set(k, v)
@ -50,37 +49,26 @@ func API(method string, r *Options, path string, data []byte) (*Response, error)
client := &http.Client{Timeout: time.Second * 10} client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return response(method, http.StatusInternalServerError, req.URL, nil), err return response(method, http.StatusInternalServerError, req.URL, nil, err), err
} }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return response(method, http.StatusInternalServerError, req.URL, nil), err return response(method, http.StatusInternalServerError, req.URL, nil, err), err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
return response(method, resp.StatusCode, resp.Request.URL, body), nil return response(method, resp.StatusCode, resp.Request.URL, body, err), nil
} }
return response(method, resp.StatusCode, req.URL, nil), apiError(r.URL, path, resp.StatusCode) err = fmt.Errorf("api: %s/%s - %d %s", r.URL, path, resp.StatusCode, http.StatusText(resp.StatusCode))
return response(method, resp.StatusCode, req.URL, nil, err), err
} }
// AsyncAPI send requests concurrently // AsyncAPI send requests concurrently
func AsyncAPI(method string, r *Options, path string, data []byte, ch chan *Response, chFinished chan bool, chError chan error) { func AsyncAPI(method string, r *Options, path string, data []byte, ch chan<- *Response, wg *sync.WaitGroup) {
resp, err := API(method, r, path, data) defer wg.Done()
defer func() { resp, _ := API(method, r, path, data)
chFinished <- true ch <- resp
}()
if err != nil {
ch <- response(method, resp.StatusCode, resp.URL, nil)
chError <- err
return
}
if resp.StatusCode == 200 {
ch <- response(method, resp.StatusCode, resp.URL, resp.Body)
return
}
ch <- response(method, resp.StatusCode, resp.URL, nil)
chError <- apiError(r.URL, path, resp.StatusCode)
} }
// JSONReq Request // JSONReq Request
@ -94,7 +82,6 @@ func JSONParse(r *Options, path string) (*JSONReq, error) {
var result map[string]interface{} var result map[string]interface{}
resp, err := API(http.MethodGet, r, path, nil) resp, err := API(http.MethodGet, r, path, nil)
if err != nil { if err != nil {
fmt.Println(err)
return &JSONReq{StatusCode: resp.StatusCode}, err return &JSONReq{StatusCode: resp.StatusCode}, err
} }
json.Unmarshal(resp.Body, &result) json.Unmarshal(resp.Body, &result)