Skip to content

Commit

Permalink
fix unmarshalling and update providers
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspawn committed Nov 12, 2024
1 parent 3173d4b commit 0adad3a
Show file tree
Hide file tree
Showing 3 changed files with 390 additions and 4 deletions.
3 changes: 1 addition & 2 deletions api_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package odesli
type APIProvider string

const (
APIProviderAmazon APIProvider = "amazon"
APIProviderAmazonMusic APIProvider = "amazonMusic"
APIProviderAmazonStore APIProvider = "amazonStore"
APIProviderAnghami APIProvider = "anghami"
APIProviderAppleMusic APIProvider = "appleMusic"
APIProviderAudiomack APIProvider = "audiomack"
APIProviderAudius APIProvider = "audius"
APIProviderBandcamp APIProvider = "bandcamp"
APIProviderBoomplay APIProvider = "boomplay"
APIProviderDeezer APIProvider = "deezer"
APIProviderGoogle APIProvider = "google"
APIProviderGoogleStore APIProvider = "googleStore"
APIProviderItunes APIProvider = "itunes"
APIProviderNapster APIProvider = "napster"
APIProviderPandora APIProvider = "pandora"
Expand Down
52 changes: 50 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"time"
)

Expand Down Expand Up @@ -117,6 +120,37 @@ type Entity struct {
Platforms []Platform `json:"platforms,omitempty"`
}

func (e *Entity) UnmarshalJSON(data []byte) error {
type Alias Entity
aux := &struct {
Id interface{} `json:"id"` // unmarshal as string or number as priority field for id
*Alias
}{
Alias: (*Alias)(e),
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

switch id := aux.Id.(type) {
case string:
e.Id = id
case int:
e.Id = strconv.Itoa(id)
case int64:
e.Id = strconv.FormatInt(id, 10)
case float32:
e.Id = strconv.FormatFloat(float64(id), 'f', -1, 64)
case float64:
e.Id = strconv.FormatFloat(id, 'f', -1, 64)
default:
return fmt.Errorf("unexpected type for id: %T", id)
}

return nil
}

type LinkByPlatform struct {

// The unique ID for this entity. Use it to look up data about this entity
Expand All @@ -141,10 +175,12 @@ type API interface {

type ClientOption struct {
APIToken string
Debug bool
}

type Client struct {
client *http.Client
debug bool
}

func NewClient(opt ClientOption) (*Client, error) {
Expand All @@ -160,19 +196,24 @@ func NewClient(opt ClientOption) (*Client, error) {
Transport: rt,
Timeout: DefaultHTTPTimeout,
},
debug: opt.Debug,
}, nil
}

func (c *Client) GetLinks(ctx context.Context, r GetLinksRequest) (GetLinksResponse, error) {
path := fmt.Sprint(LinksPath, "?", r.GetURLValues().Encode())
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprint(LinksPath, "?", r.GetURLValues().Encode()),
path,
nil,
)
if err != nil {
return GetLinksResponse{}, err
}
if c.debug {
log.Printf("request: %s", req.URL.String())
}
resp, err := c.client.Do(req)
if err != nil {
return GetLinksResponse{}, err
Expand All @@ -183,7 +224,14 @@ func (c *Client) GetLinks(ctx context.Context, r GetLinksRequest) (GetLinksRespo
return GetLinksResponse{}, err
}
res := GetLinksResponse{}
err = json.NewDecoder(resp.Body).Decode(&res)
body, err := io.ReadAll(resp.Body)
if err != nil {
return GetLinksResponse{}, err
}
if c.debug {
log.Printf("body: %s", string(body))
}
err = json.Unmarshal(body, &res)
if err != nil {
return GetLinksResponse{}, err
}
Expand Down
Loading

0 comments on commit 0adad3a

Please sign in to comment.