Explorar o código

go-music: fix search api

Douglas Andreani %!s(int64=5) %!d(string=hai) anos
pai
achega
50368a3de4
Modificáronse 2 ficheiros con 40 adicións e 42 borrados
  1. 32 27
      go-music/mb.go
  2. 8 15
      go-music/mb_test.go

+ 32 - 27
go-music/mb.go

@@ -13,34 +13,29 @@ const (
 	artistSearchEndpoint = "artist?query"
 )
 
-//MB holds the http client and the search results
+//MB holds the http client
 type MB struct {
 	Client http.Client
+}
+
+//Search holds the search returns
+type Search struct {
 	artist artistSearch
 }
 
-//NewMusicBrainzSearch returns a new Search object
-func NewMusicBrainzSearch() MB {
+//NewMusicBrainz returns a new Search object
+func NewMusicBrainz() MB {
 
-	var mbSearch MB
+	var mb MB
 	timeout := time.Duration(10 * time.Second)
 
-	mbSearch.Client = http.Client{Timeout: timeout}
-	return mbSearch
-}
-
-//GetArtistName get the artist with the highest score on the search
-func (m MB) GetArtistName() (string, error) {
-
-	if len(m.artist.Artists) == 0 {
-		return "", fmt.Errorf("could not find any artist")
-	}
-	return m.artist.Artists[0].Name, nil
+	mb.Client = http.Client{Timeout: timeout}
+	return mb
 }
 
 //SearchForArtist return a list of artists using `query` as parameter if none found returns error
-func (m *MB) SearchForArtist(query string) error {
-
+func (m MB) SearchForArtist(query string) (*Search, error) {
+	var ret Search
 	uri := fmt.Sprintf("%s/%s=%s", apiURI, artistSearchEndpoint, query)
 
 	req, err := http.NewRequest(
@@ -56,36 +51,46 @@ func (m *MB) SearchForArtist(query string) error {
 
 	if err != nil {
 		fmt.Printf("error creating request\n%v", err)
-		return err
+		return nil, err
 	}
 
 	resp, err := m.Client.Do(req)
 	if err != nil {
 		fmt.Printf("error opening request\n%v", err)
-		return err
+		return nil, err
 	}
 	defer resp.Body.Close()
 	if err != nil {
 		fmt.Printf("could not read body\n%v", err)
-		return err
+		return nil, err
 	}
 
-	err = json.NewDecoder(resp.Body).Decode(&m.artist)
+	err = json.NewDecoder(resp.Body).Decode(&ret.artist)
 	if err != nil {
 		fmt.Printf("could not decode body\n%v", err)
-		return err
+		return nil, err
+	}
+
+	if ret.artist.Count == 0 {
+		return nil, fmt.Errorf("cound not found artist: %s", query)
 	}
 
-	if m.artist.Count == 0 {
-		return fmt.Errorf("cound not found artist: %s", query)
+	return &ret, nil
+}
+
+//GetArtistName get the artist with the highest score on the search
+func (s Search) GetArtistName() (string, error) {
+
+	if len(s.artist.Artists) == 0 {
+		return "", fmt.Errorf("could not find any artist")
 	}
-	return nil
+	return s.artist.Artists[0].Name, nil
 }
 
 //GetArtistTags returns the tags in order of relevance of the current artist
-func (m MB) GetArtistTags() []Tags {
+func (s Search) GetArtistTags() []Tags {
 	var t []Tags
-	for _, elem := range m.artist.Artists[0].Tags {
+	for _, elem := range s.artist.Artists[0].Tags {
 		if int(elem.Count) > 0 {
 			t = append(t, elem)
 		}

+ 8 - 15
go-music/mb_test.go

@@ -1,34 +1,27 @@
 package mb
 
 import (
-	"fmt"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
 )
 
 func TestShouldBeEqualToArtist(t *testing.T) {
-	m := NewMusicBrainzSearch()
-	err := m.SearchForArtist("Metallica")
-	if err != nil {
-		fmt.Printf("Error on search_for_artist with parameter Metallica\n")
-		return
-	}
-	name, err := m.GetArtistName()
-	if err != nil {
-		fmt.Printf("Error on get_artist_name: no artist found\n")
-	}
+	m := NewMusicBrainz()
+	s, _ := m.SearchForArtist("Metallica")
+	name, _ := s.GetArtistName()
+
 	assert.Equal(t, name, "Metallica")
 }
 
 func TestIfArtistIsValidThenErrIsNil(t *testing.T) {
-	m := NewMusicBrainzSearch()
-	err := m.SearchForArtist("Metallica")
+	m := NewMusicBrainz()
+	_, err := m.SearchForArtist("Metallica")
 	assert.Nil(t, err, "err should be nil if found artist")
 }
 
 func TestErrShouldContainErrorIfArtistIsInvalid(t *testing.T) {
-	m := NewMusicBrainzSearch()
-	err := m.SearchForArtist("asdfkjahsdflkjasdhfoia")
+	m := NewMusicBrainz()
+	_, err := m.SearchForArtist("asdfkjahsdflkjasdhfoia")
 	assert.NotNil(t, err, "err should not be nil if found artist")
 }