1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! The search function.

use crate::*;

/// Search for an item.
///
/// `include_external` specifies whether to include audio content that is hosted externally.
/// Playlist results are not affected by `market`. `limit` must be in the range [1..50], and is
/// applied individually to each type specified in `types`, not the whole response. `offset` has a
/// maximum of 10,000.
///
/// See
/// [here](https://developer.spotify.com/documentation/web-api/reference/search/search/#writing-a-query---guidelines)
/// on how to write a query. The only difference is that you shouldn't encode spaces as `%20` or
/// `+`, as that is done by this function automatically.
///
/// # Limitations
///
/// - You cannot fetch sorted results.
/// - You cannot search for playlists that contain a track.
/// - You can only search for one genre at a time.
/// - You cannot search for playlists in a user's library.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/search/search/).
pub async fn search(
    token: &AccessToken,
    query: &str,
    types: &[ItemType],
    include_external: bool,
    limit: usize,
    offset: usize,
    market: Option<Market>,
) -> Result<SearchResults, EndpointError<Error>> {
    let types = if types.is_empty() {
        &[
            ItemType::Album,
            ItemType::Artist,
            ItemType::Playlist,
            ItemType::Track,
        ]
    } else {
        types
    };

    Ok(request!(
        token,
        GET "/v1/search",
        query_params = {
            "q": query,
            "type": &types.iter().map(|&item| item.as_str()).collect::<Vec<_>>().join(","),
            "limit": &limit.to_string(),
            "offset": &offset.to_string()
        },
        optional_query_params = {
            "include_external": if include_external {
                Some("audio")
            } else {
                None
            },
            "market": market.map(|m| m.as_str())
        },
        ret = SearchResults
    ))
}

#[cfg(test)]
mod tests {
    use crate::endpoints::token;
    use crate::*;

    #[tokio::test]
    async fn test_search_artist() {
        let res = search(
            &token().await,
            "tania bowra",
            &[ItemType::Artist],
            false,
            1,
            0,
            None,
        )
        .await
        .unwrap();
        assert_eq!(res.albums, None);
        assert_eq!(res.tracks, None);
        assert_eq!(res.playlists, None);
        let artists = res.artists.unwrap();
        assert_eq!(artists.limit, 1);
        assert_eq!(artists.offset, 0);
        assert_eq!(artists.items.len(), 1);
        assert_eq!(artists.items[0].name, "Tania Bowra");
    }

    #[tokio::test]
    async fn test_search_album_tracks() {
        search(
            &token().await,
            "abba",
            &[ItemType::Album, ItemType::Track],
            true,
            1,
            0,
            Some(Market::FromToken),
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_search_playlist() {
        search(
            &token().await,
            "doom metal",
            &[ItemType::Playlist],
            false,
            1,
            0,
            None,
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_search_all() {
        search(&token().await, "test", &[], false, 3, 2, None)
            .await
            .unwrap();
    }
}