This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
/
client.go
67 lines (55 loc) · 1.41 KB
/
client.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
package twitter
import (
"fmt"
"github.com/mrjones/oauth"
"net/http"
)
var (
twitterApiUrl = "https://api.twitter.com"
requestTokenPath = "/oauth/request_token"
authorizeTokenPath = "/oauth/authorize"
accessTokenPath = "/oauth/access_token"
)
type Client struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
cons *oauth.Consumer
token *oauth.AccessToken
}
func (c *Client) get(requestUrl string, params map[string]string) (*http.Response, error) {
return c.consumer().Get(requestUrl, params, c.accessToken())
}
func (c *Client) post(requestUrl string, params map[string]string) (*http.Response, error) {
return c.consumer().Post(requestUrl, params, c.accessToken())
}
func (c *Client) consumer() *oauth.Consumer {
if c.cons != nil {
return c.cons
}
c.cons = oauth.NewConsumer(
c.ConsumerKey,
c.ConsumerSecret,
oauth.ServiceProvider{
RequestTokenUrl: c.apiUrl(requestTokenPath),
AuthorizeTokenUrl: c.apiUrl(authorizeTokenPath),
AccessTokenUrl: c.apiUrl(accessTokenPath),
},
)
return c.cons
}
func (c *Client) accessToken() *oauth.AccessToken {
if c.token != nil {
return c.token
}
c.token = &oauth.AccessToken{
Token: c.AccessToken,
Secret: c.AccessTokenSecret,
}
return c.token
}
func (c *Client) apiUrl(format string, a ...interface{}) string {
apiPath := fmt.Sprintf(format, a...)
return twitterApiUrl + apiPath
}