From b8ba6c0ca3a8e80dfad8f6740a865bcc9b7678cc Mon Sep 17 00:00:00 2001 From: utahta Date: Sun, 4 Jun 2017 01:35:28 +0900 Subject: [PATCH] Change notify interface --- example/notify/main.go | 11 ++++------- notify.go | 42 ++++++++++++------------------------------ notify_test.go | 6 +++--- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/example/notify/main.go b/example/notify/main.go index 15a10de..fcca104 100644 --- a/example/notify/main.go +++ b/example/notify/main.go @@ -9,11 +9,8 @@ import ( func main() { token := "" // EDIT THIS - c := linenotify.New(linenotify.WithToken(token)) - c.Notify("hello world", "", "", nil) - - c = linenotify.New() - c.SetToken(token) - c.Notify("hello world", "http://localhost/thumb.jpg", "http://localhost/full.jpg", nil) - c.Notify("hello world", "", "", bytes.NewReader([]byte("image bytes"))) + c := linenotify.New() + c.Notify(token, "hello world", "", "", nil) + c.Notify(token, "hello world", "http://localhost/thumb.jpg", "http://localhost/full.jpg", nil) + c.Notify(token, "hello world", "", "", bytes.NewReader([]byte("image bytes"))) } diff --git a/notify.go b/notify.go index 8f764e4..81282bf 100644 --- a/notify.go +++ b/notify.go @@ -15,65 +15,47 @@ import ( type Client struct { HTTPClient *http.Client - token string } -type ClientOption func(*Client) - var ( ErrNotifyInvalidAccessToken = errors.New("Invalid access token.") ) -// https://notify-bot.line.me/doc/ja/ -func New(options ...ClientOption) *Client { - c := &Client{HTTPClient: http.DefaultClient} - - for _, opt := range options { - opt(c) - } - return c -} - -func WithToken(token string) ClientOption { - return func(c *Client) { - c.token = token - } -} - -func (c *Client) SetToken(token string) { - c.token = token +// https://notify-bot.line.me/doc/ +func New() *Client { + return &Client{HTTPClient: http.DefaultClient} } -func (c *Client) Notify(message, imageThumbnail, imageFullsize string, image io.Reader) error { +func (c *Client) Notify(token, message, imageThumbnail, imageFullsize string, image io.Reader) error { if image != nil { - return c.NotifyWithImage(message, image) + return c.NotifyWithImage(token, message, image) } - return c.NotifyWithImageURL(message, imageThumbnail, imageFullsize) + return c.NotifyWithImageURL(token, message, imageThumbnail, imageFullsize) } -func (c *Client) NotifyWithImage(message string, image io.Reader) error { +func (c *Client) NotifyWithImage(token, message string, image io.Reader) error { body, contentType, err := c.requestBodyWithImage(message, image) if err != nil { return err } - return c.notify(message, body, contentType) + return c.notify(token, message, body, contentType) } -func (c *Client) NotifyWithImageURL(message, imageThumbnail, imageFullsize string) error { +func (c *Client) NotifyWithImageURL(token, message, imageThumbnail, imageFullsize string) error { body, contentType, err := c.requestBody(message, imageThumbnail, imageFullsize) if err != nil { return err } - return c.notify(message, body, contentType) + return c.notify(token, message, body, contentType) } -func (c *Client) notify(message string, body io.Reader, contentType string) error { +func (c *Client) notify(token, message string, body io.Reader, contentType string) error { req, err := http.NewRequest("POST", "https://notify-api.line.me/api/notify", body) if err != nil { return err } req.Header.Set("Content-Type", contentType) - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token)) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) resp, err := c.HTTPClient.Do(req) if err != nil { diff --git a/notify_test.go b/notify_test.go index 2fecdfb..5fe796d 100644 --- a/notify_test.go +++ b/notify_test.go @@ -22,7 +22,7 @@ func (t *notifyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error } func TestClient_Notify(t *testing.T) { - c := New(WithToken("")) + c := New() body := ioutil.NopCloser(strings.NewReader("")) tests := []struct { resp *http.Response @@ -41,7 +41,7 @@ func TestClient_Notify(t *testing.T) { for _, test := range tests { c.HTTPClient.Transport = ¬ifyRoundTripper{resp: test.resp} - err := c.Notify("test", test.imageThumbnail, test.imageFullsize, test.image) + err := c.Notify("token", "test", test.imageThumbnail, test.imageFullsize, test.image) if err != test.expectedErr { t.Errorf("%v err:%v", test.explain, err) } @@ -49,7 +49,7 @@ func TestClient_Notify(t *testing.T) { } func TestClient_requestBodyWithImage(t *testing.T) { - c := New(WithToken("")) + c := New() c.HTTPClient.Transport = ¬ifyRoundTripper{ resp: &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(""))},