spotify_rs/endpoint/
search.rs

1use serde::Serialize;
2
3use crate::{
4    auth::AuthFlow,
5    error::Result,
6    model::search::{Item, SearchQuery, SearchResults},
7    query_list,
8};
9
10use super::{Client, Endpoint};
11
12impl Endpoint for SearchEndpoint {}
13
14/// Search for an item. The query can be either a string or
15/// [`SearchQuery`](crate::model::search::SearchQuery). More details about
16/// search queries can be found
17/// [here](https://developer.spotify.com/documentation/web-api/reference/search).
18pub fn search(query: impl Into<SearchQuery>, item_types: &[Item]) -> SearchEndpoint {
19    let r#type = query_list(item_types);
20    let query = query.into().to_string();
21
22    SearchEndpoint {
23        query,
24        r#type,
25        ..Default::default()
26    }
27}
28
29#[derive(Clone, Debug, Default, Serialize)]
30pub struct SearchEndpoint {
31    #[serde(rename = "q")]
32    pub(crate) query: String,
33    pub(crate) r#type: String,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub(crate) market: Option<String>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub(crate) limit: Option<u32>,
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub(crate) offset: Option<u32>,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub(crate) include_external: Option<bool>,
42}
43
44impl SearchEndpoint {
45    #[doc = include_str!("../docs/market.md")]
46    pub fn market(mut self, market: impl Into<String>) -> Self {
47        self.market = Some(market.into());
48        self
49    }
50
51    #[doc = include_str!("../docs/limit.md")]
52    pub fn limit(mut self, limit: u32) -> Self {
53        self.limit = Some(limit);
54        self
55    }
56
57    #[doc = include_str!("../docs/offset.md")]
58    pub fn offset(mut self, offset: u32) -> Self {
59        self.offset = Some(offset);
60        self
61    }
62
63    /// If specified, it signals that the client can play externally hosted audio content,
64    /// and marks the content as playable in the response.
65    ///
66    /// By default externally hosted audio content is marked as unplayable in the response.
67    pub fn include_external(mut self, include_external: bool) -> Self {
68        self.include_external = Some(include_external);
69        self
70    }
71
72    /// Allows you to change the types of items to search.
73    pub fn item_types(mut self, item_types: &[Item]) -> Self {
74        self.r#type = query_list(item_types);
75        self
76    }
77
78    #[doc = include_str!("../docs/send.md")]
79    pub async fn get(self, spotify: &Client<impl AuthFlow>) -> Result<SearchResults> {
80        spotify.get("/search".to_owned(), self).await
81    }
82}