Skip to content

Commit

Permalink
#7: rename base to baseUrl, and construct Url in a cleaner way, trimm…
Browse files Browse the repository at this point in the history
…ing any leading or trailing slashes from the base url
  • Loading branch information
t3kmateo committed May 18, 2024
1 parent 586ba17 commit bafbe58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 3 additions & 1 deletion examples/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"_comment": "This is a sample configuration file. Port and Base URL are optional fields",
"host": "0.0.0.0",
"protocol": "http",
"apiKey": "<YOUR_API_KEY>",
"port": "6767",
"apiKey": "<YOUR_API_KEY>"
"baseUrl": "bazarr"
}
7 changes: 3 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::{Parser, Subcommand};
use reqwest::{header, Client, Url};
use reqwest::{header, Client};
use reqwest_middleware::ClientBuilder;
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, str::FromStr, time::Duration};
use std::{path::PathBuf, time::Duration};

use crate::{actions::Action, connection::check_health, data_types::app_config::AppConfig};

Expand Down Expand Up @@ -79,8 +79,7 @@ impl Commands {
let client = ClientBuilder::new(reqwest_client)
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
let base_url = format!("{}://{}:{}{}/api", config.protocol, config.host, config.port, config.base);
let url = Url::from_str(&base_url)?;
let url = config.construct_url();
check_health(&client, &url).await;

let mut action = Action::new(client, url);
Expand Down
28 changes: 24 additions & 4 deletions src/data_types/app_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use config::{Config, ConfigError, File, FileFormat};
use reqwest::Url;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
Expand All @@ -24,8 +25,8 @@ impl fmt::Display for Protocol {
pub struct AppConfig {
pub protocol: Protocol,
pub host: String,
pub port: String,
pub base: String,
pub port: Option<String>,
pub base_url: String,
pub api_key: String,
}

Expand All @@ -34,11 +35,30 @@ impl AppConfig {
let config = Config::builder()
.add_source(File::new(config_path, FileFormat::Json))
.set_default("host", "0.0.0.0")?
.set_default("port", "6767")?
.set_default("protocol", "http")?
.set_default("base", "")?
.set_default("baseUrl", "")?
.build()?;

config.try_deserialize()
}

pub fn construct_url(&self) -> Url {
let mut bazarr_url = format!("{}://{}", self.protocol, self.host);

if let Some(port) = &self.port {
bazarr_url = format!("{}:{}", bazarr_url, port);
}

// clean the base_url by removing leading and trailing slashes
let clean_base_url = self.base_url.trim_matches('/');

let mut url = Url::parse(&bazarr_url).unwrap();
url.path_segments_mut()
.unwrap()
.push(clean_base_url)
.push("api");

println!("Bazarr API URL: {}", url);
url
}
}

0 comments on commit bafbe58

Please sign in to comment.