cool-spotify-blend/server/src/spotify_playlist.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

2023-06-28 00:34:49 +00:00
use std::collections::HashMap;
use sqlx::MySqlPool;
use crate::{spotify_types::*, spotify_auth::Token};
pub async fn add_to_playlist(id: &str, uris: Vec<Uri>, 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<String, Vec<String>> = HashMap::new();
let uris: Vec<String> = 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(&params)
.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<Uri>, token: &Token, pool: &MySqlPool)
-> Result<(), reqwest::Error> {
let mut body: HashMap<String, Vec<String>> = HashMap::new();
let uris: Vec<String> = 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(())
}