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

feat(service): Add Gotify #487

wants to merge 3 commits into from

Conversation

heyztb
Copy link

@heyztb heyztb commented Dec 12, 2022

Description

Adds support for Gotify.

Motivation and Context

Closes #455

How Has This Been Tested?

Unit tests are included.

Screenshots / Output (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (no code change)
  • Refactor (refactoring production code)
  • Other

Checklist:

  • My code follows the code style of this project.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link
Owner

@nikoksr nikoksr left a comment

Choose a reason for hiding this comment

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

Hi @heyztb, we appreciate your contribution! Can't wait to get your changes into Notify.

Overall I'm happy with your PR, there's just some minor oversights I'd like you to fix. Also, please add a doc.go (example) and a README.md (example).

We're basically ready to merge as soon as you get these fixed! Thank you so much for your efforts.

Comment on lines +26 to +27

func New(appToken, baseURL string) *Gotify {
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.

Comment on lines +37 to +41
type newMessageRequestBody struct {
Title string
Message string
Priority int
}
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
}

Comment on lines +42 to +43

func (g *Gotify) Send(ctx context.Context, subject, message string) error {
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.

Comment on lines +59 to +62
req, err := http.NewRequestWithContext(context.Background(), "POST", fmt.Sprintf("%s/message", g.baseURL), bytes.NewReader(body))
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
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")
}


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")

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{

Comment on lines +64 to +65
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", g.appToken))

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")

Comment on lines +66 to +70
b, err := g.httpClient.Do(req)
b.Body.Close()
if err != nil {
return errors.Wrapf(err, "failed to send message to gotify server")
}
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))
}

@nikoksr nikoksr changed the title Gotify feat(service): Add Gotify Dec 19, 2022
@heyztb
Copy link
Author

heyztb commented Dec 19, 2022

Hi @nikoksr

I'll push changes as per your suggestions once I have a bit of time sometime this week. Thanks!

@nikoksr nikoksr closed this Jul 13, 2023
@nikoksr
Copy link
Owner

nikoksr commented Jul 13, 2023

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(service): Add Gotify service
2 participants