Skip to content

Commit b8ba6c0

Browse files
committed
Change notify interface
1 parent cc72f9c commit b8ba6c0

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

example/notify/main.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ import (
99
func main() {
1010
token := "" // EDIT THIS
1111

12-
c := linenotify.New(linenotify.WithToken(token))
13-
c.Notify("hello world", "", "", nil)
14-
15-
c = linenotify.New()
16-
c.SetToken(token)
17-
c.Notify("hello world", "http://localhost/thumb.jpg", "http://localhost/full.jpg", nil)
18-
c.Notify("hello world", "", "", bytes.NewReader([]byte("image bytes")))
12+
c := linenotify.New()
13+
c.Notify(token, "hello world", "", "", nil)
14+
c.Notify(token, "hello world", "http://localhost/thumb.jpg", "http://localhost/full.jpg", nil)
15+
c.Notify(token, "hello world", "", "", bytes.NewReader([]byte("image bytes")))
1916
}

notify.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,65 +15,47 @@ import (
1515

1616
type Client struct {
1717
HTTPClient *http.Client
18-
token string
1918
}
2019

21-
type ClientOption func(*Client)
22-
2320
var (
2421
ErrNotifyInvalidAccessToken = errors.New("Invalid access token.")
2522
)
2623

27-
// https://notify-bot.line.me/doc/ja/
28-
func New(options ...ClientOption) *Client {
29-
c := &Client{HTTPClient: http.DefaultClient}
30-
31-
for _, opt := range options {
32-
opt(c)
33-
}
34-
return c
35-
}
36-
37-
func WithToken(token string) ClientOption {
38-
return func(c *Client) {
39-
c.token = token
40-
}
41-
}
42-
43-
func (c *Client) SetToken(token string) {
44-
c.token = token
24+
// https://notify-bot.line.me/doc/
25+
func New() *Client {
26+
return &Client{HTTPClient: http.DefaultClient}
4527
}
4628

47-
func (c *Client) Notify(message, imageThumbnail, imageFullsize string, image io.Reader) error {
29+
func (c *Client) Notify(token, message, imageThumbnail, imageFullsize string, image io.Reader) error {
4830
if image != nil {
49-
return c.NotifyWithImage(message, image)
31+
return c.NotifyWithImage(token, message, image)
5032
}
51-
return c.NotifyWithImageURL(message, imageThumbnail, imageFullsize)
33+
return c.NotifyWithImageURL(token, message, imageThumbnail, imageFullsize)
5234
}
5335

54-
func (c *Client) NotifyWithImage(message string, image io.Reader) error {
36+
func (c *Client) NotifyWithImage(token, message string, image io.Reader) error {
5537
body, contentType, err := c.requestBodyWithImage(message, image)
5638
if err != nil {
5739
return err
5840
}
59-
return c.notify(message, body, contentType)
41+
return c.notify(token, message, body, contentType)
6042
}
6143

62-
func (c *Client) NotifyWithImageURL(message, imageThumbnail, imageFullsize string) error {
44+
func (c *Client) NotifyWithImageURL(token, message, imageThumbnail, imageFullsize string) error {
6345
body, contentType, err := c.requestBody(message, imageThumbnail, imageFullsize)
6446
if err != nil {
6547
return err
6648
}
67-
return c.notify(message, body, contentType)
49+
return c.notify(token, message, body, contentType)
6850
}
6951

70-
func (c *Client) notify(message string, body io.Reader, contentType string) error {
52+
func (c *Client) notify(token, message string, body io.Reader, contentType string) error {
7153
req, err := http.NewRequest("POST", "https://notify-api.line.me/api/notify", body)
7254
if err != nil {
7355
return err
7456
}
7557
req.Header.Set("Content-Type", contentType)
76-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
58+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
7759

7860
resp, err := c.HTTPClient.Do(req)
7961
if err != nil {

notify_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (t *notifyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error
2222
}
2323

2424
func TestClient_Notify(t *testing.T) {
25-
c := New(WithToken(""))
25+
c := New()
2626
body := ioutil.NopCloser(strings.NewReader(""))
2727
tests := []struct {
2828
resp *http.Response
@@ -41,15 +41,15 @@ func TestClient_Notify(t *testing.T) {
4141
for _, test := range tests {
4242
c.HTTPClient.Transport = &notifyRoundTripper{resp: test.resp}
4343

44-
err := c.Notify("test", test.imageThumbnail, test.imageFullsize, test.image)
44+
err := c.Notify("token", "test", test.imageThumbnail, test.imageFullsize, test.image)
4545
if err != test.expectedErr {
4646
t.Errorf("%v err:%v", test.explain, err)
4747
}
4848
}
4949
}
5050

5151
func TestClient_requestBodyWithImage(t *testing.T) {
52-
c := New(WithToken(""))
52+
c := New()
5353

5454
c.HTTPClient.Transport = &notifyRoundTripper{
5555
resp: &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(""))},

0 commit comments

Comments
 (0)