Skip to content

Instantly share code, notes, and snippets.

@faceair
Last active February 21, 2019 05:41
Show Gist options
  • Save faceair/9cdc04c806cf60c56d3613c02231513b to your computer and use it in GitHub Desktop.
Save faceair/9cdc04c806cf60c56d3613c02231513b to your computer and use it in GitHub Desktop.
request.go
package request
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
const DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
func New(client *http.Client, headers map[string]string) *Request {
if client == nil {
jar, _ := cookiejar.New(nil)
client = &http.Client{
Jar: jar,
}
}
if headers == nil {
headers = map[string]string{
"User-Agent": DefaultUserAgent,
}
}
return &Request{
Headers: headers,
client: client,
}
}
type Request struct {
client *http.Client
Headers map[string]string
}
func (r *Request) Do(method, uri string, headers map[string]string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
for key, value := range r.Headers {
req.Header.Set(key, value)
}
for key, value := range headers {
req.Header.Set(key, value)
}
return r.client.Do(req)
}
func (r *Request) readBody(resp *http.Response, err error) (*http.Response, []byte, error) {
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
return resp, respBody, err
}
func (r *Request) Get(uri string, params ...map[string]string) (*http.Response, []byte, error) {
var headers map[string]string
switch len(params) {
case 0:
case 1:
headers = params[0]
default:
panic("too much parameters")
}
return r.readBody(r.Do("GET", uri, headers, nil))
}
func (r *Request) Delete(uri string) (*http.Response, []byte, error) {
return r.readBody(r.Do("DELETE", uri, nil, nil))
}
func (r *Request) Post(uri string, headers map[string]string, body io.Reader) (*http.Response, []byte, error) {
return r.readBody(r.Do("POST", uri, headers, body))
}
func (r *Request) PostJSON(uri string, data interface{}) (*http.Response, []byte, error) {
jsonValue, _ := json.Marshal(data)
return r.readBody(r.Do("POST", uri, map[string]string{
"Content-Type": "application/json; charset=utf-8",
}, bytes.NewBuffer(jsonValue)))
}
func (r *Request) PatchJSON(uri string, data interface{}) (*http.Response, []byte, error) {
jsonValue, _ := json.Marshal(data)
return r.readBody(r.Do("PATCH", uri, map[string]string{
"Content-Type": "application/json; charset=utf-8",
}, bytes.NewBuffer(jsonValue)))
}
func (r *Request) PostForm(uri string, data map[string]string) (*http.Response, []byte, error) {
form := url.Values{}
for key, value := range data {
form.Add(key, value)
}
return r.readBody(r.Do("POST", uri, map[string]string{
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
}, strings.NewReader(form.Encode())))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment