improved handling of requests

This commit is contained in:
Michael 2020-01-02 01:02:33 +00:00
parent 1d95dbe6b4
commit ba729525f1

76
api.go
View File

@ -1,32 +1,46 @@
package request package request
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
) )
// Request data // Request API return request
type Request struct { type Request struct {
Method string
StatusCode int
URL *url.URL
Body []byte
}
// Options Request Options
type Options struct {
URL string URL string
Headers []string Headers []string
} }
// HTTPError Status // Req HTTP Request
type HTTPError struct { func Req(method string, statuscode int, url *url.URL, body []byte) *Request {
StatusCode int return &Request{
Error error Method: method,
StatusCode: statuscode,
URL: url,
Body: body,
}
} }
// Get sends GET request to path // Get sends GET request to path
func Get(r *Request, path string) ([]byte, *HTTPError) { func Get(r *Options, path string) (*Request, error) {
req, err := http.NewRequest("GET", r.URL+"/"+path, nil) req, err := http.NewRequest("GET", r.URL+"/"+path, nil)
if err != nil { if err != nil {
return nil, &HTTPError{StatusCode: http.StatusInternalServerError, Error: err} return Req(http.MethodGet, http.StatusInternalServerError, req.URL, nil), err
} }
for i := 0; i < len(r.Headers); i++ { for i := 0; i < len(r.Headers); i++ {
header := strings.Split(r.Headers[i], ",") header := strings.Split(r.Headers[i], ",")
@ -35,30 +49,58 @@ func Get(r *Request, path string) ([]byte, *HTTPError) {
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 nil, &HTTPError{StatusCode: http.StatusInternalServerError, Error: err} return Req(http.MethodGet, 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 nil, &HTTPError{StatusCode: http.StatusInternalServerError, Error: err} return Req(http.MethodGet, http.StatusInternalServerError, req.URL, nil), err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
return body, nil return Req(http.MethodGet, resp.StatusCode, resp.Request.URL, body), nil
} }
e := fmt.Sprintf("api: %s/%s - %d %s", r.URL, path, resp.StatusCode, http.StatusText(resp.StatusCode)) e := fmt.Sprintf("api: %s/%s - %d %s", r.URL, path, resp.StatusCode, http.StatusText(resp.StatusCode))
return nil, &HTTPError{StatusCode: resp.StatusCode, Error: errors.New(e)} return Req(http.MethodGet, http.StatusInternalServerError, req.URL, nil), errors.New(e)
}
// Post sends POST request to path
func Post(r *Options, path string, data []byte) (*Request, error) {
fmt.Println(string(data))
req, err := http.NewRequest("POST", r.URL+"/"+path, bytes.NewBuffer(data))
if err != nil {
return Req(http.MethodPost, http.StatusInternalServerError, req.URL, nil), err
}
for i := 0; i < len(r.Headers); i++ {
header := strings.Split(r.Headers[i], ",")
req.Header.Set(header[0], header[1])
}
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
return Req(http.MethodPost, http.StatusInternalServerError, req.URL, nil), err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
return Req(http.MethodPost, resp.StatusCode, resp.Request.URL, nil), nil
}
e := fmt.Sprintf("api: %s/%s - %d %s", r.URL, path, resp.StatusCode, http.StatusText(resp.StatusCode))
return Req(http.MethodPost, http.StatusInternalServerError, req.URL, nil), errors.New(e)
}
type JSONReq struct {
StatusCode int
Body map[string]interface{}
} }
// JSONParse parses json data // JSONParse parses json data
func JSONParse(r *Request, path string) (map[string]interface{}, *HTTPError) { func JSONParse(r *Options, path string) (*JSONReq, error) {
var result map[string]interface{} var result map[string]interface{}
resp, err := Get(r, path) resp, err := Get(r, path)
if err != nil { if err != nil {
fmt.Println(err.Error) fmt.Println(err)
return nil, err return &JSONReq{StatusCode: resp.StatusCode}, err
} }
json.Unmarshal(resp, &result) json.Unmarshal(resp.Body, &result)
return &JSONReq{StatusCode: resp.StatusCode, Body: result}, nil
return result, nil
} }