feat(api): return (Self, AuthenticationResult) from authenticate
Some checks failed
build / checks-matrix (push) Has been cancelled
build / codecov (push) Has been cancelled
docs / docs (push) Has been cancelled
build / checks-build (push) Has been cancelled

This commit is contained in:
2026-01-16 11:33:55 +05:30
parent e5ef173473
commit c8c371230f
3 changed files with 354 additions and 71 deletions

View File

@@ -34,10 +34,10 @@ impl JellyfinClient {
username: impl AsRef<str>,
password: impl AsRef<str>,
config: JellyfinConfig,
) -> Result<Self> {
) -> Result<(Self, jellyfin::AuthenticationResult)> {
let url = format!("{}/Users/AuthenticateByName", config.server_url);
let client = reqwest::Client::new();
let token = client
let auth_result = client
.post(url)
.json(&jellyfin::AuthenticateUserByName {
username: Some(username.as_ref().to_string()),
@@ -47,10 +47,14 @@ impl JellyfinClient {
.await?
.error_for_status()?
.json::<jellyfin::AuthenticationResult>()
.await?
.await?;
let token = auth_result
.access_token
.as_ref()
.ok_or_else(|| std::io::Error::other("No field access_token in auth response"))?;
Self::pre_authenticated(token, config)
Ok((Self::pre_authenticated(token, config)?, auth_result))
}
pub fn pre_authenticated(token: impl AsRef<str>, config: JellyfinConfig) -> Result<Self> {
@@ -80,6 +84,10 @@ impl JellyfinClient {
}
}
pub fn access_token(&self) -> Option<&str> {
self.access_token.as_deref().map(|s| &*s)
}
pub async fn save_token(&self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
if let Some(token) = &self.access_token {
tokio::fs::write(path, &**token).await
@@ -104,6 +112,19 @@ impl JellyfinClient {
self.access_token = Some(token.as_ref().into());
}
pub async fn me(&self) -> Result<jellyfin::UserDto> {
let uri = "Users/Me";
let text = self
.request_builder(reqwest::Method::GET, uri)
.send()
.await?
.error_for_status()?
.text()
.await?;
let out: jellyfin::UserDto = serde_json::from_str(&text)?;
Ok(out)
}
pub fn request_builder(
&self,
method: reqwest::Method,