-
Notifications
You must be signed in to change notification settings - Fork 3
/
call.go
109 lines (91 loc) · 2.34 KB
/
call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package facebook
import (
"fmt"
"io"
"io/ioutil"
"net/http"
)
type (
// Call is a place where Facebook Graph API
// are being called. This is the HIT-man!
Call struct {
HTTPClient http.Client
HTTPRequest *http.Request
HTTPHeader map[string]string
Method string
URL string
Body io.Reader
}
// CallInterface contains the common method
// to be setup before submitting request to
// facebook graph api
CallInterface interface {
New() CallInterface
SetMethod(method string) CallInterface
SetURL(url string) CallInterface
SetBody(body io.Reader) CallInterface
AddHeader(key string, value string) CallInterface
Submit() (interface{}, error)
}
)
func (c *Call) newClient() http.Client {
client := http.Client{}
// Since there's something wrong with `http.Request`
// when setting up a header for Authorization using Bearer,
// then we need to make it right :)
client.CheckRedirect = func(req *http.Request, reqs []*http.Request) error {
req.Header.Add("Authorization", reqs[0].Header.Get("Authorization"))
fmt.Println(reqs[0].Header.Get("Authorization"))
return nil
}
return client
}
// New is use to initialize the Call methods
func (c *Call) New() CallInterface {
return &Call{
HTTPClient: c.newClient(),
Body: nil,
}
}
// SetMethod is use to set the http method
func (c *Call) SetMethod(method string) CallInterface {
c.Method = method
return c
}
// SetURL is use to set the target URL
func (c *Call) SetURL(url string) CallInterface {
c.URL = url
return c
}
// SetBody is use to set the request body
func (c *Call) SetBody(body io.Reader) CallInterface {
c.Body = body
return c
}
// AddHeader is use to add request header
func (c *Call) AddHeader(key string, value string) CallInterface {
if c.HTTPHeader == nil {
c.HTTPHeader = make(map[string]string)
}
c.HTTPHeader[key] = value
return c
}
// Submit is where the target URL is being called
func (c *Call) Submit() (interface{}, error) {
req, errReq := http.NewRequest(c.Method, c.URL, c.Body)
if errReq != nil {
return nil, errReq
}
for key, val := range c.HTTPHeader {
req.Header.Add(key, val)
}
resp, errResp := c.HTTPClient.Do(req)
if errResp != nil {
return nil, errResp
}
respBytes, errRespBytes := ioutil.ReadAll(resp.Body)
if errRespBytes != nil {
return nil, errRespBytes
}
return string(respBytes), nil
}