Cotton code
50 lines · 2.2 KB · RustRaw
//! `spotify.*` — Spotify on the person's own object.//!//! The grant, the pull and every figure the status says live in the object//! on the Worker (`apps/business/src/spotify.ts`): it keeps the refresh token//! sealed, pulls the library on its own alarm and says how the account//! stands. This family asks it, through the door `personal.connect` held//! ([`ObjectDoor`]), and hands its JSON to Swift as it came; it keeps nothing.//!//! - `spotify.status {}` → `{ available, account: null | { userId,//!   displayName, connectedAt, phase, syncing, syncedAt, lastPassAt,//!   lastError, lastErrorMessage, counts, shows } }` — one account at most.//! - `spotify.connect {}` → `{ authorizeUrl, state }`: the caller opens the//!   URL in the system browser; Spotify brings the browser back to the//!   Worker, which trades the code and says so on its own page. There is no//!   second call.//! - `spotify.disconnect {}` → `{ ok, account }`: the one account forgotten;//!   what was pulled stays.use serde_json::{json, Value};use cotton_client::transport::object::routes;use crate::handle::CottonHandle;use crate::methods::MethodError;use crate::phone::personal_object::ObjectDoor;pub const METHODS: [&str; 3] = ["spotify.connect", "spotify.disconnect", "spotify.status"];pub fn call(handle: &CottonHandle, method: &str, request: &Value) -> Result<Value, MethodError> {    let door = handle        .client()        .object_door()        .ok_or_else(|| MethodError::new("not_connected", "this phone is not connected to its database yet"))?;    over(&door, method, request)}/// The same, through a NAMED door: a suite drives one over a scripted exchange.pub fn over(door: &ObjectDoor, method: &str, _request: &Value) -> Result<Value, MethodError> {    match method {        "spotify.status" => door.send("GET", routes::SPOTIFY_STATUS, None),        "spotify.connect" => door.send("GET", routes::SPOTIFY_CONNECT, None),        // One account at most: the object forgets the one it holds.        "spotify.disconnect" => door.send("POST", routes::SPOTIFY_DISCONNECT, Some(&json!({}))),        other => Err(MethodError::new("unknown_method", format!("`{other}` is not a Spotify method"))),    }}#[cfg(test)]#[path = "spotify_tests.rs"]mod tests;