more sensible naming
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
d683b10c8a
commit
5a50d60a12
28
api.go
28
api.go
@ -10,8 +10,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Request API return request
|
// Response API return request
|
||||||
type Request struct {
|
type Response struct {
|
||||||
Method string
|
Method string
|
||||||
StatusCode int
|
StatusCode int
|
||||||
URL *url.URL
|
URL *url.URL
|
||||||
@ -25,8 +25,8 @@ type Options struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Req HTTP Request
|
// Req HTTP Request
|
||||||
func Req(method string, statuscode int, url *url.URL, body []byte) *Request {
|
func response(method string, statuscode int, url *url.URL, body []byte) *Response {
|
||||||
return &Request{
|
return &Response{
|
||||||
Method: method,
|
Method: method,
|
||||||
StatusCode: statuscode,
|
StatusCode: statuscode,
|
||||||
URL: url,
|
URL: url,
|
||||||
@ -39,10 +39,10 @@ func apiError(url string, path string, statuscode int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// API sends RESTful API requests
|
// API sends RESTful API requests
|
||||||
func API(method string, r *Options, path string, data []byte) (*Request, 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 Req(method, http.StatusInternalServerError, req.URL, nil), err
|
return response(method, http.StatusInternalServerError, req.URL, nil), err
|
||||||
}
|
}
|
||||||
for k, v := range r.Headers {
|
for k, v := range r.Headers {
|
||||||
req.Header.Set(k, v)
|
req.Header.Set(k, v)
|
||||||
@ -50,36 +50,36 @@ func API(method string, r *Options, path string, data []byte) (*Request, 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 Req(method, http.StatusInternalServerError, req.URL, nil), 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 Req(method, http.StatusInternalServerError, req.URL, nil), 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 Req(method, resp.StatusCode, resp.Request.URL, body), nil
|
return response(method, resp.StatusCode, resp.Request.URL, body), nil
|
||||||
}
|
}
|
||||||
return Req(method, resp.StatusCode, req.URL, nil), apiError(r.URL, path, resp.StatusCode)
|
return response(method, resp.StatusCode, req.URL, nil), apiError(r.URL, path, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsyncAPI send requests concurrently
|
// AsyncAPI send requests concurrently
|
||||||
func AsyncAPI(method string, r *Options, path string, data []byte, ch chan *Request, chFinished chan bool, chError chan error) {
|
func AsyncAPI(method string, r *Options, path string, data []byte, ch chan *Response, chFinished chan bool, chError chan error) {
|
||||||
resp, err := API(method, r, path, data)
|
resp, err := API(method, r, path, data)
|
||||||
defer func() {
|
defer func() {
|
||||||
chFinished <- true
|
chFinished <- true
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- Req(method, resp.StatusCode, resp.URL, nil)
|
ch <- response(method, resp.StatusCode, resp.URL, nil)
|
||||||
chError <- err
|
chError <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if resp.StatusCode == 200 {
|
if resp.StatusCode == 200 {
|
||||||
ch <- Req(method, resp.StatusCode, resp.URL, resp.Body)
|
ch <- response(method, resp.StatusCode, resp.URL, resp.Body)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch <- Req(method, resp.StatusCode, resp.URL, nil)
|
ch <- response(method, resp.StatusCode, resp.URL, nil)
|
||||||
chError <- apiError(r.URL, path, resp.StatusCode)
|
chError <- apiError(r.URL, path, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user