various improvemnts and fixes
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
2d12094f85
commit
61b3c71fca
88
api.go
88
api.go
@ -6,7 +6,6 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -16,14 +15,13 @@ type Response struct {
|
||||
StatusCode int
|
||||
URL *url.URL
|
||||
Body []byte
|
||||
Error error
|
||||
}
|
||||
|
||||
// API sends RESTful API requests
|
||||
func API(method, url string, headers map[string]string, data io.Reader) (*Response, error) {
|
||||
// Request sends RESTful requests
|
||||
func Request(method, url string, headers map[string]string, data io.Reader) (*Response, error) {
|
||||
req, err := http.NewRequest(method, url, data)
|
||||
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 {
|
||||
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}
|
||||
resp, err := client.Do(req)
|
||||
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)
|
||||
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()
|
||||
|
||||
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))
|
||||
return &Response{method, resp.StatusCode, resp.Request.URL, nil, err}, err
|
||||
err = fmt.Errorf("request: %s - %d %s", url, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
return &Response{method, resp.StatusCode, resp.Request.URL, nil}, err
|
||||
}
|
||||
|
||||
// AsyncAPI send requests concurrently
|
||||
func AsyncAPI(method, url string, headers map[string]string, data io.Reader, ch chan<- *Response, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
resp, _ := API(method, url, headers, data)
|
||||
// Get sends GET request
|
||||
func Get(url string, headers map[string]string, data io.Reader) (*Response, error) {
|
||||
resp, err := Request(http.MethodGet, 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
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user