Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force market as request option to search for shows #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ const (
CountryCanada = "CA"
CountryChile = "CL"
CountryChina = "CN"
CountryColombia = "CO"
CountryFinland = "FI"
CountryFrance = "FR"
CountryGermany = "DE"
CountryHongKong = "HK"
CountryIreland = "IE"
CountryIndia = "IN"
CountryItaly = "IT"
CountryJapan = "JP"
CountrySpain = "ES"
CountryFinland = "FI"
CountryFrance = "FR"
CountryMexico = "MX"
CountryNewZealand = "NZ"
CountryRussia = "RU"
CountrySpain = "ES"
CountrySwitzerland = "CH"
CountryUnitedArabEmirates = "AE"
CountryUnitedKingdom = "GB"
Expand Down
19 changes: 18 additions & 1 deletion examples/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"context"
"fmt"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"log"
"os"

spotifyauth "github.com/zmb3/spotify/v2/auth"

"golang.org/x/oauth2/clientcredentials"

"github.com/zmb3/spotify/v2"
Expand Down Expand Up @@ -46,4 +47,20 @@ func main() {
fmt.Println(" ", item.Name)
}
}

// search for shows using market query parameter
options := []spotify.RequestOption{
spotify.Market("CO"),
}

results, err = client.Search(ctx, "cyclast", spotify.SearchTypeShow, options...)
if err != nil {
log.Fatal(err)
}
// handle show results
if results.Shows != nil {
for _, item := range results.Shows.Shows {
fmt.Println(" ", item.Name)
}
}
}
9 changes: 9 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package spotify

import (
"context"
"errors"
"strings"
)

// ErrSearchMarketRequired is the error returned when you attempt to search for shows
// but "market" is not part of options parameters. It is required due to Spotify API unsolved issue.
var ErrSearchMarketRequired = errors.New("spotify:search: searching for shows requires market parameter")

const (
// MarketFromToken can be used in place of the Options.Country parameter
// if the Client has a valid access token. In this case, the
Expand Down Expand Up @@ -124,6 +129,10 @@ func (c *Client) Search(ctx context.Context, query string, t SearchType, opts ..
v.Set("q", query)
v.Set("type", t.encode())

if t == SearchTypeShow && v.Get("market") == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is specific to searching Shows - but I'm also not sure that returning an Error here is the right thing to do. Spotify could adjust their API behaviour in future. Perhaps we just need to better document that when using Client ID/Secret you need to specify the market as there's no default market.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, better documentation is probably the way to go as we don't control the scope of these params or if/when this issue really will get fixed. we can't decode the bearer token to get user info for us to implement the fallback method can we?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't decode the bearer token to get user info for us to implement the fallback method can we?

I think we'd probably want to avoid that - even if the bearer token was decodable it'd probably be a bad idea for us to couple with that.

return nil, ErrSearchMarketRequired
}

spotifyURL := c.baseURL + "search?" + v.Encode()

var result SearchResult
Expand Down
29 changes: 29 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ func TestSearchPlaylistTrack(t *testing.T) {
}
}

func TestSearchShow(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/search_show.txt")
defer server.Close()

options := []RequestOption{
Market("CO"),
}

result, err := client.Search(context.Background(), "go time", SearchTypeShow, options...)
if err != nil {
t.Error(err)
}
if result.Albums != nil {
t.Error("Searched for shows but received album results")
}
if result.Playlists != nil {
t.Error("Searched for shows but received playlist results")
}
if result.Tracks != nil {
t.Error("Searched for shows but received track results")
}
if result.Shows == nil || len(result.Shows.Shows) == 0 {
t.Error("Didn't receive show results")
}
if result.Shows.Shows[0].Name != "Go Time: Golang, Software Engineering" {
t.Error("Got wrong show name")
}
}

func TestPrevNextSearchPageErrors(t *testing.T) {
client, server := testClientString(0, "")
defer server.Close()
Expand Down
49 changes: 49 additions & 0 deletions test_data/search_show.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"shows": {
"href": "https://api.spotify.com/v1/search?query=go+time&type=show&locale=es-CO%2Ces%3Bq%3D0.9%2Cen-US%3Bq%3D0.8%2Cen%3Bq%3D0.7%2Ces-419%3Bq%3D0.6&offset=0&limit=1",
"limit": 20,
"next": "https://api.spotify.com/v1/search?query=go+time&type=show&locale=es-CO%2Ces%3Bq%3D0.9%2Cen-US%3Bq%3D0.8%2Cen%3Bq%3D0.7%2Ces-419%3Bq%3D0.6&offset=1&limit=1",
"offset": 0,
"previous": null,
"total": 1,
"items": [
{
"available_markets": ["AD", "AE", "AG", "AL", "AM", "AR", "AT", "AU", "BA", "BB", "BE", "BF", "BG", "BH", "BJ", "BO", "BR", "BS", "BT", "BW", "BZ", "CA", "CH", "CL", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GB", "GD", "GE", "GH", "GM", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JM", "JO", "JP", "KE", "KI", "KN", "KR", "KW", "LB", "LC", "LI", "LR", "LS", "LT", "LU", "LV", "MA", "MC", "ME", "MG", "MH", "MK", "ML", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PL", "PR", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "TH", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "UA", "US", "UY", "VC", "VN", "VU", "WS", "XK", "ZA"],
"copyrights": [],
"description": "Your source for diverse discussions from around the Go community. This show records LIVE every Tuesday at 3pm US Eastern. Join the Golang community and chat with us during the show in the #gotimefm channel of Gophers slack. Panelists include Mat Ryer, Jon Calhoun, Natalie Pistunovich, Johnny Boursiquot, Angelica Hill, Kris Brandow, and Ian Lopshire. We discuss cloud infrastructure, distributed systems, microservices, Kubernetes, Docker… oh and also Go! Some people search for GoTime or GoTimeFM and can’t find the show, so now the strings GoTime and GoTimeFM are in our description too.",
"html_description": "Your source for diverse discussions from around the Go community. This show records LIVE every Tuesday at 3pm US Eastern. Join the Golang community and chat with us during the show in the #gotimefm channel of Gophers slack. Panelists include Mat Ryer, Jon Calhoun, Natalie Pistunovich, Johnny Boursiquot, Angelica Hill, Kris Brandow, and Ian Lopshire. We discuss cloud infrastructure, distributed systems, microservices, Kubernetes, Docker… oh and also Go! Some people search for GoTime or GoTimeFM and can’t find the show, so now the strings GoTime and GoTimeFM are in our description too.",
"explicit": false,
"external_urls": {
"spotify": "https://open.spotify.com/show/2cKdcxETn7jDp7uJCwqmSE"
},
"href": "https://api.spotify.com/v1/shows/2cKdcxETn7jDp7uJCwqmSE",
"id": "2cKdcxETn7jDp7uJCwqmSE",
"images": [
{
"url": "https://i.scdn.co/image/18d9cacf366a7173b10204eb983fe5d374b728dd",
"height": 640,
"width": 640
},
{
"url": "https://i.scdn.co/image/3f9b1dd55b3353a0d70dfcca6a44c01775cfbfcc",
"height": 300,
"width": 300
},
{
"url": "https://i.scdn.co/image/aa9e23de8a5a64bedaf526bb963123413984e53f",
"height": 64,
"width": 64
}
],
"is_externally_hosted": false,
"languages": ["en-US"],
"media_type": "audio",
"name": "Go Time: Golang, Software Engineering",
"publisher": "Changelog Media",
"type": "show",
"uri": "spotify:show:2cKdcxETn7jDp7uJCwqmSE",
"total_episodes": 316
}
]
}
}