2019-06-09 13:05:36 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
2020-01-02 01:02:33 +00:00
|
|
|
"bytes"
|
2019-06-13 17:11:42 +00:00
|
|
|
"encoding/json"
|
2019-11-21 22:37:26 +00:00
|
|
|
"fmt"
|
2019-06-13 17:11:42 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-01-02 01:02:33 +00:00
|
|
|
"net/url"
|
2020-05-13 13:35:30 +00:00
|
|
|
"sync"
|
2019-06-13 17:11:42 +00:00
|
|
|
"time"
|
2019-06-09 13:05:36 +00:00
|
|
|
)
|
|
|
|
|
2020-02-15 03:05:26 +00:00
|
|
|
// Response API return request
|
|
|
|
type Response struct {
|
2020-01-02 01:02:33 +00:00
|
|
|
Method string
|
|
|
|
StatusCode int
|
|
|
|
URL *url.URL
|
|
|
|
Body []byte
|
2020-05-13 13:35:30 +00:00
|
|
|
Error error
|
2020-01-02 01:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options Request Options
|
|
|
|
type Options struct {
|
2019-10-20 02:00:41 +00:00
|
|
|
URL string
|
2020-01-10 07:22:35 +00:00
|
|
|
Headers map[string]string
|
2019-10-20 02:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 01:02:33 +00:00
|
|
|
// Req HTTP Request
|
2020-05-13 13:35:30 +00:00
|
|
|
func response(method string, statuscode int, url *url.URL, body []byte, err error) *Response {
|
2020-02-15 03:05:26 +00:00
|
|
|
return &Response{
|
2020-01-02 01:02:33 +00:00
|
|
|
Method: method,
|
|
|
|
StatusCode: statuscode,
|
|
|
|
URL: url,
|
|
|
|
Body: body,
|
2020-05-13 13:35:30 +00:00
|
|
|
Error: err,
|
2020-01-02 01:02:33 +00:00
|
|
|
}
|
2019-06-09 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 03:34:02 +00:00
|
|
|
// API sends RESTful API requests
|
2020-02-15 03:05:26 +00:00
|
|
|
func API(method string, r *Options, path string, data []byte) (*Response, error) {
|
2020-01-02 03:34:02 +00:00
|
|
|
req, err := http.NewRequest(method, r.URL+"/"+path, bytes.NewBuffer(data))
|
2019-06-09 18:05:18 +00:00
|
|
|
if err != nil {
|
2020-05-13 13:35:30 +00:00
|
|
|
return response(method, http.StatusInternalServerError, req.URL, nil, err), err
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
2020-01-10 07:22:35 +00:00
|
|
|
for k, v := range r.Headers {
|
|
|
|
req.Header.Set(k, v)
|
2019-06-18 03:30:01 +00:00
|
|
|
}
|
2019-06-13 17:11:42 +00:00
|
|
|
client := &http.Client{Timeout: time.Second * 10}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2020-05-13 13:35:30 +00:00
|
|
|
return response(method, http.StatusInternalServerError, req.URL, nil, err), err
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-05-13 13:35:30 +00:00
|
|
|
return response(method, http.StatusInternalServerError, req.URL, nil, err), err
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
if resp.StatusCode == 200 {
|
2020-05-13 13:35:30 +00:00
|
|
|
return response(method, resp.StatusCode, resp.Request.URL, body, err), nil
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
2020-05-13 13:35:30 +00:00
|
|
|
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
|
2020-02-10 18:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:50:00 +00:00
|
|
|
// AsyncAPI send requests concurrently
|
2020-05-13 13:35:30 +00:00
|
|
|
func AsyncAPI(method string, r *Options, path string, data []byte, ch chan<- *Response, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
resp, _ := API(method, r, path, data)
|
|
|
|
ch <- resp
|
2020-01-02 01:02:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 03:34:02 +00:00
|
|
|
// JSONReq Request
|
2020-01-02 01:02:33 +00:00
|
|
|
type JSONReq struct {
|
|
|
|
StatusCode int
|
|
|
|
Body map[string]interface{}
|
2019-06-09 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 02:00:41 +00:00
|
|
|
// JSONParse parses json data
|
2020-01-02 01:02:33 +00:00
|
|
|
func JSONParse(r *Options, path string) (*JSONReq, error) {
|
2019-10-20 02:00:41 +00:00
|
|
|
var result map[string]interface{}
|
2020-01-02 03:34:02 +00:00
|
|
|
resp, err := API(http.MethodGet, r, path, nil)
|
2019-06-20 00:33:48 +00:00
|
|
|
if err != nil {
|
2020-01-02 01:02:33 +00:00
|
|
|
return &JSONReq{StatusCode: resp.StatusCode}, err
|
2019-06-20 00:33:48 +00:00
|
|
|
}
|
2020-01-02 01:02:33 +00:00
|
|
|
json.Unmarshal(resp.Body, &result)
|
|
|
|
return &JSONReq{StatusCode: resp.StatusCode, Body: result}, nil
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|