Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service): Add Gotify #487

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions service/gotify/gotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package gotify

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"
)

//go:generate mockery --name=gotifyService --output=. --case=underscore --inpackage
type gotifyService interface {
Send(ctx context.Context, subject, message string) error
}

var _ gotifyService = &Gotify{}

// Gotify struct holds necessary data to communicate with Gotify API
type Gotify struct {
httpClient *http.Client
baseURL string
appToken string
}

func New(appToken, baseURL string) *Gotify {
Comment on lines +26 to +27
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment.

g := &Gotify{
httpClient: http.DefaultClient,
baseURL: baseURL,
appToken: appToken,
}

return g
}

type newMessageRequestBody struct {
Title string
Message string
Priority int
}
Comment on lines +37 to +41
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newMessageRequestBody sounds more like a function, let's simplify it a bit.

Suggested change
type newMessageRequestBody struct {
Title string
Message string
Priority int
}
type messageRequestBody struct {
Title string
Message string
Priority int
}


func (g *Gotify) Send(ctx context.Context, subject, message string) error {
Comment on lines +42 to +43
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment.

select {
case <-ctx.Done():
return ctx.Err()
default:
reqBody := &newMessageRequestBody{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reqBody := &newMessageRequestBody{
reqBody := &messageRequestBody{

Title: subject,
Message: message,
Priority: 1,
}

body, err := json.Marshal(reqBody)
if err != nil {
return err
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return err
return errors.Wrap(err, "encode message body")

}

req, err := http.NewRequestWithContext(context.Background(), "POST", fmt.Sprintf("%s/message", g.baseURL), bytes.NewReader(body))
if err != nil {
return err
}
Comment on lines +59 to +62
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
req, err := http.NewRequestWithContext(context.Background(), "POST", fmt.Sprintf("%s/message", g.baseURL), bytes.NewReader(body))
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/message", g.baseURL), bytes.NewReader(body))
if err != nil {
return errors.Wrap(err, "create new request")
}


req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", g.appToken))

Comment on lines +64 to +65
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the functionalities of Gotify too well, but I'm pretty sure that we can safely add this to be more up to standards.

Suggested change
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", g.appToken))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", g.appToken))
req.Header.Set("Content-Type", "application/json; charset=utf-8")

b, err := g.httpClient.Do(req)
b.Body.Close()
if err != nil {
return errors.Wrapf(err, "failed to send message to gotify server")
}
Comment on lines +66 to +70
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more careful with the success of our requests, also prevents potential resource leaks.

Suggested change
b, err := g.httpClient.Do(req)
b.Body.Close()
if err != nil {
return errors.Wrapf(err, "failed to send message to gotify server")
}
resp, err := g.httpClient.Do(req)
if err != nil {
return errors.Wrapf(err, "send request to gotify server")
}
// Read response and verify success
result, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "read response")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("gotify returned status code %d: %s", resp.StatusCode, string(result))
}


return nil
}
}
47 changes: 47 additions & 0 deletions service/gotify/gotify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package gotify

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/require"
)

func TestGotify_New(t *testing.T) {
t.Parallel()

assert := require.New(t)
assert.NotNil(New(
"testingAppToken",
"baseUrl",
))
}

func TestGotify_SendSuccess(t *testing.T) {
t.Parallel()

assert := require.New(t)

mock := newMockGotifyService(t)
mock.On("Send", context.Background(), "testing", "hello world!").Return(nil)

err := mock.Send(context.Background(), "testing", "hello world!")
assert.Nil(err)

mock.AssertCalled(t, "Send", context.Background(), "testing", "hello world!")
}

func TestGotify_SendFailure(t *testing.T) {
t.Parallel()

assert := require.New(t)

mock := newMockGotifyService(t)
mock.On("Send", context.Background(), "testing", "hello world!").Return(errors.New("failed to send message"))

err := mock.Send(context.Background(), "testing", "hello world!")
assert.NotNil(err)

mock.AssertCalled(t, "Send", context.Background(), "testing", "hello world!")
}
43 changes: 43 additions & 0 deletions service/gotify/mock_gotify_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.