append metadata tags after saving tracks
also rewrite requests to monochrome using 2 helper methods
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package monochrome
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type ClientConfig struct {
|
||||
ApiURL string
|
||||
}
|
||||
@@ -13,3 +20,41 @@ func NewClient(config ClientConfig) *Client {
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) makeRequest(method string, path string, body io.Reader, params map[string]string) (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, c.config.ApiURL+path, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||
}
|
||||
|
||||
p := url.Values{}
|
||||
for key, value := range params {
|
||||
p.Add(key, value)
|
||||
}
|
||||
req.URL.RawQuery = p.Encode()
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) do(req *http.Request) ([]byte, error) {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusTooManyRequests {
|
||||
return nil, fmt.Errorf("rate limited")
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return data, fmt.Errorf("got error from api, data is filled with response")
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user