rate-current CLI option (#847)
This commit is contained in:
@@ -27,6 +27,7 @@ const (
|
||||
VolumeAdjustPath = "/volume/adjust" // ?pct=<+/- percentage>
|
||||
ShowPath = "/window/show"
|
||||
QuitPath = "/window/quit"
|
||||
RateCurrentTrackPath = "/current_track/rate" // ?r=<rating 0-5>
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
@@ -76,3 +77,7 @@ func BuildSearchTrackPath(search string) string {
|
||||
s := url.QueryEscape(search)
|
||||
return fmt.Sprintf("%s?s=%s", SearchTrackPath, s)
|
||||
}
|
||||
|
||||
func BuildRateCurrentTrackPath(rating int) string {
|
||||
return fmt.Sprintf("%s?r=%d", RateCurrentTrackPath, rating)
|
||||
}
|
||||
|
||||
@@ -118,6 +118,11 @@ func (c *Client) AdjustVolumePct(pct float64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) RateCurrentTrack(rating int) error {
|
||||
_, err := c.sendRequest(BuildRateCurrentTrackPath(rating))
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) Show() error {
|
||||
_, err := c.sendRequest(ShowPath)
|
||||
return err
|
||||
|
||||
+18
-2
@@ -43,13 +43,14 @@ type ServerManager interface {
|
||||
type serverImpl struct {
|
||||
server *http.Server
|
||||
pbHandler PlaybackHandler
|
||||
rateFn func(int)
|
||||
sm ServerManager
|
||||
showFn func()
|
||||
quitFn func()
|
||||
}
|
||||
|
||||
func NewServer(pbHandler PlaybackHandler, sm ServerManager, showFn, quitFn func()) IPCServer {
|
||||
s := &serverImpl{pbHandler: pbHandler, sm: sm, showFn: showFn, quitFn: quitFn}
|
||||
func NewServer(pbHandler PlaybackHandler, rateFn func(int), sm ServerManager, showFn, quitFn func()) IPCServer {
|
||||
s := &serverImpl{pbHandler: pbHandler, rateFn: rateFn, sm: sm, showFn: showFn, quitFn: quitFn}
|
||||
s.server = &http.Server{
|
||||
Handler: s.createHandler(),
|
||||
}
|
||||
@@ -172,6 +173,21 @@ func (s *serverImpl) createHandler() http.Handler {
|
||||
|
||||
return tracks, nil
|
||||
}))
|
||||
m.HandleFunc(RateCurrentTrackPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
v := r.URL.Query().Get("r")
|
||||
if rating, err := strconv.Atoi(v); err == nil {
|
||||
// convert to 0-5 range if needed
|
||||
if rating > 5 {
|
||||
rating = 5
|
||||
} else if rating < 0 {
|
||||
rating = 0
|
||||
}
|
||||
s.rateFn(rating)
|
||||
s.writeOK(w)
|
||||
} else {
|
||||
s.writeErr(w, err)
|
||||
}
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user