remove dependency on common from mediabrowser

This commit is contained in:
Harvey Tindall 2021-03-29 20:57:13 +01:00
parent 40808bdcb9
commit 1cf8d3037b
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
6 changed files with 27 additions and 20 deletions

View File

@ -5,7 +5,7 @@ import (
"log" "log"
) )
// TimeoutHandler recovers from an http timeout. // TimeoutHandler recovers from an http timeout or panic.
type TimeoutHandler func() type TimeoutHandler func()
// NewTimeoutHandler returns a new Timeout handler. // NewTimeoutHandler returns a new Timeout handler.

View File

@ -421,10 +421,10 @@ func start(asDaemon, firstCall bool) {
server := app.config.Section("jellyfin").Key("server").String() server := app.config.Section("jellyfin").Key("server").String()
cacheTimeout := int(app.config.Section("jellyfin").Key("cache_timeout").MustUint(30)) cacheTimeout := int(app.config.Section("jellyfin").Key("cache_timeout").MustUint(30))
stringServerType := app.config.Section("jellyfin").Key("type").String() stringServerType := app.config.Section("jellyfin").Key("type").String()
timeoutHandler := common.NewTimeoutHandler("Jellyfin", server, true) timeoutHandler := mediabrowser.NewNamedTimeoutHandler("Jellyfin", server, true)
if stringServerType == "emby" { if stringServerType == "emby" {
serverType = mediabrowser.EmbyServer serverType = mediabrowser.EmbyServer
timeoutHandler = common.NewTimeoutHandler("Emby", server, true) timeoutHandler = mediabrowser.NewNamedTimeoutHandler("Emby", server, true)
app.info.Println("Using Emby server type") app.info.Println("Using Emby server type")
fmt.Println(warning("WARNING: Emby compatibility is experimental, and support is limited.\nPassword resets are not available.")) fmt.Println(warning("WARNING: Emby compatibility is experimental, and support is limited.\nPassword resets are not available."))
} else { } else {

View File

@ -1,7 +1,3 @@
module github.com/hrfee/jfa-go/mediabrowser module github.com/hrfee/jfa-go/mediabrowser
go 1.15 go 1.15
replace github.com/hrfee/jfa-go/common => ../common
require github.com/hrfee/jfa-go/common v0.0.0-20210105184019-fdc97b4e86cc

View File

@ -10,13 +10,32 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"github.com/hrfee/jfa-go/common"
) )
// TimeoutHandler should recover from an http timeout or panic.
type TimeoutHandler func()
// NewNamedTimeoutHandler returns a new Timeout handler that logs the error.
// name is the name of the server to use in the log (e.g Jellyfin/Emby)
// addr is the address of the server being accessed
// if noFail is false, the program will exit on a timeout.
func NewNamedTimeoutHandler(name, addr string, noFail bool) TimeoutHandler {
return func() {
if r := recover(); r != nil {
out := fmt.Sprintf("Failed to authenticate with %s @ %s: Timed out", name, addr)
if noFail {
log.Print(out)
} else {
log.Fatalf(out)
}
}
}
}
type serverType int type serverType int
const ( const (
@ -57,11 +76,11 @@ type MediaBrowser struct {
noFail bool noFail bool
Hyphens bool Hyphens bool
serverType serverType serverType serverType
timeoutHandler common.TimeoutHandler timeoutHandler TimeoutHandler
} }
// NewServer returns a new Mediabrowser object. // NewServer returns a new Mediabrowser object.
func NewServer(st serverType, server, client, version, device, deviceID string, timeoutHandler common.TimeoutHandler, cacheTimeout int) (*MediaBrowser, error) { func NewServer(st serverType, server, client, version, device, deviceID string, timeoutHandler TimeoutHandler, cacheTimeout int) (*MediaBrowser, error) {
mb := &MediaBrowser{} mb := &MediaBrowser{}
mb.serverType = st mb.serverType = st
mb.Server = server mb.Server = server

View File

@ -14,7 +14,6 @@ type Time struct {
} }
func (t *Time) UnmarshalJSON(b []byte) (err error) { func (t *Time) UnmarshalJSON(b []byte) (err error) {
// str := strings.TrimSuffix(strings.TrimPrefix(string(b), "\""), "\"")
// Trim quotes from beginning and end, and any number of Zs (indicates UTC). // Trim quotes from beginning and end, and any number of Zs (indicates UTC).
for b[0] == '"' { for b[0] == '"' {
b = b[1:] b = b[1:]
@ -31,12 +30,6 @@ func (t *Time) UnmarshalJSON(b []byte) (err error) {
b = b[:i] b = b[:i]
} }
t.Time, err = time.Parse("2006-01-02T15:04:05", string(b)) t.Time, err = time.Parse("2006-01-02T15:04:05", string(b))
// str := string(b) + "Z"
// timeJSON := []byte("{ \"parseme\": \"" + str + "\" }")
// var parsed magicParse
// // Magically turn it into a time.Time
// err = json.Unmarshal(timeJSON, &parsed)
// t.Time = parsed.Parsed
return return
} }

View File

@ -7,7 +7,6 @@ import (
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/hrfee/jfa-go/common"
"github.com/hrfee/jfa-go/mediabrowser" "github.com/hrfee/jfa-go/mediabrowser"
) )
@ -60,7 +59,7 @@ func (app *appContext) TestJF(gc *gin.Context) {
if req.ServerType == "emby" { if req.ServerType == "emby" {
serverType = mediabrowser.EmbyServer serverType = mediabrowser.EmbyServer
} }
tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", common.NewTimeoutHandler("authJF", req.Server, true), 30) tempjf, _ := mediabrowser.NewServer(serverType, req.Server, "jfa-go-setup", app.version, "auth", "auth", mediabrowser.NewNamedTimeoutHandler("authJF", req.Server, true), 30)
_, status, err := tempjf.Authenticate(req.Username, req.Password) _, status, err := tempjf.Authenticate(req.Username, req.Password)
if !(status == 200 || status == 204) || err != nil { if !(status == 200 || status == 204) || err != nil {
app.info.Printf("Auth failed with code %d (%s)", status, err) app.info.Printf("Auth failed with code %d (%s)", status, err)