Skip to content

Commit

Permalink
feat: add entity not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGorbanev committed Aug 4, 2024
1 parent dfbf7b9 commit b89909c
Show file tree
Hide file tree
Showing 17 changed files with 455 additions and 173 deletions.
25 changes: 13 additions & 12 deletions apple_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package streaminx

import (
"context"
"errors"
"fmt"

"github.com/GeorgeGorbanev/streaminx/internal/apple"
Expand All @@ -25,11 +26,11 @@ func (a *AppleAdapter) FetchTrack(ctx context.Context, id string) (*Entity, erro

track, err := a.client.FetchTrack(ctx, ck.ID, ck.Storefront)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to get track from apple: %w", err)
}
if track == nil {
return nil, nil
}

res, err := a.adaptTrack(track)
if err != nil {
Expand All @@ -41,11 +42,11 @@ func (a *AppleAdapter) FetchTrack(ctx context.Context, id string) (*Entity, erro
func (a *AppleAdapter) SearchTrack(ctx context.Context, artistName, trackName string) (*Entity, error) {
track, err := a.client.SearchTrack(ctx, artistName, trackName)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to search track from apple: %w", err)
}
if track == nil {
return nil, nil
}
res, err := a.adaptTrack(track)
if err != nil {
return nil, err
Expand All @@ -61,11 +62,11 @@ func (a *AppleAdapter) FetchAlbum(ctx context.Context, id string) (*Entity, erro

album, err := a.client.FetchAlbum(ctx, ck.ID, ck.Storefront)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to get album from apple: %w", err)
}
if album == nil {
return nil, nil
}

res, err := a.adaptAlbum(album)
if err != nil {
Expand All @@ -77,11 +78,11 @@ func (a *AppleAdapter) FetchAlbum(ctx context.Context, id string) (*Entity, erro
func (a *AppleAdapter) SearchAlbum(ctx context.Context, artistName, albumName string) (*Entity, error) {
album, err := a.client.SearchAlbum(ctx, artistName, albumName)
if err != nil {
if errors.Is(err, apple.NotFoundError) {
return nil, EntityNotFoundError
}
return nil, fmt.Errorf("failed to search album from apple: %w", err)
}
if album == nil {
return nil, nil
}
res, err := a.adaptAlbum(album)
if err != nil {
return nil, err
Expand Down
68 changes: 54 additions & 14 deletions apple_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,41 @@ type appleClientMock struct {
}

func (c *appleClientMock) FetchTrack(_ context.Context, id, storefront string) (*apple.Entity, error) {
return c.fetchTrack[storefront+"-"+id], nil
track, ok := c.fetchTrack[storefront+"-"+id]
if !ok {
return nil, apple.NotFoundError
}
return track, nil
}

func (c *appleClientMock) SearchTrack(_ context.Context, artistName, trackName string) (*apple.Entity, error) {
if tracks, ok := c.searchTrack[artistName]; ok {
return tracks[trackName], nil
track, ok := tracks[trackName]
if !ok {
return nil, apple.NotFoundError
}
return track, nil
}
return nil, nil
return nil, apple.NotFoundError
}

func (c *appleClientMock) FetchAlbum(_ context.Context, id, storefront string) (*apple.Entity, error) {
return c.fetchAlbum[storefront+"-"+id], nil
album, ok := c.fetchAlbum[storefront+"-"+id]
if !ok {
return nil, apple.NotFoundError
}
return album, nil
}

func (c *appleClientMock) SearchAlbum(_ context.Context, artistName, albumName string) (*apple.Entity, error) {
if albums, ok := c.searchAlbum[artistName]; ok {
return albums[albumName], nil
album, ok := albums[albumName]
if !ok {
return nil, apple.NotFoundError
}
return album, nil
}
return nil, nil
return nil, apple.NotFoundError
}

func TestAppleAdapter_FetchTrack(t *testing.T) {
Expand All @@ -45,6 +61,7 @@ func TestAppleAdapter_FetchTrack(t *testing.T) {
id string
clientMock *appleClientMock
expectedTrack *Entity
expectedErr error
}{
{
name: "found ID",
Expand Down Expand Up @@ -75,6 +92,7 @@ func TestAppleAdapter_FetchTrack(t *testing.T) {
id: "ru-123",
clientMock: &appleClientMock{},
expectedTrack: nil,
expectedErr: EntityNotFoundError,
},
}
for _, tt := range tests {
Expand All @@ -85,8 +103,12 @@ func TestAppleAdapter_FetchTrack(t *testing.T) {
a := newAppleAdapter(tt.clientMock)
result, err := a.FetchTrack(ctx, tt.id)

require.NoError(t, err)
require.Equal(t, tt.expectedTrack, result)
if tt.expectedErr != nil {
require.ErrorIs(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
require.Equal(t, tt.expectedTrack, result)
}
})
}
}
Expand All @@ -98,6 +120,7 @@ func TestAppleAdapter_SearchTrack(t *testing.T) {
searchName string
clientMock *appleClientMock
expectedTrack *Entity
expectedErr error
}{
{
name: "found query",
Expand Down Expand Up @@ -132,6 +155,7 @@ func TestAppleAdapter_SearchTrack(t *testing.T) {
searchName: "not found name",
clientMock: &appleClientMock{},
expectedTrack: nil,
expectedErr: EntityNotFoundError,
},
}
for _, tt := range tests {
Expand All @@ -142,8 +166,12 @@ func TestAppleAdapter_SearchTrack(t *testing.T) {
a := newAppleAdapter(tt.clientMock)
result, err := a.SearchTrack(ctx, tt.artistName, tt.searchName)

require.NoError(t, err)
require.Equal(t, tt.expectedTrack, result)
if tt.expectedErr != nil {
require.ErrorIs(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
require.Equal(t, tt.expectedTrack, result)
}
})
}
}
Expand All @@ -155,6 +183,7 @@ func TestAppleAdapter_FetchAlbum(t *testing.T) {
storefront string
clientMock *appleClientMock
expectedAlbum *Entity
expectedErr error
}{
{
name: "found ID",
Expand Down Expand Up @@ -187,6 +216,7 @@ func TestAppleAdapter_FetchAlbum(t *testing.T) {
storefront: "notFoundStorefront",
clientMock: &appleClientMock{},
expectedAlbum: nil,
expectedErr: EntityNotFoundError,
},
}
for _, tt := range tests {
Expand All @@ -197,8 +227,12 @@ func TestAppleAdapter_FetchAlbum(t *testing.T) {
a := newAppleAdapter(tt.clientMock)
result, err := a.FetchAlbum(ctx, tt.id)

require.NoError(t, err)
require.Equal(t, tt.expectedAlbum, result)
if tt.expectedErr != nil {
require.ErrorIs(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
require.Equal(t, tt.expectedAlbum, result)
}
})
}
}
Expand All @@ -210,6 +244,7 @@ func TestAppleAdapter_SearchAlbum(t *testing.T) {
searchName string
clientMock *appleClientMock
expectedAlbum *Entity
expectedErr error
}{
{
name: "found query",
Expand Down Expand Up @@ -244,6 +279,7 @@ func TestAppleAdapter_SearchAlbum(t *testing.T) {
searchName: "not found name",
clientMock: &appleClientMock{},
expectedAlbum: nil,
expectedErr: EntityNotFoundError,
},
}
for _, tt := range tests {
Expand All @@ -254,8 +290,12 @@ func TestAppleAdapter_SearchAlbum(t *testing.T) {
a := newAppleAdapter(tt.clientMock)
result, err := a.SearchAlbum(ctx, tt.artistName, tt.searchName)

require.NoError(t, err)
require.Equal(t, tt.expectedAlbum, result)
if tt.expectedErr != nil {
require.ErrorIs(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
require.Equal(t, tt.expectedAlbum, result)
}
})
}
}
13 changes: 9 additions & 4 deletions internal/apple/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apple
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -14,6 +15,10 @@ const (
defaulWebPlayerURL = "https://music.apple.com"
)

var (
NotFoundError = errors.New("not found")
)

type Client interface {
FetchTrack(ctx context.Context, id, storefront string) (*Entity, error)
SearchTrack(ctx context.Context, artistName, trackName string) (*Entity, error)
Expand Down Expand Up @@ -78,7 +83,7 @@ func (c *HTTPClient) FetchTrack(ctx context.Context, id, storefront string) (*En
defer response.Body.Close()

if response.StatusCode == http.StatusNotFound {
return nil, nil
return nil, NotFoundError
}

gr := getResponse{}
Expand All @@ -105,7 +110,7 @@ func (c *HTTPClient) SearchTrack(ctx context.Context, artistName, trackName stri
return sr.Resources.Songs[topResult.ID], nil
}
}
return nil, nil
return nil, NotFoundError
}
func (c *HTTPClient) FetchAlbum(ctx context.Context, id, storefront string) (*Entity, error) {
url := fmt.Sprintf(`%s/v1/catalog/%s/albums/%s`, c.apiURL, storefront, id)
Expand All @@ -116,7 +121,7 @@ func (c *HTTPClient) FetchAlbum(ctx context.Context, id, storefront string) (*En
defer response.Body.Close()

if response.StatusCode == http.StatusNotFound {
return nil, nil
return nil, NotFoundError
}
gr := getResponse{}
if err := json.NewDecoder(response.Body).Decode(&gr); err != nil {
Expand All @@ -141,7 +146,7 @@ func (c *HTTPClient) SearchAlbum(ctx context.Context, artistName, albumName stri
return sr.Resources.Albums[topResult.ID], nil
}
}
return nil, nil
return nil, NotFoundError
}

func (c *HTTPClient) getAPI(ctx context.Context, reqURL string) (*http.Response, error) {
Expand Down
Loading

0 comments on commit b89909c

Please sign in to comment.