mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00

* Add AnkiHub section to preferences screen * Add short intro for AnkiWeb and AnkiHub to syncing section * Add AnkiHub login screen * Implement login methods in backend * Set minimum dialog width * Add missing colon * Respect the ANKIHUB_APP_URL env var This is used by the add-on. * Simplify login error reporting * Fix from_prefs_screen not passed to subcall * Add missing ankihub_pb2 import * Install AnkiHub add-on after sign-in * Avoid .exec() * Update ftl/core/sync.ftl Co-authored-by: Damien Elmes <dae@users.noreply.github.com> * Split translation string * Support login by username/email * Fix entered username/email not being passed back to on_done * Remove unused import * Move to 'Third-party services' section * Tweak login dialog's heading * Remove 'third-party' from intro text * Tweak copy * Prefix profile keys * Tweak strings * Remove description from login dialog * Remove signup links * Clear credentials in ankihub_logout() * Call .adjustSize() * Title Case * Add padding to third-party services, and fix tab order from other PR
62 lines
1.8 KiB
Rust
62 lines
1.8 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use std::env;
|
|
|
|
use reqwest::Client;
|
|
use reqwest::Response;
|
|
use reqwest::Result;
|
|
use reqwest::Url;
|
|
use serde::Serialize;
|
|
|
|
use crate::ankihub::login::LoginRequest;
|
|
|
|
static API_VERSION: &str = "18.0";
|
|
static DEFAULT_API_URL: &str = "https://app.ankihub.net/api/";
|
|
|
|
#[derive(Clone)]
|
|
pub struct HttpAnkiHubClient {
|
|
pub token: String,
|
|
pub endpoint: Url,
|
|
client: Client,
|
|
}
|
|
|
|
impl HttpAnkiHubClient {
|
|
pub fn new<S: Into<String>>(token: S, client: Client) -> HttpAnkiHubClient {
|
|
let endpoint = match env::var("ANKIHUB_APP_URL") {
|
|
Ok(url) => {
|
|
if let Ok(u) = Url::try_from(url.as_str()) {
|
|
u.join("api/").unwrap().to_string()
|
|
} else {
|
|
DEFAULT_API_URL.to_string()
|
|
}
|
|
}
|
|
Err(_) => DEFAULT_API_URL.to_string(),
|
|
};
|
|
HttpAnkiHubClient {
|
|
token: token.into(),
|
|
endpoint: Url::try_from(endpoint.as_str()).unwrap(),
|
|
client,
|
|
}
|
|
}
|
|
|
|
async fn request<T: Serialize + ?Sized>(&self, method: &str, data: &T) -> Result<Response> {
|
|
let url = self.endpoint.join(method).unwrap();
|
|
let mut builder = self.client.post(url).header(
|
|
reqwest::header::ACCEPT,
|
|
format!("application/json; version={API_VERSION}"),
|
|
);
|
|
if !self.token.is_empty() {
|
|
builder = builder.header("Authorization", format!("Token {}", self.token));
|
|
}
|
|
builder.json(&data).send().await
|
|
}
|
|
|
|
pub async fn login(&self, data: LoginRequest) -> Result<Response> {
|
|
self.request("login/", &data).await
|
|
}
|
|
|
|
pub async fn logout(&self) -> Result<Response> {
|
|
self.request("logout/", "").await
|
|
}
|
|
}
|