Skip to content

Commit

Permalink
rework subsonic.GetResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Aug 9, 2024
1 parent a74a91b commit a1ce50c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions subsonic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package subsonic

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -346,26 +347,29 @@ func (connection *SubsonicConnection) CreatePlaylist(name string) (*SubsonicResp

func (connection *SubsonicConnection) getResponse(caller, requestUrl string) (*SubsonicResponse, error) {
res, err := http.Get(requestUrl)

if err != nil {
return nil, err
return nil, fmt.Errorf("[%s] failed to make GET request: %v", caller, err)
}

if res.Body != nil {
defer res.Body.Close()
} else {
return nil, fmt.Errorf("[%s] response body is nil", caller)
}

responseBody, readErr := io.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("[%s] unexpected status code: %d, status: %s", caller, res.StatusCode, res.Status)
}

responseBody, readErr := io.ReadAll(res.Body)
if readErr != nil {
return nil, err
return nil, fmt.Errorf("[%s] failed to read response body: %v", caller, readErr)
}

var decodedBody responseWrapper
err = json.Unmarshal(responseBody, &decodedBody)

if err != nil {
return nil, err
return nil, fmt.Errorf("[%s] failed to unmarshal response body: %v", caller, err)
}

return &decodedBody.Response, nil
Expand Down

0 comments on commit a1ce50c

Please sign in to comment.