style: prefer stdlib idioms for errors and http methods
This commit is contained in:
+13
-12
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -62,7 +63,7 @@ func (c *Client) FetchResource(ctx context.Context, kind Kind, id string) (Resou
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload := map[string]interface{}{
|
||||
payload := map[string]any{
|
||||
"nb": 10000,
|
||||
"start": 0,
|
||||
"lang": "en",
|
||||
@@ -78,7 +79,7 @@ func (c *Client) FetchResource(ctx context.Context, kind Kind, id string) (Resou
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://www.deezer.com/ajax/gw-light.php?method=deezer.page%s&input=3&api_version=1.0&api_token=%s", kind.pageMethod(), c.Session.APIToken)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -109,12 +110,12 @@ func (c *Client) FetchResource(ctx context.Context, kind Kind, id string) (Resou
|
||||
{`"DATA_ERROR":"song::getData"`, "invalid track ID"},
|
||||
} {
|
||||
if strings.Contains(bodyStr, check.marker) {
|
||||
return nil, fmt.Errorf("%s", check.errMsg)
|
||||
return nil, errors.New(check.errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(bodyStr, `"results":{}`) {
|
||||
return nil, fmt.Errorf("unexpected response")
|
||||
return nil, errors.New("unexpected response")
|
||||
}
|
||||
|
||||
if err := resource.decode(body); err != nil {
|
||||
@@ -132,7 +133,7 @@ func (c *Client) FetchMedia(ctx context.Context, track *Track, quality string) (
|
||||
}
|
||||
|
||||
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":%s}],"track_tokens":["%s"]}`, c.Session.LicenseToken, qualityFormats[quality], track.TrackToken)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", "https://media.deezer.com/v1/get_url", bytes.NewBuffer([]byte(reqBody)))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://media.deezer.com/v1/get_url", bytes.NewBuffer([]byte(reqBody)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -159,20 +160,20 @@ func (c *Client) FetchMedia(ctx context.Context, track *Track, quality string) (
|
||||
|
||||
if len(media.Errors) > 0 {
|
||||
if media.Errors[0].Code == 1000 {
|
||||
return nil, fmt.Errorf("invalid license token")
|
||||
return nil, errors.New("invalid license token")
|
||||
}
|
||||
return nil, fmt.Errorf("%s", media.Errors[0].Message)
|
||||
return nil, errors.New(media.Errors[0].Message)
|
||||
}
|
||||
|
||||
if len(media.Data) > 0 && len(media.Data[0].Errors) > 0 {
|
||||
if media.Data[0].Errors[0].Code == 2002 {
|
||||
return nil, fmt.Errorf("invalid track token")
|
||||
return nil, errors.New("invalid track token")
|
||||
}
|
||||
return nil, fmt.Errorf("%s", media.Data[0].Errors[0].Message)
|
||||
return nil, errors.New(media.Data[0].Errors[0].Message)
|
||||
}
|
||||
|
||||
if len(media.Data) == 0 || len(media.Data[0].Media) == 0 || len(media.Data[0].Media[0].Sources) == 0 {
|
||||
return nil, fmt.Errorf("no sources found")
|
||||
return nil, errors.New("no sources found")
|
||||
}
|
||||
|
||||
return &media, nil
|
||||
@@ -180,7 +181,7 @@ func (c *Client) FetchMedia(ctx context.Context, track *Track, quality string) (
|
||||
|
||||
func (c *Client) FetchCoverImage(ctx context.Context, track *Track) ([]byte, error) {
|
||||
url := fmt.Sprintf("https://e-cdn-images.dzcdn.net/images/cover/%s/500x500-000000-80-0-0.jpg", track.Cover)
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -199,7 +200,7 @@ func (c *Client) FetchCoverImage(ctx context.Context, track *Track) ([]byte, err
|
||||
}
|
||||
|
||||
func (c *Client) MediaStream(ctx context.Context, media *Media) (io.ReadCloser, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", media.URL(), nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, media.URL(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+12
-11
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/aes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand/v2"
|
||||
@@ -47,7 +48,7 @@ func gatewayEnv() (string, string, error) {
|
||||
apiKey := os.Getenv("DEEZER_MOBILE_API_KEY")
|
||||
gwKey := os.Getenv("DEEZER_MOBILE_GW_KEY")
|
||||
if apiKey == "" || gwKey == "" {
|
||||
return "", "", fmt.Errorf("DEEZER_MOBILE_API_KEY and DEEZER_MOBILE_GW_KEY must be set to use email/password login")
|
||||
return "", "", errors.New("DEEZER_MOBILE_API_KEY and DEEZER_MOBILE_GW_KEY must be set to use email/password login")
|
||||
}
|
||||
|
||||
if len(gwKey) != aes.BlockSize {
|
||||
@@ -89,7 +90,7 @@ func (m *mobileClient) login(ctx context.Context, email, password string) (*Cred
|
||||
}
|
||||
|
||||
func (m *mobileClient) authenticate(ctx context.Context) (string, string, string, error) {
|
||||
body, err := m.gatewayRequest(ctx, "mobile_auth", "GET", "uniq_id", genUniqID(), nil)
|
||||
body, err := m.gatewayRequest(ctx, "mobile_auth", http.MethodGet, "uniq_id", genUniqID(), nil)
|
||||
if err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
@@ -104,10 +105,10 @@ func (m *mobileClient) authenticate(ctx context.Context) (string, string, string
|
||||
}
|
||||
|
||||
if strings.Contains(string(body), "Undefined or invalid API key") {
|
||||
return "", "", "", fmt.Errorf("DEEZER_MOBILE_API_KEY is invalid")
|
||||
return "", "", "", errors.New("DEEZER_MOBILE_API_KEY is invalid")
|
||||
}
|
||||
if strings.Contains(string(body), "GATEWAY_ERROR") || res.Results.Token == "" {
|
||||
return "", "", "", fmt.Errorf("unexpected response from gateway")
|
||||
return "", "", "", errors.New("unexpected response from gateway")
|
||||
}
|
||||
|
||||
encrypted, err := hex.DecodeString(res.Results.Token)
|
||||
@@ -121,7 +122,7 @@ func (m *mobileClient) authenticate(ctx context.Context) (string, string, string
|
||||
}
|
||||
|
||||
if len(decrypted) < 96 {
|
||||
return "", "", "", fmt.Errorf("unexpected response from gateway")
|
||||
return "", "", "", errors.New("unexpected response from gateway")
|
||||
}
|
||||
|
||||
token := string(decrypted[0:64])
|
||||
@@ -138,7 +139,7 @@ func (m *mobileClient) checkToken(ctx context.Context, token, tokenKey string) e
|
||||
}
|
||||
authToken := hex.EncodeToString(encrypted)
|
||||
|
||||
body, err := m.gatewayRequest(ctx, "api_checkToken", "GET", "auth_token", authToken, nil)
|
||||
body, err := m.gatewayRequest(ctx, "api_checkToken", http.MethodGet, "auth_token", authToken, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -150,7 +151,7 @@ func (m *mobileClient) checkToken(ctx context.Context, token, tokenKey string) e
|
||||
return err
|
||||
}
|
||||
if res.Results == "" {
|
||||
return fmt.Errorf("unexpected response from gateway")
|
||||
return errors.New("unexpected response from gateway")
|
||||
}
|
||||
m.sid = res.Results
|
||||
|
||||
@@ -183,13 +184,13 @@ func (m *mobileClient) userAuth(ctx context.Context, email, password, userKey st
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
body, err := m.gatewayRequest(ctx, "mobile_userAuth", "POST", "", "", jsonBody)
|
||||
body, err := m.gatewayRequest(ctx, "mobile_userAuth", http.MethodPost, "", "", jsonBody)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
if strings.Contains(string(body), "USER_AUTH_ERROR") {
|
||||
return "", "", fmt.Errorf("invalid email or password")
|
||||
return "", "", errors.New("invalid email or password")
|
||||
}
|
||||
|
||||
var res struct {
|
||||
@@ -204,7 +205,7 @@ func (m *mobileClient) userAuth(ctx context.Context, email, password, userKey st
|
||||
}
|
||||
|
||||
if res.Results.ARL == "" || res.Results.UserID == 0 {
|
||||
return "", "", fmt.Errorf("unexpected response from gateway")
|
||||
return "", "", errors.New("unexpected response from gateway")
|
||||
}
|
||||
|
||||
return res.Results.ARL, res.Results.BlogName, nil
|
||||
@@ -220,7 +221,7 @@ func (m *mobileClient) gatewayRequest(ctx context.Context, method, httpMethod, p
|
||||
q.Set("method", method)
|
||||
q.Set("api_key", m.apiKey)
|
||||
q.Set("output", "3")
|
||||
if httpMethod == "POST" {
|
||||
if httpMethod == http.MethodPost {
|
||||
q.Set("input", "3")
|
||||
}
|
||||
if m.sid != "" {
|
||||
|
||||
@@ -32,7 +32,7 @@ func resolveARL(ctx context.Context, validate func(ctx context.Context, arl stri
|
||||
return arl, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("run 'godeez login' or export DEEZER_ARL environment variable")
|
||||
return "", errors.New("run 'godeez login' or export DEEZER_ARL environment variable")
|
||||
}
|
||||
|
||||
func Login(ctx context.Context, email, password string) (string, string, error) {
|
||||
|
||||
@@ -31,7 +31,7 @@ func authenticate(ctx context.Context, arlCookie string) (*Session, error) {
|
||||
}
|
||||
|
||||
url := "https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData&input=3&api_version=1.0&api_token="
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user