From ddc0166f090aa29b8a32a16e147857684173c5a9 Mon Sep 17 00:00:00 2001 From: anthony Date: Sun, 17 Dec 2023 20:40:07 -0500 Subject: [PATCH] task/remove-slog --- go.mod | 3 +-- go.sum | 2 -- topic_publisher.go | 31 ++----------------------------- 3 files changed, 3 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index cb62754..a848921 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 6fa0326..57c201b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/topic_publisher.go b/topic_publisher.go index f208272..0fd126c 100644 --- a/topic_publisher.go +++ b/topic_publisher.go @@ -7,11 +7,8 @@ import ( "errors" "fmt" "io" - "math" "net/http" "net/url" - - "log/slog" ) var ( @@ -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 } @@ -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 }