From 0bd550a30e74e0b68baa853819b33a2cf18e7a8c Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Mon, 4 Jan 2021 21:05:55 -0700 Subject: [PATCH] switch to DoJSON --- plugins/dmr.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/plugins/dmr.go b/plugins/dmr.go index c0565a1..1f8430b 100644 --- a/plugins/dmr.go +++ b/plugins/dmr.go @@ -1,13 +1,11 @@ package plugins import ( - "encoding/json" "fmt" - "io/ioutil" - "net/http" "net/url" "regexp" "strings" + "time" "github.com/matrix-org/gomatrix" ) @@ -101,28 +99,21 @@ func (p *DMR) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string u := fmt.Sprintf(endpoint, mode, params.Encode()) - fmt.Println(u) - - resp, err := http.Get(u) - if err != nil { - _ = SendText(c, ev.RoomID, fmt.Sprintf("Can't search radioid.net: %s", err)) - return err - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err + var req = HTTPRequest{ + Timeout: 10 * time.Second, + URL: u, + Method: "GET", } switch mode { case "repeater": var res = &DMRRepeater{} - err = json.Unmarshal(body, res) + req.ResBody = res + err := req.DoJSON() if err != nil { return err } + if res.Count == 0 { return SendMD(c, ev.RoomID, fmt.Sprintf("nothing found for '%s'", params.Encode())) } @@ -137,10 +128,12 @@ func (p *DMR) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string case "user": var res = &DMRUser{} - err = json.Unmarshal(body, res) + req.ResBody = res + err := req.DoJSON() if err != nil { return err } + if res.Count == 0 { return SendMD(c, ev.RoomID, fmt.Sprintf("nothing found for '%s'", params.Encode())) }