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

54 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_auth::Token, spotify_types::*};
2023-06-28 00:34:49 +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();
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();
let _res = client
.post(crate::BASE_URL.to_owned() + "/playlists/" + id + "/tracks")
2023-06-28 00:34:49 +00:00
.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> {
2023-06-28 00:34:49 +00:00
let mut body: HashMap<String, Vec<String>> = HashMap::new();
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();
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(())
}