Skip to content

Commit

Permalink
Respect the ANKIHUB_APP_URL env var
Browse files Browse the repository at this point in the history
This is used by the add-on.
  • Loading branch information
abdnh committed Jun 13, 2024
1 parent d8a4fd6 commit d7e1ef6
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion rslib/src/ankihub/http_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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;
Expand All @@ -10,6 +12,7 @@ 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 {
Expand All @@ -20,9 +23,19 @@ pub struct HttpAnkiHubClient {

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("https://app.ankihub.net/api/").unwrap(),
endpoint: Url::try_from(endpoint.as_str()).unwrap(),
client,
}
}
Expand Down

0 comments on commit d7e1ef6

Please sign in to comment.