mirror of
https://github.com/gesellix/Bose-SoundTouch.git
synced 2026-08-18 16:46:17 +00:00
Refactor hardcoded source provider IDs to use lookup from constants (#125)
Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
co-authored by
Junie
parent
50b694aa08
commit
d5d6585517
@@ -724,8 +724,8 @@ func setupRouter(server *handlers.Server) *chi.Mux {
|
||||
})
|
||||
|
||||
r.Route("/oauth", func(r chi.Router) {
|
||||
r.Post("/device/{deviceID}/music/musicprovider/15/token/cs3", server.HandleBoseSpotifyToken)
|
||||
r.Post("/device/{deviceID}/music/musicprovider/15/token", server.HandleBoseSpotifyLegacyToken)
|
||||
r.Post("/device/{deviceID}/music/musicprovider/{sourceID}/token/cs3", server.HandleBoseToken)
|
||||
r.Post("/device/{deviceID}/music/musicprovider/{sourceID}/token", server.HandleBoseLegacyToken)
|
||||
r.HandleFunc("/*", server.HandleBoseProxy)
|
||||
})
|
||||
|
||||
|
||||
@@ -5,9 +5,33 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"strconv"
|
||||
|
||||
"github.com/gesellix/bose-soundtouch/pkg/service/constants"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// HandleBoseToken handles the Bose-specific token refresh request from the speaker.
|
||||
// POST /oauth/device/{deviceID}/music/musicprovider/{sourceID}/token/cs3
|
||||
func (s *Server) HandleBoseToken(w http.ResponseWriter, r *http.Request) {
|
||||
sourceID := chi.URLParam(r, "sourceID")
|
||||
|
||||
for _, provider := range constants.StaticProviders {
|
||||
if strconv.Itoa(provider.ID) == sourceID && provider.Name == "SPOTIFY" {
|
||||
s.HandleBoseSpotifyToken(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
s.HandleBoseProxy(w, r)
|
||||
}
|
||||
|
||||
// HandleBoseLegacyToken handles the Bose-specific token refresh request (legacy or variant).
|
||||
// POST /oauth/device/{deviceID}/music/musicprovider/{sourceID}/token
|
||||
func (s *Server) HandleBoseLegacyToken(w http.ResponseWriter, r *http.Request) {
|
||||
s.HandleBoseToken(w, r)
|
||||
}
|
||||
|
||||
// HandleBoseSpotifyToken handles the Bose-specific Spotify token refresh request from the speaker.
|
||||
// POST /oauth/device/{deviceID}/music/musicprovider/15/token/cs3
|
||||
func (s *Server) HandleBoseSpotifyToken(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -61,10 +85,3 @@ func (s *Server) HandleBoseSpotifyToken(w http.ResponseWriter, r *http.Request)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleBoseSpotifyLegacyToken handles the Bose-specific Spotify token refresh request (legacy or variant).
|
||||
// POST /oauth/device/{deviceID}/music/musicprovider/15/token
|
||||
func (s *Server) HandleBoseSpotifyLegacyToken(w http.ResponseWriter, r *http.Request) {
|
||||
// Some firmware might use a slightly different path.
|
||||
s.HandleBoseSpotifyToken(w, r)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestHandleBoseSpotifyToken_LocalResponse(t *testing.T) {
|
||||
|
||||
// chi.URLParam works when using chi router
|
||||
r := chi.NewRouter()
|
||||
r.Post("/oauth/device/{deviceID}/music/musicprovider/15/token/cs3", server.HandleBoseSpotifyToken)
|
||||
r.Post("/oauth/device/{deviceID}/music/musicprovider/{sourceID}/token/cs3", server.HandleBoseToken)
|
||||
|
||||
req := httptest.NewRequest("POST", "/oauth/device/DEVICE123/music/musicprovider/15/token/cs3", nil)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -85,7 +85,7 @@ func TestHandleBoseSpotifyToken_FallbackToProxy(t *testing.T) {
|
||||
|
||||
// chi.URLParam works when using chi router
|
||||
r := chi.NewRouter()
|
||||
r.Post("/oauth/device/{deviceID}/music/musicprovider/15/token/cs3", server.HandleBoseSpotifyToken)
|
||||
r.Post("/oauth/device/{deviceID}/music/musicprovider/{sourceID}/token/cs3", server.HandleBoseToken)
|
||||
|
||||
// Since there's no Spotify service, it should fall back to HandleBoseProxy.
|
||||
// HandleBoseProxy will try to contact streaming.bose.com.
|
||||
@@ -113,13 +113,13 @@ func TestHandleBoseSpotifyToken_FallbackToProxy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBoseSpotifyLegacyToken(t *testing.T) {
|
||||
func TestHandleBoseLegacyToken(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
ds := datastore.NewDataStore(tmpDir)
|
||||
server := NewServer(ds, nil, "http://localhost", false, false, false)
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Post("/oauth/device/{deviceID}/music/musicprovider/15/token", server.HandleBoseSpotifyLegacyToken)
|
||||
r.Post("/oauth/device/{deviceID}/music/musicprovider/{sourceID}/token", server.HandleBoseLegacyToken)
|
||||
|
||||
// Since we are not configuring Spotify, it should fall back to proxy
|
||||
req := httptest.NewRequest("POST", "/oauth/device/DEVICE123/music/musicprovider/15/token", nil)
|
||||
|
||||
@@ -487,20 +487,25 @@ func AccountFullToXML(ds *datastore.DataStore, account string) ([]byte, error) {
|
||||
AccountStatus: "OK",
|
||||
Mode: "global",
|
||||
PreferredLanguage: "de",
|
||||
ProviderSettings: []models.ProviderSetting{
|
||||
{
|
||||
}
|
||||
|
||||
for _, p := range constants.StaticProviders {
|
||||
switch p.Name {
|
||||
case "DEEZER":
|
||||
resp.ProviderSettings = append(resp.ProviderSettings, models.ProviderSetting{
|
||||
BoseID: account,
|
||||
KeyName: "ELIGIBLE_FOR_TRIAL",
|
||||
Value: "false",
|
||||
ProviderID: "14",
|
||||
},
|
||||
{
|
||||
ProviderID: strconv.Itoa(p.ID),
|
||||
})
|
||||
case "SPOTIFY":
|
||||
resp.ProviderSettings = append(resp.ProviderSettings, models.ProviderSetting{
|
||||
BoseID: account,
|
||||
KeyName: "STREAMING_QUALITY",
|
||||
Value: "2",
|
||||
ProviderID: "15",
|
||||
},
|
||||
},
|
||||
ProviderID: strconv.Itoa(p.ID),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if info, _ := ds.GetAccountInfo(account); info != nil {
|
||||
|
||||
Reference in New Issue
Block a user