2022-01-22 23:26:52 +00:00
|
|
|
use std::{
|
|
|
|
convert::TryInto,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2021-11-26 22:21:27 +00:00
|
|
|
|
2021-11-27 07:30:51 +00:00
|
|
|
use bytes::Bytes;
|
2021-12-16 21:42:37 +00:00
|
|
|
use futures_util::future::IntoStream;
|
2021-11-27 07:30:51 +00:00
|
|
|
use http::header::HeaderValue;
|
2021-12-26 20:18:42 +00:00
|
|
|
use hyper::{
|
|
|
|
client::ResponseFuture,
|
2022-01-22 22:17:10 +00:00
|
|
|
header::{ACCEPT, AUTHORIZATION, CONTENT_ENCODING, CONTENT_TYPE, RANGE},
|
2021-12-26 20:18:42 +00:00
|
|
|
Body, HeaderMap, Method, Request,
|
|
|
|
};
|
2021-11-27 07:30:51 +00:00
|
|
|
use protobuf::Message;
|
2021-11-26 22:21:27 +00:00
|
|
|
use rand::Rng;
|
2021-12-30 19:52:49 +00:00
|
|
|
use thiserror::Error;
|
2021-12-26 20:18:42 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
apresolve::SocketAddress,
|
2021-12-27 20:37:22 +00:00
|
|
|
cdn_url::CdnUrl,
|
2021-12-26 20:18:42 +00:00
|
|
|
error::ErrorKind,
|
|
|
|
protocol::{
|
2022-01-22 22:17:10 +00:00
|
|
|
canvaz::EntityCanvazRequest,
|
|
|
|
clienttoken_http::{ClientTokenRequest, ClientTokenRequestType, ClientTokenResponse},
|
|
|
|
connect::PutStateRequest,
|
2021-12-26 20:18:42 +00:00
|
|
|
extended_metadata::BatchedEntityRequest,
|
|
|
|
},
|
2022-01-22 23:26:52 +00:00
|
|
|
token::Token,
|
2022-01-22 22:17:10 +00:00
|
|
|
version, Error, FileId, SpotifyId,
|
2021-12-26 20:18:42 +00:00
|
|
|
};
|
2021-11-26 22:21:27 +00:00
|
|
|
|
|
|
|
component! {
|
|
|
|
SpClient : SpClientInner {
|
|
|
|
accesspoint: Option<SocketAddress> = None,
|
|
|
|
strategy: RequestStrategy = RequestStrategy::default(),
|
2022-01-22 23:26:52 +00:00
|
|
|
client_token: Option<Token> = None,
|
2021-11-26 22:21:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
pub type SpClientResult = Result<Bytes, Error>;
|
2021-11-26 22:21:27 +00:00
|
|
|
|
2021-12-30 19:52:49 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum SpClientError {
|
|
|
|
#[error("missing attribute {0}")]
|
|
|
|
Attribute(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SpClientError> for Error {
|
|
|
|
fn from(err: SpClientError) -> Self {
|
|
|
|
Self::failed_precondition(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:21:27 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum RequestStrategy {
|
|
|
|
TryTimes(usize),
|
|
|
|
Infinitely,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RequestStrategy {
|
|
|
|
fn default() -> Self {
|
|
|
|
RequestStrategy::TryTimes(10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpClient {
|
|
|
|
pub fn set_strategy(&self, strategy: RequestStrategy) {
|
|
|
|
self.lock(|inner| inner.strategy = strategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn flush_accesspoint(&self) {
|
|
|
|
self.lock(|inner| inner.accesspoint = None)
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:44:08 +00:00
|
|
|
pub async fn get_accesspoint(&self) -> Result<SocketAddress, Error> {
|
2021-11-26 22:21:27 +00:00
|
|
|
// Memoize the current access point.
|
|
|
|
let ap = self.lock(|inner| inner.accesspoint.clone());
|
2022-01-05 19:44:08 +00:00
|
|
|
let tuple = match ap {
|
2021-11-26 22:21:27 +00:00
|
|
|
Some(tuple) => tuple,
|
|
|
|
None => {
|
2022-01-05 19:44:08 +00:00
|
|
|
let tuple = self.session().apresolver().resolve("spclient").await?;
|
2021-11-26 22:21:27 +00:00
|
|
|
self.lock(|inner| inner.accesspoint = Some(tuple.clone()));
|
|
|
|
info!(
|
|
|
|
"Resolved \"{}:{}\" as spclient access point",
|
|
|
|
tuple.0, tuple.1
|
|
|
|
);
|
|
|
|
tuple
|
|
|
|
}
|
2022-01-05 19:44:08 +00:00
|
|
|
};
|
|
|
|
Ok(tuple)
|
2021-11-26 22:21:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 19:44:08 +00:00
|
|
|
pub async fn base_url(&self) -> Result<String, Error> {
|
|
|
|
let ap = self.get_accesspoint().await?;
|
|
|
|
Ok(format!("https://{}:{}", ap.0, ap.1))
|
2021-11-26 22:21:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 22:17:10 +00:00
|
|
|
pub async fn client_token(&self) -> Result<String, Error> {
|
2022-01-22 23:26:52 +00:00
|
|
|
let client_token = self.lock(|inner| {
|
|
|
|
if let Some(token) = &inner.client_token {
|
|
|
|
if token.is_expired() {
|
|
|
|
inner.client_token = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inner.client_token.clone()
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(client_token) = client_token {
|
|
|
|
return Ok(client_token.access_token);
|
2022-01-22 22:17:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 23:26:52 +00:00
|
|
|
trace!("Client token unavailable or expired, requesting new token.");
|
|
|
|
|
2022-01-22 22:17:10 +00:00
|
|
|
let mut message = ClientTokenRequest::new();
|
|
|
|
message.set_request_type(ClientTokenRequestType::REQUEST_CLIENT_DATA_REQUEST);
|
|
|
|
|
|
|
|
let client_data = message.mut_client_data();
|
|
|
|
client_data.set_client_id(self.session().client_id());
|
|
|
|
client_data.set_client_version(version::SEMVER.to_string());
|
|
|
|
|
|
|
|
let connectivity_data = client_data.mut_connectivity_sdk_data();
|
|
|
|
connectivity_data.set_device_id(self.session().device_id().to_string());
|
|
|
|
|
|
|
|
let platform_data = connectivity_data.mut_platform_specific_data();
|
2022-01-26 21:24:40 +00:00
|
|
|
|
|
|
|
match std::env::consts::OS {
|
|
|
|
"windows" => {
|
|
|
|
let (pe, image_file) = match std::env::consts::ARCH {
|
|
|
|
"arm" => (448, 452),
|
|
|
|
"aarch64" => (43620, 452),
|
|
|
|
"x86_64" => (34404, 34404),
|
|
|
|
_ => (332, 332), // x86
|
|
|
|
};
|
|
|
|
|
|
|
|
let windows_data = platform_data.mut_desktop_windows();
|
|
|
|
windows_data.set_os_version(10);
|
|
|
|
windows_data.set_os_build(21370);
|
|
|
|
windows_data.set_platform_id(2);
|
|
|
|
windows_data.set_unknown_value_6(9);
|
|
|
|
windows_data.set_image_file_machine(image_file);
|
|
|
|
windows_data.set_pe_machine(pe);
|
|
|
|
windows_data.set_unknown_value_10(true);
|
|
|
|
}
|
|
|
|
"ios" => {
|
|
|
|
let ios_data = platform_data.mut_ios();
|
|
|
|
ios_data.set_user_interface_idiom(0);
|
|
|
|
ios_data.set_target_iphone_simulator(false);
|
|
|
|
ios_data.set_hw_machine("iPhone14,5".to_string());
|
|
|
|
ios_data.set_system_version("15.2.1".to_string());
|
|
|
|
}
|
|
|
|
"android" => {
|
|
|
|
let android_data = platform_data.mut_android();
|
|
|
|
android_data.set_android_version("12.0.0_r26".to_string());
|
|
|
|
android_data.set_api_version(31);
|
|
|
|
android_data.set_device_name("Pixel".to_owned());
|
|
|
|
android_data.set_model_str("GF5KQ".to_owned());
|
|
|
|
android_data.set_vendor("Google".to_owned());
|
|
|
|
}
|
|
|
|
"macos" => {
|
|
|
|
let macos_data = platform_data.mut_desktop_macos();
|
|
|
|
macos_data.set_system_version("Darwin Kernel Version 17.7.0: Fri Oct 30 13:34:27 PDT 2020; root:xnu-4570.71.82.8~1/RELEASE_X86_64".to_string());
|
|
|
|
macos_data.set_hw_model("iMac21,1".to_string());
|
|
|
|
macos_data.set_compiled_cpu_type(std::env::consts::ARCH.to_string());
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let linux_data = platform_data.mut_desktop_linux();
|
|
|
|
linux_data.set_system_name("Linux".to_string());
|
|
|
|
linux_data.set_system_release("5.4.0-56-generic".to_string());
|
|
|
|
linux_data
|
|
|
|
.set_system_version("#62-Ubuntu SMP Mon Nov 23 19:20:19 UTC 2020".to_string());
|
|
|
|
linux_data.set_hardware(std::env::consts::ARCH.to_string());
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 22:17:10 +00:00
|
|
|
|
2022-01-22 23:26:52 +00:00
|
|
|
let body = message.write_to_bytes()?;
|
2022-01-22 22:17:10 +00:00
|
|
|
|
|
|
|
let request = Request::builder()
|
|
|
|
.method(&Method::POST)
|
|
|
|
.uri("https://clienttoken.spotify.com/v1/clienttoken")
|
|
|
|
.header(ACCEPT, HeaderValue::from_static("application/x-protobuf"))
|
|
|
|
.header(CONTENT_ENCODING, HeaderValue::from_static(""))
|
|
|
|
.body(Body::from(body))?;
|
|
|
|
|
|
|
|
let response = self.session().http_client().request_body(request).await?;
|
2022-01-22 23:26:52 +00:00
|
|
|
let message = ClientTokenResponse::parse_from_bytes(&response)?;
|
|
|
|
|
|
|
|
let client_token = self.lock(|inner| {
|
|
|
|
let access_token = message.get_granted_token().get_token().to_owned();
|
|
|
|
|
|
|
|
let client_token = Token {
|
|
|
|
access_token: access_token.clone(),
|
|
|
|
expires_in: Duration::from_secs(
|
|
|
|
message
|
|
|
|
.get_granted_token()
|
|
|
|
.get_refresh_after_seconds()
|
|
|
|
.try_into()
|
|
|
|
.unwrap_or(7200),
|
|
|
|
),
|
|
|
|
token_type: "client-token".to_string(),
|
|
|
|
scopes: message
|
|
|
|
.get_granted_token()
|
|
|
|
.get_domains()
|
|
|
|
.iter()
|
|
|
|
.map(|d| d.domain.clone())
|
|
|
|
.collect(),
|
|
|
|
timestamp: Instant::now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!("Got client token: {:?}", client_token);
|
2022-01-22 22:17:10 +00:00
|
|
|
|
2022-01-22 23:26:52 +00:00
|
|
|
inner.client_token = Some(client_token);
|
|
|
|
access_token
|
|
|
|
});
|
2022-01-22 22:17:10 +00:00
|
|
|
|
|
|
|
Ok(client_token)
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:27:15 +00:00
|
|
|
pub async fn request_with_protobuf(
|
2021-11-26 22:21:27 +00:00
|
|
|
&self,
|
2021-12-16 21:42:37 +00:00
|
|
|
method: &Method,
|
2021-11-26 22:21:27 +00:00
|
|
|
endpoint: &str,
|
|
|
|
headers: Option<HeaderMap>,
|
2021-11-27 07:30:51 +00:00
|
|
|
message: &dyn Message,
|
2021-11-26 22:21:27 +00:00
|
|
|
) -> SpClientResult {
|
|
|
|
let body = protobuf::text_format::print_to_string(message);
|
|
|
|
|
|
|
|
let mut headers = headers.unwrap_or_else(HeaderMap::new);
|
2022-01-05 20:15:19 +00:00
|
|
|
headers.insert(
|
|
|
|
CONTENT_TYPE,
|
2022-01-22 22:17:10 +00:00
|
|
|
HeaderValue::from_static("application/x-protobuf"),
|
2022-01-05 20:15:19 +00:00
|
|
|
);
|
2021-11-26 22:21:27 +00:00
|
|
|
|
|
|
|
self.request(method, endpoint, Some(headers), Some(body))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:27:15 +00:00
|
|
|
pub async fn request_as_json(
|
|
|
|
&self,
|
2021-12-16 21:42:37 +00:00
|
|
|
method: &Method,
|
2021-12-08 19:27:15 +00:00
|
|
|
endpoint: &str,
|
|
|
|
headers: Option<HeaderMap>,
|
|
|
|
body: Option<String>,
|
|
|
|
) -> SpClientResult {
|
|
|
|
let mut headers = headers.unwrap_or_else(HeaderMap::new);
|
2022-01-05 20:15:19 +00:00
|
|
|
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
|
2021-12-08 19:27:15 +00:00
|
|
|
|
|
|
|
self.request(method, endpoint, Some(headers), body).await
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:21:27 +00:00
|
|
|
pub async fn request(
|
|
|
|
&self,
|
2021-12-16 21:42:37 +00:00
|
|
|
method: &Method,
|
2021-11-26 22:21:27 +00:00
|
|
|
endpoint: &str,
|
|
|
|
headers: Option<HeaderMap>,
|
|
|
|
body: Option<String>,
|
|
|
|
) -> SpClientResult {
|
|
|
|
let mut tries: usize = 0;
|
|
|
|
let mut last_response;
|
|
|
|
|
2022-03-20 03:12:24 +00:00
|
|
|
let body = body.unwrap_or_default();
|
2021-11-26 22:21:27 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
tries += 1;
|
|
|
|
|
|
|
|
// Reconnection logic: retrieve the endpoint every iteration, so we can try
|
|
|
|
// another access point when we are experiencing network issues (see below).
|
2022-01-05 19:44:08 +00:00
|
|
|
let mut url = self.base_url().await?;
|
2021-12-16 21:42:37 +00:00
|
|
|
url.push_str(endpoint);
|
2021-11-26 22:21:27 +00:00
|
|
|
|
2021-12-30 21:36:38 +00:00
|
|
|
// Add metrics. There is also an optional `partner` key with a value like
|
|
|
|
// `vodafone-uk` but we've yet to discover how we can find that value.
|
|
|
|
let separator = match url.find('?') {
|
|
|
|
Some(_) => "&",
|
|
|
|
None => "?",
|
|
|
|
};
|
|
|
|
url.push_str(&format!("{}product=0", separator));
|
|
|
|
|
2021-11-26 22:21:27 +00:00
|
|
|
let mut request = Request::builder()
|
|
|
|
.method(method)
|
2021-12-16 21:42:37 +00:00
|
|
|
.uri(url)
|
2021-11-26 22:21:27 +00:00
|
|
|
.body(Body::from(body.clone()))?;
|
|
|
|
|
|
|
|
// Reconnection logic: keep getting (cached) tokens because they might have expired.
|
2022-01-22 23:26:52 +00:00
|
|
|
let token = self
|
|
|
|
.session()
|
|
|
|
.token_provider()
|
|
|
|
.get_token("playlist-read")
|
|
|
|
.await?;
|
|
|
|
|
2021-11-26 22:21:27 +00:00
|
|
|
let headers_mut = request.headers_mut();
|
|
|
|
if let Some(ref hdrs) = headers {
|
|
|
|
*headers_mut = hdrs.clone();
|
|
|
|
}
|
|
|
|
headers_mut.insert(
|
2021-12-16 21:42:37 +00:00
|
|
|
AUTHORIZATION,
|
2022-01-22 23:26:52 +00:00
|
|
|
HeaderValue::from_str(&format!("{} {}", token.token_type, token.access_token,))?,
|
2021-11-26 22:21:27 +00:00
|
|
|
);
|
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
last_response = self.session().http_client().request_body(request).await;
|
|
|
|
|
2021-11-26 22:21:27 +00:00
|
|
|
if last_response.is_ok() {
|
|
|
|
return last_response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break before the reconnection logic below, so that the current access point
|
|
|
|
// is retained when max_tries == 1. Leave it up to the caller when to flush.
|
|
|
|
if let RequestStrategy::TryTimes(max_tries) = self.lock(|inner| inner.strategy) {
|
|
|
|
if tries >= max_tries {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconnection logic: drop the current access point if we are experiencing issues.
|
|
|
|
// This will cause the next call to base_url() to resolve a new one.
|
2021-12-26 20:18:42 +00:00
|
|
|
if let Err(ref network_error) = last_response {
|
|
|
|
match network_error.kind {
|
|
|
|
ErrorKind::Unavailable | ErrorKind::DeadlineExceeded => {
|
2021-11-26 22:21:27 +00:00
|
|
|
// Keep trying the current access point three times before dropping it.
|
|
|
|
if tries % 3 == 0 {
|
|
|
|
self.flush_accesspoint().await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => break, // if we can't build the request now, then we won't ever
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When retrying, avoid hammering the Spotify infrastructure by sleeping a while.
|
|
|
|
// The backoff time is chosen randomly from an ever-increasing range.
|
|
|
|
let max_seconds = u64::pow(tries as u64, 2) * 3;
|
|
|
|
let backoff = Duration::from_secs(rand::thread_rng().gen_range(1..=max_seconds));
|
|
|
|
warn!(
|
|
|
|
"Unable to complete API request, waiting {} seconds before retrying...",
|
|
|
|
backoff.as_secs(),
|
|
|
|
);
|
|
|
|
debug!("Error was: {:?}", last_response);
|
|
|
|
tokio::time::sleep(backoff).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_response
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn put_connect_state(
|
|
|
|
&self,
|
|
|
|
connection_id: String,
|
2021-11-27 07:30:51 +00:00
|
|
|
state: PutStateRequest,
|
2021-11-26 22:21:27 +00:00
|
|
|
) -> SpClientResult {
|
|
|
|
let endpoint = format!("/connect-state/v1/devices/{}", self.session().device_id());
|
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("X-Spotify-Connection-Id", connection_id.parse()?);
|
|
|
|
|
2021-12-16 21:42:37 +00:00
|
|
|
self.request_with_protobuf(&Method::PUT, &endpoint, Some(headers), &state)
|
2021-11-26 22:21:27 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_metadata(&self, scope: &str, id: SpotifyId) -> SpClientResult {
|
2022-02-14 11:15:19 +00:00
|
|
|
let endpoint = format!("/metadata/4/{}/{}", scope, id.to_base16()?);
|
2021-12-16 21:42:37 +00:00
|
|
|
self.request(&Method::GET, &endpoint, None, None).await
|
2021-11-26 22:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_track_metadata(&self, track_id: SpotifyId) -> SpClientResult {
|
|
|
|
self.get_metadata("track", track_id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_episode_metadata(&self, episode_id: SpotifyId) -> SpClientResult {
|
|
|
|
self.get_metadata("episode", episode_id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_album_metadata(&self, album_id: SpotifyId) -> SpClientResult {
|
|
|
|
self.get_metadata("album", album_id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_artist_metadata(&self, artist_id: SpotifyId) -> SpClientResult {
|
|
|
|
self.get_metadata("artist", artist_id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_show_metadata(&self, show_id: SpotifyId) -> SpClientResult {
|
|
|
|
self.get_metadata("show", show_id).await
|
|
|
|
}
|
|
|
|
|
2021-12-08 19:27:15 +00:00
|
|
|
pub async fn get_lyrics(&self, track_id: SpotifyId) -> SpClientResult {
|
2022-02-14 11:15:19 +00:00
|
|
|
let endpoint = format!("/color-lyrics/v1/track/{}", track_id.to_base62()?);
|
2021-12-08 19:27:15 +00:00
|
|
|
|
2021-12-16 21:42:37 +00:00
|
|
|
self.request_as_json(&Method::GET, &endpoint, None, None)
|
|
|
|
.await
|
2021-12-08 19:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_lyrics_for_image(
|
|
|
|
&self,
|
|
|
|
track_id: SpotifyId,
|
|
|
|
image_id: FileId,
|
|
|
|
) -> SpClientResult {
|
2021-11-27 07:30:51 +00:00
|
|
|
let endpoint = format!(
|
|
|
|
"/color-lyrics/v2/track/{}/image/spotify:image:{}",
|
2022-02-14 11:15:19 +00:00
|
|
|
track_id.to_base62()?,
|
2021-11-27 07:30:51 +00:00
|
|
|
image_id
|
|
|
|
);
|
2021-11-26 22:21:27 +00:00
|
|
|
|
2021-12-16 21:42:37 +00:00
|
|
|
self.request_as_json(&Method::GET, &endpoint, None, None)
|
|
|
|
.await
|
2021-11-26 22:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Find endpoint for newer canvas.proto and upgrade to that.
|
2021-11-27 07:30:51 +00:00
|
|
|
pub async fn get_canvases(&self, request: EntityCanvazRequest) -> SpClientResult {
|
2021-11-26 22:21:27 +00:00
|
|
|
let endpoint = "/canvaz-cache/v0/canvases";
|
2021-12-16 21:42:37 +00:00
|
|
|
self.request_with_protobuf(&Method::POST, endpoint, None, &request)
|
2021-11-26 22:21:27 +00:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-11-27 07:30:51 +00:00
|
|
|
pub async fn get_extended_metadata(&self, request: BatchedEntityRequest) -> SpClientResult {
|
2021-11-26 22:21:27 +00:00
|
|
|
let endpoint = "/extended-metadata/v0/extended-metadata";
|
2021-12-16 21:42:37 +00:00
|
|
|
self.request_with_protobuf(&Method::POST, endpoint, None, &request)
|
2021-11-26 22:21:27 +00:00
|
|
|
.await
|
|
|
|
}
|
2021-12-16 21:42:37 +00:00
|
|
|
|
2021-12-27 20:37:22 +00:00
|
|
|
pub async fn get_audio_storage(&self, file_id: FileId) -> SpClientResult {
|
2021-12-16 21:42:37 +00:00
|
|
|
let endpoint = format!(
|
|
|
|
"/storage-resolve/files/audio/interactive/{}",
|
2022-02-14 11:15:19 +00:00
|
|
|
file_id.to_base16()?
|
2021-12-16 21:42:37 +00:00
|
|
|
);
|
|
|
|
self.request(&Method::GET, &endpoint, None, None).await
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:37:22 +00:00
|
|
|
pub fn stream_from_cdn(
|
2021-12-16 21:42:37 +00:00
|
|
|
&self,
|
2021-12-27 20:37:22 +00:00
|
|
|
cdn_url: &CdnUrl,
|
2021-12-16 21:42:37 +00:00
|
|
|
offset: usize,
|
|
|
|
length: usize,
|
2021-12-26 20:18:42 +00:00
|
|
|
) -> Result<IntoStream<ResponseFuture>, Error> {
|
2021-12-27 20:37:22 +00:00
|
|
|
let url = cdn_url.try_get_url()?;
|
2021-12-16 21:42:37 +00:00
|
|
|
let req = Request::builder()
|
|
|
|
.method(&Method::GET)
|
|
|
|
.uri(url)
|
|
|
|
.header(
|
|
|
|
RANGE,
|
|
|
|
HeaderValue::from_str(&format!("bytes={}-{}", offset, offset + length - 1))?,
|
|
|
|
)
|
|
|
|
.body(Body::empty())?;
|
|
|
|
|
|
|
|
let stream = self.session().http_client().request_stream(req)?;
|
|
|
|
|
|
|
|
Ok(stream)
|
|
|
|
}
|
2021-12-30 19:52:49 +00:00
|
|
|
|
|
|
|
pub async fn request_url(&self, url: String) -> SpClientResult {
|
|
|
|
let request = Request::builder()
|
|
|
|
.method(&Method::GET)
|
|
|
|
.uri(url)
|
|
|
|
.body(Body::empty())?;
|
|
|
|
|
|
|
|
self.session().http_client().request_body(request).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio preview in 96 kbps MP3, unencrypted
|
|
|
|
pub async fn get_audio_preview(&self, preview_id: &FileId) -> SpClientResult {
|
|
|
|
let attribute = "audio-preview-url-template";
|
|
|
|
let template = self
|
|
|
|
.session()
|
|
|
|
.get_user_attribute(attribute)
|
|
|
|
.ok_or_else(|| SpClientError::Attribute(attribute.to_string()))?;
|
|
|
|
|
2022-02-14 11:15:19 +00:00
|
|
|
let mut url = template.replace("{id}", &preview_id.to_base16()?);
|
2022-01-12 21:09:57 +00:00
|
|
|
let separator = match url.find('?') {
|
|
|
|
Some(_) => "&",
|
|
|
|
None => "?",
|
|
|
|
};
|
|
|
|
url.push_str(&format!("{}cid={}", separator, self.session().client_id()));
|
2021-12-30 19:52:49 +00:00
|
|
|
|
|
|
|
self.request_url(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first 128 kB of a track, unencrypted
|
|
|
|
pub async fn get_head_file(&self, file_id: FileId) -> SpClientResult {
|
|
|
|
let attribute = "head-files-url";
|
|
|
|
let template = self
|
|
|
|
.session()
|
|
|
|
.get_user_attribute(attribute)
|
|
|
|
.ok_or_else(|| SpClientError::Attribute(attribute.to_string()))?;
|
|
|
|
|
2022-02-14 11:15:19 +00:00
|
|
|
let url = template.replace("{file_id}", &file_id.to_base16()?);
|
2021-12-30 19:52:49 +00:00
|
|
|
|
|
|
|
self.request_url(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_image(&self, image_id: FileId) -> SpClientResult {
|
|
|
|
let attribute = "image-url";
|
|
|
|
let template = self
|
|
|
|
.session()
|
|
|
|
.get_user_attribute(attribute)
|
|
|
|
.ok_or_else(|| SpClientError::Attribute(attribute.to_string()))?;
|
2022-02-14 11:15:19 +00:00
|
|
|
let url = template.replace("{file_id}", &image_id.to_base16()?);
|
2021-12-30 19:52:49 +00:00
|
|
|
|
|
|
|
self.request_url(url).await
|
|
|
|
}
|
2021-11-26 22:21:27 +00:00
|
|
|
}
|