request/api.go

103 lines
2.5 KiB
Go
Raw Normal View History

2019-06-09 13:05:36 +00:00
package request
import (
2020-01-02 01:02:33 +00:00
"bytes"
"encoding/json"
2019-11-21 22:37:26 +00:00
"fmt"
"io/ioutil"
"net/http"
2020-01-02 01:02:33 +00:00
"net/url"
"time"
2019-06-09 13:05:36 +00:00
)
2020-01-02 01:02:33 +00:00
// Request API return request
2019-10-20 02:00:41 +00:00
type Request struct {
2020-01-02 01:02:33 +00:00
Method string
StatusCode int
URL *url.URL
Body []byte
}
// 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
func Req(method string, statuscode int, url *url.URL, body []byte) *Request {
return &Request{
Method: method,
StatusCode: statuscode,
URL: url,
Body: body,
}
2019-06-09 13:05:36 +00:00
}
func apiError(url string, path string, statuscode int) error {
return fmt.Errorf("api: %s/%s - %d %s", url, path, statuscode, http.StatusText(statuscode))
}
2020-01-02 03:34:02 +00:00
// API sends RESTful API requests
func API(method string, r *Options, path string, data []byte) (*Request, error) {
req, err := http.NewRequest(method, r.URL+"/"+path, bytes.NewBuffer(data))
if err != nil {
2020-01-02 03:34:02 +00:00
return Req(method, http.StatusInternalServerError, req.URL, nil), err
}
2020-01-10 07:22:35 +00:00
for k, v := range r.Headers {
req.Header.Set(k, v)
}
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
2020-01-02 03:34:02 +00:00
return Req(method, http.StatusInternalServerError, req.URL, nil), err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2020-01-02 03:34:02 +00:00
return Req(method, http.StatusInternalServerError, req.URL, nil), err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
2020-01-02 03:34:02 +00:00
return Req(method, resp.StatusCode, resp.Request.URL, body), nil
}
return Req(method, resp.StatusCode, req.URL, nil), apiError(r.URL, path, resp.StatusCode)
}
// AsyncAPI send requests concurrently
func AsyncAPI(method string, r *Options, path string, data []byte, ch chan *Request, chFinished chan bool, chError chan error) {
resp, err := API(method, r, path, data)
defer func() {
chFinished <- true
}()
if err != nil {
ch <- Req(method, resp.StatusCode, resp.URL, nil)
chError <- err
return
}
if resp.StatusCode == 200 {
ch <- Req(method, resp.StatusCode, resp.URL, resp.Body)
return
}
ch <- Req(method, resp.StatusCode, resp.URL, nil)
chError <- apiError(r.URL, path, resp.StatusCode)
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
fmt.Println(err)
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
}