Skip to content

Commit

Permalink
Change notify interface
Browse files Browse the repository at this point in the history
  • Loading branch information
utahta committed Jun 3, 2017
1 parent cc72f9c commit b8ba6c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
11 changes: 4 additions & 7 deletions example/notify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
42 changes: 12 additions & 30 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,15 +41,15 @@ func TestClient_Notify(t *testing.T) {
for _, test := range tests {
c.HTTPClient.Transport = &notifyRoundTripper{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)
}
}
}

func TestClient_requestBodyWithImage(t *testing.T) {
c := New(WithToken(""))
c := New()

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

0 comments on commit b8ba6c0

Please sign in to comment.