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