Skip to content

Commit

Permalink
feat: support adding track to queue from API
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Feb 9, 2024
1 parent 9feee26 commit efee5dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
20 changes: 18 additions & 2 deletions api-spec.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.0
info:
version: 0.0.8
version: 0.0.13
title: go-librespot API
description: go-librespot daemon API

Expand Down Expand Up @@ -236,4 +236,20 @@ paths:
required: true
responses:
200:
description: Successful response
description: Successful response
/player/add_to_queue:
post:
description: Add a track to the queue
requestBody:
content:
application/json:
schema:
type: object
properties:
uri:
description: The URI for the track that should be added
type: string
required: true
responses:
200:
description: Successful response
16 changes: 16 additions & 0 deletions cmd/daemon/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
ApiRequestTypeSetRepeatingContext ApiRequestType = "repeating_context"
ApiRequestTypeSetRepeatingTrack ApiRequestType = "repeating_track"
ApiRequestTypeSetShufflingContext ApiRequestType = "shuffling_context"
ApiRequestTypeAddToQueue ApiRequestType = "add_to_queue"
)

type ApiEventType string
Expand Down Expand Up @@ -399,6 +400,21 @@ func (s *ApiServer) serve() {
w.WriteHeader(http.StatusBadRequest)
}
})
m.HandleFunc("/player/add_to_queue", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var data struct {
Uri string `json:"uri"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeAddToQueue, Data: data.Uri}, w)
} else {
w.WriteHeader(http.StatusBadRequest)
}
})
m.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/daemon/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ func (p *AppPlayer) handleApiRequest(req ApiRequest) (any, error) {
case ApiRequestTypeSetShufflingContext:
p.setShufflingContext(req.Data.(bool))
return nil, nil
case ApiRequestTypeAddToQueue:
p.addToQueue(&connectpb.ContextTrack{Uri: req.Data.(string)})
return nil, nil
default:
return nil, fmt.Errorf("unknown request type: %s", req.Type)
}
Expand Down

0 comments on commit efee5dc

Please sign in to comment.