Skip to content

Commit

Permalink
task/remove-slog
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyHewins committed Dec 18, 2023
1 parent 24d52ef commit ddc0166
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 33 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module github.com/AnthonyHewins/gotfy

go 1.19
go 1.21

require github.com/stretchr/testify v1.8.3

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
31 changes: 2 additions & 29 deletions topic_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"

"log/slog"
)

var (
Expand All @@ -21,22 +18,13 @@ var (

// TopicPublisher creates messages for topics
type TopicPublisher struct {
logger *slog.Logger

server *url.URL
httpClient *http.Client
}

// NewTopicPublisher creates a topic publisher for the specified server URL,
// and uses the supplied HTTP client to resolve the request. Uses the golang
// slog package to log to; if you want to skip all logs supply slog.Logger{}
// with a blank handler, and the publisher will do a no-op
func NewTopicPublisher(slogger *slog.Logger, server *url.URL, httpClient *http.Client) (*TopicPublisher, error) {
if slogger == nil {
// if no logger is passed, ignore absolutely everything
slogger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{Level: slog.Level(math.MaxInt)}))
}

// and uses the supplied HTTP client to resolve the request
func NewTopicPublisher(server *url.URL, httpClient *http.Client) (*TopicPublisher, error) {
if server == nil {
return nil, ErrNoServer
}
Expand All @@ -48,54 +36,39 @@ func NewTopicPublisher(slogger *slog.Logger, server *url.URL, httpClient *http.C
return &TopicPublisher{
server: server,
httpClient: httpClient,
logger: slogger,
}, nil
}

func (t *TopicPublisher) SendMessage(ctx context.Context, m *Message) (*PublishResp, error) {
l := t.logger.With("message", m)

l.DebugContext(ctx, "marshaling NTFY message")
buf, err := json.Marshal(m)
if err != nil {
l.ErrorContext(ctx, "failed marshal", "err", err)
return nil, err
}

l.DebugContext(ctx, "finished marshal, creating request struct", "server", t.server)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.server.String(), bytes.NewReader(buf))
if err != nil {
l.ErrorContext(ctx, "failed creating HTTP request", "err", err)
return nil, err
}

l.DebugContext(ctx, "finished creation of request struct, prepping HTTP call", "req", req)
resp, err := t.httpClient.Do(req)
if err != nil {
l.ErrorContext(ctx, "failed HTTP call", "http client", t.httpClient, "req", req, "err", err)
return nil, err
}

code := resp.StatusCode
l.DebugContext(ctx, "finished HTTP call, reading response body", "status code", code)
buf, err = io.ReadAll(resp.Body)
if err != nil {
l.ErrorContext(ctx, "failed reading response body", "status code", code, "err", err)
return nil, err
}

if s := resp.StatusCode; s < 200 || s >= 300 {
l.ErrorContext(ctx, "bad HTTP response code from server", "response body", string(buf), "status code", code)
return nil, fmt.Errorf("bad http response from server: %d", code)
}

l.DebugContext(ctx, "unmarshaling response body")
var pubResp PublishResp
if err = json.Unmarshal(buf, &pubResp); err != nil {
l.ErrorContext(ctx, "failed unmarshaling response body", "response body", string(buf), "status code", code)
return nil, err
}

l.DebugContext(ctx, "finished unmarshal")
return &pubResp, nil
}

0 comments on commit ddc0166

Please sign in to comment.