From efee5dcb4a198e323ef7abce665eb6cdc1453e3a Mon Sep 17 00:00:00 2001 From: devgianlu Date: Fri, 9 Feb 2024 14:05:30 +0100 Subject: [PATCH] feat: support adding track to queue from API --- api-spec.yml | 20 ++++++++++++++++++-- cmd/daemon/api_server.go | 16 ++++++++++++++++ cmd/daemon/player.go | 3 +++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/api-spec.yml b/api-spec.yml index 1d7f377..b0707a3 100644 --- a/api-spec.yml +++ b/api-spec.yml @@ -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 @@ -236,4 +236,20 @@ paths: required: true responses: 200: - description: Successful response \ No newline at end of file + 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 diff --git a/cmd/daemon/api_server.go b/cmd/daemon/api_server.go index 543185b..581e64c 100644 --- a/cmd/daemon/api_server.go +++ b/cmd/daemon/api_server.go @@ -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 @@ -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 { diff --git a/cmd/daemon/player.go b/cmd/daemon/player.go index d8ca4f2..ef8bdb8 100644 --- a/cmd/daemon/player.go +++ b/cmd/daemon/player.go @@ -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) }