/
client.go
177 lines (159 loc) · 5.21 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2016 LINE Corporation
//
// LINE Corporation licenses this file to you under the Apache License,
// version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package linebot
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"path"
)
// APIEndpoint constants
const (
APIEndpointBase = "https://api.line.me"
APIEndpointPushMessage = "/v2/bot/message/push"
APIEndpointReplyMessage = "/v2/bot/message/reply"
APIEndpointMulticast = "/v2/bot/message/multicast"
APIEndpointGetMessageContent = "/v2/bot/message/%s/content"
APIEndpointLeaveGroup = "/v2/bot/group/%s/leave"
APIEndpointLeaveRoom = "/v2/bot/room/%s/leave"
APIEndpointGetProfile = "/v2/bot/profile/%s"
APIEndpointGetGroupMemberProfile = "/v2/bot/group/%s/member/%s"
APIEndpointGetRoomMemberProfile = "/v2/bot/room/%s/member/%s"
APIEndpointGetGroupMemberIDs = "/v2/bot/group/%s/members/ids"
APIEndpointGetRoomMemberIDs = "/v2/bot/room/%s/members/ids"
APIEndpointCreateRichMenu = "/v2/bot/richmenu"
APIEndpointGetRichMenu = "/v2/bot/richmenu/%s"
APIEndpointListRichMenu = "/v2/bot/richmenu/list"
APIEndpointDeleteRichMenu = "/v2/bot/richmenu/%s"
APIEndpointGetUserRichMenu = "/v2/bot/user/%s/richmenu"
APIEndpointLinkUserRichMenu = "/v2/bot/user/%s/richmenu/%s"
APIEndpointUnlinkUserRichMenu = "/v2/bot/user/%s/richmenu"
APIEndpointDownloadRichMenuImage = "/v2/bot/richmenu/%s/content" // Download: GET / Upload: POST
APIEndpointUploadRichMenuImage = "/v2/bot/richmenu/%s/content" // Download: GET / Upload: POST
APIEndpointGetLIFFAPP = "/liff/v1/apps"
APIEndpointAddLIFFAPP = "/liff/v1/apps"
APIEndpointUpdateLIFFAPP = "/liff/v1/apps/%s/view"
APIEndpointDeleteLIFFAPP = "/liff/v1/apps/%s"
)
// Client type
type Client struct {
channelSecret string
channelToken string
endpointBase *url.URL // default APIEndpointBase
httpClient *http.Client // default http.DefaultClient
}
// ClientOption type
type ClientOption func(*Client) error
// New returns a new bot client instance.
func New(channelSecret, channelToken string, options ...ClientOption) (*Client, error) {
if channelSecret == "" {
return nil, errors.New("missing channel secret")
}
if channelToken == "" {
return nil, errors.New("missing channel access token")
}
c := &Client{
channelSecret: channelSecret,
channelToken: channelToken,
httpClient: http.DefaultClient,
}
for _, option := range options {
err := option(c)
if err != nil {
return nil, err
}
}
if c.endpointBase == nil {
u, err := url.ParseRequestURI(APIEndpointBase)
if err != nil {
return nil, err
}
c.endpointBase = u
}
return c, nil
}
// WithHTTPClient function
func WithHTTPClient(c *http.Client) ClientOption {
return func(client *Client) error {
client.httpClient = c
return nil
}
}
// WithEndpointBase function
func WithEndpointBase(endpointBase string) ClientOption {
return func(client *Client) error {
u, err := url.ParseRequestURI(endpointBase)
if err != nil {
return err
}
client.endpointBase = u
return nil
}
}
func (client *Client) url(endpoint string) string {
u := *client.endpointBase
u.Path = path.Join(u.Path, endpoint)
return u.String()
}
func (client *Client) do(ctx context.Context, req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+client.channelToken)
req.Header.Set("User-Agent", "LINE-BotSDK-Go/"+version)
if ctx != nil {
res, err := client.httpClient.Do(req.WithContext(ctx))
if err != nil {
select {
case <-ctx.Done():
err = ctx.Err()
default:
}
}
return res, err
}
return client.httpClient.Do(req)
}
func (client *Client) get(ctx context.Context, endpoint string, query url.Values) (*http.Response, error) {
req, err := http.NewRequest("GET", client.url(endpoint), nil)
if err != nil {
return nil, err
}
if query != nil {
req.URL.RawQuery = query.Encode()
}
return client.do(ctx, req)
}
func (client *Client) post(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("POST", client.url(endpoint), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
return client.do(ctx, req)
}
func (client *Client) put(ctx context.Context, endpoint string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("PUT", client.url(endpoint), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
return client.do(ctx, req)
}
func (client *Client) delete(ctx context.Context, endpoint string) (*http.Response, error) {
req, err := http.NewRequest("DELETE", client.url(endpoint), nil)
if err != nil {
return nil, err
}
return client.do(ctx, req)
}