Skip to content

Commit

Permalink
fix: better API status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Feb 9, 2024
1 parent efee5dc commit bdba4a1
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions cmd/daemon/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,69 +270,69 @@ func (s *ApiServer) serve() {
})
m.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeStatus}, w)
})
m.HandleFunc("/player/play", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

var data ApiRequestDataPlay
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypePlay, Data: data}, w)
})
m.HandleFunc("/player/resume", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeResume}, w)
})
m.HandleFunc("/player/pause", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypePause}, w)
})
m.HandleFunc("/player/next", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeNext}, w)
})
m.HandleFunc("/player/prev", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypePrev}, w)
})
m.HandleFunc("/player/seek", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

var data struct {
Position int64 `json:"position"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
return
}

Expand All @@ -346,74 +346,78 @@ func (s *ApiServer) serve() {
Volume uint32 `json:"volume"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetVolume, Data: data.Volume}, w)
} else {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
m.HandleFunc("/player/repeat_context", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var data struct {
Repeat bool `json:"repeat_context"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetRepeatingContext, Data: data.Repeat}, w)
} else {
var data struct {
Repeat bool `json:"repeat_context"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetRepeatingContext, Data: data.Repeat}, w)
})
m.HandleFunc("/player/repeat_track", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var data struct {
Repeat bool `json:"repeat_track"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetRepeatingTrack, Data: data.Repeat}, w)
} else {
var data struct {
Repeat bool `json:"repeat_track"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetRepeatingTrack, Data: data.Repeat}, w)
})
m.HandleFunc("/player/shuffle_context", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var data struct {
Shuffle bool `json:"shuffle_context"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetShufflingContext, Data: data.Shuffle}, w)
} else {
var data struct {
Shuffle bool `json:"shuffle_context"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeSetShufflingContext, Data: data.Shuffle}, w)
})
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
}
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeAddToQueue, Data: data.Uri}, w)
} else {
var data struct {
Uri string `json:"uri"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

s.handleRequest(ApiRequest{Type: ApiRequestTypeAddToQueue, Data: data.Uri}, w)
})
m.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
Expand Down

0 comments on commit bdba4a1

Please sign in to comment.