various improvemnts and fixes

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2021-03-09 01:16:26 +00:00
parent 2d12094f85
commit 61b3c71fca

88
api.go
View File

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"sync"
"time" "time"
) )
@ -16,14 +15,13 @@ type Response struct {
StatusCode int StatusCode int
URL *url.URL URL *url.URL
Body []byte Body []byte
Error error
} }
// API sends RESTful API requests // Request sends RESTful requests
func API(method, url string, headers map[string]string, data io.Reader) (*Response, error) { func Request(method, url string, headers map[string]string, data io.Reader) (*Response, error) {
req, err := http.NewRequest(method, url, data) req, err := http.NewRequest(method, url, data)
if err != nil { if err != nil {
return &Response{method, http.StatusInternalServerError, req.URL, nil, err}, err return &Response{method, http.StatusInternalServerError, req.URL, nil}, err
} }
for k, v := range headers { for k, v := range headers {
req.Header.Set(k, v) req.Header.Set(k, v)
@ -31,24 +29,84 @@ func API(method, url string, headers map[string]string, data io.Reader) (*Respon
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}, err return &Response{method, http.StatusInternalServerError, req.URL, nil}, 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}, err return &Response{method, http.StatusInternalServerError, req.URL, nil}, 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, err}, nil return &Response{method, resp.StatusCode, resp.Request.URL, body}, nil
} }
err = fmt.Errorf("api: %s - %d %s", url, resp.StatusCode, http.StatusText(resp.StatusCode)) err = fmt.Errorf("request: %s - %d %s", url, resp.StatusCode, http.StatusText(resp.StatusCode))
return &Response{method, resp.StatusCode, resp.Request.URL, nil, err}, err return &Response{method, resp.StatusCode, resp.Request.URL, nil}, err
} }
// AsyncAPI send requests concurrently // Get sends GET request
func AsyncAPI(method, url string, headers map[string]string, data io.Reader, ch chan<- *Response, wg *sync.WaitGroup) { func Get(url string, headers map[string]string, data io.Reader) (*Response, error) {
defer wg.Done() resp, err := Request(http.MethodGet, url, headers, data)
resp, _ := API(method, url, headers, data) if err != nil {
return nil, err
}
return resp, nil
}
// Post sends POST request
func Post(url string, headers map[string]string, data io.Reader) (*Response, error) {
resp, err := Request(http.MethodPost, url, headers, data)
if err != nil {
return nil, err
}
return resp, nil
}
// Put sends PUT request
func Put(url string, headers map[string]string, data io.Reader) (*Response, error) {
resp, err := Request(http.MethodPut, url, headers, data)
if err != nil {
return nil, err
}
return resp, nil
}
// Delete sends DELETE request
func Delete(url string, headers map[string]string, data io.Reader) (*Response, error) {
resp, err := Request(http.MethodDelete, url, headers, data)
if err != nil {
return nil, err
}
return resp, nil
}
// AsyncRequest send requests concurrently
func AsyncRequest(method, url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
defer func() {
chDone <- true
}()
resp, err := Request(method, url, headers, data)
if err != nil {
chErr <- err
}
ch <- resp ch <- resp
} }
// AsyncGet sends GET requests concurrently
func AsyncGet(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
AsyncRequest(http.MethodGet, url, headers, data, ch, chErr, chDone)
}
// AsyncPost sends POST requests concurrently
func AsyncPost(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
AsyncRequest(http.MethodPost, url, headers, data, ch, chErr, chDone)
}
// AsyncPut sends PUT requests concurrently
func AsyncPut(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
AsyncRequest(http.MethodPut, url, headers, data, ch, chErr, chDone)
}
// AsyncDelete sends DELETE requests concurrently
func AsyncDelete(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
AsyncRequest(http.MethodDelete, url, headers, data, ch, chErr, chDone)
}