package for handling and sending http requests
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
22b01cf9c2
commit
8ea9b384f2
|
@ -0,0 +1,131 @@
|
|||
package request
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DefaultClient = &http.Client{Timeout: time.Second * 10}
|
||||
|
||||
// Response HTTP Response
|
||||
type Response struct {
|
||||
Method string
|
||||
StatusCode int
|
||||
URL *url.URL
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// Request HTTP request
|
||||
type Request struct {
|
||||
Method string
|
||||
URL string
|
||||
Headers map[string]string
|
||||
Body io.Reader
|
||||
}
|
||||
|
||||
// NewRequest initalises new http request
|
||||
func NewRequest(method, url string, headers map[string]string, body io.Reader) (req *Request) {
|
||||
return &Request{
|
||||
Method: method,
|
||||
URL: url,
|
||||
Headers: headers,
|
||||
Body: body,
|
||||
}
|
||||
}
|
||||
|
||||
// Do initiates http request
|
||||
func (request *Request) Do(client *http.Client) (*Response, error) {
|
||||
req, err := http.NewRequest(request.Method, request.URL, request.Body)
|
||||
if err != nil {
|
||||
return &Response{request.Method, http.StatusInternalServerError, req.URL, nil}, err
|
||||
}
|
||||
for k, v := range request.Headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return &Response{request.Method, http.StatusInternalServerError, req.URL, nil}, err
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return &Response{request.Method, http.StatusInternalServerError, req.URL, nil}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return &Response{request.Method, resp.StatusCode, resp.Request.URL, body}, nil
|
||||
}
|
||||
err = fmt.Errorf("request: %s - %d %s", request.URL, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
return &Response{request.Method, resp.StatusCode, resp.Request.URL, nil}, err
|
||||
}
|
||||
|
||||
// Get sends GET request
|
||||
func Get(url string, headers map[string]string) (*Response, error) {
|
||||
resp, err := NewRequest(http.MethodGet, url, headers, nil).Do(DefaultClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Post sends POST request
|
||||
func Post(url string, headers map[string]string, body io.Reader) (*Response, error) {
|
||||
resp, err := NewRequest(http.MethodPost, url, headers, body).Do(DefaultClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Put sends PUT request
|
||||
func Put(url string, headers map[string]string, body io.Reader) (*Response, error) {
|
||||
resp, err := NewRequest(http.MethodPut, url, headers, body).Do(DefaultClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Delete sends DELETE request
|
||||
func Delete(url string, headers map[string]string, body io.Reader) (*Response, error) {
|
||||
resp, err := NewRequest(http.MethodDelete, url, headers, body).Do(DefaultClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// AsyncRequest send requests concurrently
|
||||
func AsyncRequest(method, url string, headers map[string]string, body io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
|
||||
defer func() {
|
||||
chDone <- true
|
||||
}()
|
||||
resp, err := NewRequest(method, url, headers, body).Do(DefaultClient)
|
||||
if err != nil {
|
||||
chErr <- err
|
||||
}
|
||||
ch <- resp
|
||||
}
|
||||
|
||||
// AsyncGet sends GET requests concurrently
|
||||
func AsyncGet(url string, headers map[string]string, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
|
||||
AsyncRequest(http.MethodGet, url, headers, nil, ch, chErr, chDone)
|
||||
}
|
||||
|
||||
// AsyncPost sends POST requests concurrently
|
||||
func AsyncPost(url string, headers map[string]string, body io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
|
||||
AsyncRequest(http.MethodPost, url, headers, body, ch, chErr, chDone)
|
||||
}
|
||||
|
||||
// AsyncPut sends PUT requests concurrently
|
||||
func AsyncPut(url string, headers map[string]string, body io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
|
||||
AsyncRequest(http.MethodPut, url, headers, body, ch, chErr, chDone)
|
||||
}
|
||||
|
||||
// AsyncDelete sends DELETE requests concurrently
|
||||
func AsyncDelete(url string, headers map[string]string, body io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
|
||||
AsyncRequest(http.MethodDelete, url, headers, body, ch, chErr, chDone)
|
||||
}
|
Reference in New Issue