2023-06-28 00:34:49 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use sqlx::MySqlPool;
|
|
|
|
|
2023-07-13 02:26:25 +00:00
|
|
|
use crate::{spotify_auth::Token, spotify_types::*};
|
2023-06-28 00:34:49 +00:00
|
|
|
|
2023-07-13 02:26:25 +00:00
|
|
|
pub async fn add_to_playlist(
|
|
|
|
id: &str,
|
|
|
|
uris: Vec<Uri>,
|
|
|
|
token: &Token,
|
|
|
|
) -> Result<(), reqwest::Error> {
|
2023-06-28 00:34:49 +00:00
|
|
|
let mut params = HashMap::new();
|
|
|
|
params.insert("position", "0"); // Insert at top, we could remove to append
|
|
|
|
|
|
|
|
let mut body: HashMap<String, Vec<String>> = HashMap::new();
|
2023-07-13 02:26:25 +00:00
|
|
|
let uris: Vec<String> = uris.iter().map(|u| &u.0).cloned().collect();
|
2023-06-28 00:34:49 +00:00
|
|
|
body.insert("uris".to_owned(), uris);
|
|
|
|
|
|
|
|
log::info!("Uri body: {:?}", body);
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
2023-07-13 02:26:25 +00:00
|
|
|
let _res = client
|
|
|
|
.post(crate::BASE_URL.to_owned() + "/playlists/" + id + "/tracks")
|
2023-06-28 00:34:49 +00:00
|
|
|
.query(¶ms)
|
|
|
|
.json(&body)
|
|
|
|
.header("Authorization", "Bearer ".to_owned() + &token.0)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// WE CANNOT SET MORE THAN 100 AT ONCE!!!
|
2023-07-13 02:26:25 +00:00
|
|
|
pub async fn set_playlist(
|
|
|
|
id: &str,
|
|
|
|
uris: Vec<Uri>,
|
|
|
|
token: &Token,
|
|
|
|
_pool: &MySqlPool,
|
|
|
|
) -> Result<(), reqwest::Error> {
|
2023-06-28 00:34:49 +00:00
|
|
|
let mut body: HashMap<String, Vec<String>> = HashMap::new();
|
2023-07-13 02:26:25 +00:00
|
|
|
let uris: Vec<String> = uris.iter().map(|u| &u.0).cloned().collect();
|
2023-06-28 00:34:49 +00:00
|
|
|
body.insert("uris".to_owned(), uris);
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
2023-07-13 02:26:25 +00:00
|
|
|
let _res = client
|
|
|
|
.put(crate::BASE_URL.to_owned() + "/playlists/" + id + "/tracks")
|
2023-06-28 00:34:49 +00:00
|
|
|
.json(&body)
|
|
|
|
.header("Authorization", "Bearer ".to_owned() + &token.0)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|