-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauth_request.go
105 lines (93 loc) · 2.83 KB
/
oauth_request.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
package oauthio
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type OAuthRequestObject struct {
AccessToken string `json:"access_token"`
OAuthToken string `json:"oauth_token"`
OAuthSecret string `json:"oauth_token_secret"`
State string `json:"state"`
Provider string `json:"provider"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
ExpireDate int64
Refreshed bool
OAuthdURL string
appKey string
Client *http.Client
}
func (a *OAuthRequestObject) Get(endpoint string) ([]byte, error) {
return a.makeRequest("GET", endpoint, nil)
}
func (a *OAuthRequestObject) Post(endpoint string, body interface{}) ([]byte, error) {
payload, err := json.Marshal(body)
if err != nil {
return nil, err
}
return a.makeRequest("POST", endpoint, bytes.NewReader(payload))
}
func (a *OAuthRequestObject) Put(endpoint string, body interface{}) ([]byte, error) {
payload, err := json.Marshal(body)
if err != nil {
return nil, err
}
return a.makeRequest("PUT", endpoint, bytes.NewReader(payload))
}
func (a *OAuthRequestObject) Del(endpoint string) ([]byte, error) {
return a.makeRequest("DELETE", endpoint, nil)
}
func (a *OAuthRequestObject) Patch(endpoint string, body interface{}) ([]byte, error) {
payload, err := json.Marshal(body)
if err != nil {
return nil, err
}
return a.makeRequest("PATCH", endpoint, bytes.NewReader(payload))
}
func (a *OAuthRequestObject) Me(filters []string) ([]byte, error) {
qs := url.Values{}
qs.Set("filters", strings.Join(filters, ","))
return a.makeMeRequest(qs)
}
func (a *OAuthRequestObject) buildHeaders(r *http.Request) {
headers := url.Values{}
headers.Set("k", a.appKey)
if a.AccessToken == "" {
headers.Set("access_token", a.AccessToken)
} else {
headers.Set("oauth_token", a.OAuthToken)
headers.Set("oauth_token_secret", a.OAuthSecret)
headers.Set("oauthv1", "1")
}
r.Header = http.Header{
"oauthio": []string{headers.Encode()},
}
}
func (a *OAuthRequestObject) makeMeRequest(filters url.Values) ([]byte, error) {
req, _ := http.NewRequest("GET", a.OAuthdURL+"/auth/"+a.Provider+"/me?"+filters.Encode(), nil)
a.buildHeaders(req)
return excuteRequest(a.Client, req)
}
func (a *OAuthRequestObject) makeRequest(method, endpoint string, body io.Reader) ([]byte, error) {
req, _ := http.NewRequest(method, a.OAuthdURL+"/request/"+a.Provider+endpoint, body)
a.buildHeaders(req)
return excuteRequest(a.Client, req)
}
func excuteRequest(c *http.Client, r *http.Request) ([]byte, error) {
response, err := c.Do(r)
if err != nil {
return nil, errors.New("oauth_request.go: Couldn't reach Oauthd")
}
respBody, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, errors.New("oauth_request.go: Couldn't read Oauthd response")
}
return respBody, nil
}