|
1 | 1 | package gotwi |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
4 | 6 | "net/http" |
5 | 7 | "os" |
6 | 8 | "time" |
7 | 9 |
|
8 | | - "github.com/michimani/gotwi/api/oauth2" |
9 | 10 | "github.com/michimani/gotwi/types" |
| 11 | + "github.com/michimani/gotwi/types/response" |
10 | 12 | ) |
11 | 13 |
|
12 | | -func NewClient() *types.TwitterClient { |
13 | | - return &types.TwitterClient{ |
| 14 | +const ( |
| 15 | + APIKeyEnvName = "GOTWI_API_KEY" |
| 16 | + APIKeySecretEnvName = "GOTWI_API_KEY_SECRET" |
| 17 | +) |
| 18 | + |
| 19 | +type TwitterClient struct { |
| 20 | + Client *http.Client |
| 21 | + AccessToken string |
| 22 | +} |
| 23 | + |
| 24 | +func NewClient() *TwitterClient { |
| 25 | + return &TwitterClient{ |
14 | 26 | Client: &http.Client{ |
15 | 27 | Timeout: time.Duration(30) * time.Second, |
16 | 28 | }, |
17 | 29 | } |
18 | 30 | } |
19 | 31 |
|
20 | | -func NewAuthorizedClient() (*types.TwitterClient, error) { |
| 32 | +func NewAuthorizedClient() (*TwitterClient, error) { |
21 | 33 | c := NewClient() |
22 | 34 | return Authorize(c) |
23 | 35 | } |
24 | 36 |
|
25 | | -func Authorize(c *types.TwitterClient) (*types.TwitterClient, error) { |
26 | | - apiKey := os.Getenv(types.APIKeyEnvName) |
27 | | - apiKeySecret := os.Getenv(types.APIKeySecretEnvName) |
28 | | - accessToken, err := oauth2.GenerateBearerToken(c, apiKey, apiKeySecret) |
| 37 | +func Authorize(c *TwitterClient) (*TwitterClient, error) { |
| 38 | + apiKey := os.Getenv(APIKeyEnvName) |
| 39 | + apiKeySecret := os.Getenv(APIKeySecretEnvName) |
| 40 | + accessToken, err := GenerateBearerToken(c, apiKey, apiKeySecret) |
29 | 41 | if err != nil { |
30 | 42 | return nil, err |
31 | 43 | } |
32 | 44 |
|
33 | 45 | c.AccessToken = accessToken |
34 | 46 | return c, nil |
35 | 47 | } |
| 48 | + |
| 49 | +func (c *TwitterClient) IsReady() bool { |
| 50 | + if c == nil { |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + if c.AccessToken == "" { |
| 55 | + return false |
| 56 | + } |
| 57 | + |
| 58 | + return true |
| 59 | +} |
| 60 | + |
| 61 | +func (c *TwitterClient) Exec(req *http.Request) (*response.ClientResponse, error) { |
| 62 | + res, err := c.Client.Do(req) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + defer res.Body.Close() |
| 67 | + |
| 68 | + bytes, err := ioutil.ReadAll(res.Body) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + return &response.ClientResponse{ |
| 74 | + StatusCode: res.StatusCode, |
| 75 | + Status: res.Status, |
| 76 | + Body: bytes, |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (c *TwitterClient) Prepare(endpointBase, method string, p types.Parameters) (*http.Request, error) { |
| 81 | + if p == nil { |
| 82 | + return nil, fmt.Errorf(types.ErrorParametersNil, endpointBase) |
| 83 | + } |
| 84 | + |
| 85 | + if !c.IsReady() { |
| 86 | + return nil, fmt.Errorf(types.ErrorClientNotReady) |
| 87 | + } |
| 88 | + |
| 89 | + endpoint := p.ResolveEndpoint(endpointBase) |
| 90 | + p.SetAccessToken(c.AccessToken) |
| 91 | + return newRequest(endpoint, method, p) |
| 92 | +} |
| 93 | + |
| 94 | +func newRequest(endpoint, method string, p types.Parameters) (*http.Request, error) { |
| 95 | + req, err := http.NewRequest(method, endpoint, p.Body()) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + req.Header.Set("Content-Type", "application/json;charset=UTF-8") |
| 101 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", p.AccessToken())) |
| 102 | + |
| 103 | + return req, nil |
| 104 | +} |
0 commit comments