switch to DoJSON

This commit is contained in:
Aaron Bieber 2021-01-04 21:05:55 -07:00
parent 26647bc235
commit 0bd550a30e

View File

@ -1,13 +1,11 @@
package plugins package plugins
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/matrix-org/gomatrix" "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()) u := fmt.Sprintf(endpoint, mode, params.Encode())
fmt.Println(u) var req = HTTPRequest{
Timeout: 10 * time.Second,
resp, err := http.Get(u) URL: u,
if err != nil { Method: "GET",
_ = 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
} }
switch mode { switch mode {
case "repeater": case "repeater":
var res = &DMRRepeater{} var res = &DMRRepeater{}
err = json.Unmarshal(body, res) req.ResBody = res
err := req.DoJSON()
if err != nil { if err != nil {
return err return err
} }
if res.Count == 0 { if res.Count == 0 {
return SendMD(c, ev.RoomID, fmt.Sprintf("nothing found for '%s'", params.Encode())) 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": case "user":
var res = &DMRUser{} var res = &DMRUser{}
err = json.Unmarshal(body, res) req.ResBody = res
err := req.DoJSON()
if err != nil { if err != nil {
return err return err
} }
if res.Count == 0 { if res.Count == 0 {
return SendMD(c, ev.RoomID, fmt.Sprintf("nothing found for '%s'", params.Encode())) return SendMD(c, ev.RoomID, fmt.Sprintf("nothing found for '%s'", params.Encode()))
} }