Skip to content

Commit

Permalink
refactor: url helper
Browse files Browse the repository at this point in the history
  • Loading branch information
beeb committed Aug 22, 2024
1 parent 49a645a commit ea196fe
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 43 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ env_logger = "0.11.3"
mockito = "1.4.0"
rand = "0.8.5"
serial_test = "3.1.1"
temp-env = { version = "0.3.6", features = ["async_closure"] }

[lib]
name = "soldeer"
Expand Down
6 changes: 3 additions & 3 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
errors::AuthError,
utils::{get_base_url, security_file_path},
utils::{api_url, security_file_path},
};
use cliclack::log::{info, remark, success};
use email_address_parser::{EmailAddress, ParsingOptions};
Expand Down Expand Up @@ -55,9 +55,9 @@ pub fn get_token() -> Result<String> {

async fn execute_login(login: &Login) -> Result<()> {
let security_file = security_file_path()?;
let url = format!("{}/api/v1/auth/login", get_base_url());
let url = api_url("auth/login", &[]);
let client = Client::new();
let res = client.post(&url).json(login).send().await?;
let res = client.post(url).json(login).send().await?;
match res.status() {
s if s.is_success() => {
success("Login successful")?;
Expand Down
4 changes: 2 additions & 2 deletions src/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
auth::get_token,
errors::{AuthError, PublishError},
registry::get_project_id,
utils::{get_base_url, read_file},
utils::{api_url, read_file},
};
use cliclack::log::{info, remark, success};
use ignore::{WalkBuilder, WalkState};
Expand Down Expand Up @@ -147,7 +147,7 @@ async fn push_to_repo(
let token = get_token()?;
let client = Client::new();

let url = format!("{}/api/v1/revision/upload", get_base_url());
let url = api_url("revision/upload", &[]);

let mut headers: HeaderMap = HeaderMap::new();

Expand Down
27 changes: 10 additions & 17 deletions src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
config::{Dependency, HttpDependency},
errors::RegistryError,
utils::get_base_url,
utils::api_url,
};
use chrono::{DateTime, Utc};
use semver::{Version, VersionReq};
Expand Down Expand Up @@ -49,12 +49,9 @@ pub struct ProjectResponse {
}

pub async fn get_dependency_url_remote(dependency: &Dependency, version: &str) -> Result<String> {
let url = format!(
"{}/api/v1/revision-cli?project_name={}&revision={}",
get_base_url(),
dependency.name(),
version
);
let url =
api_url("revision-cli", &[("project_name", dependency.name()), ("revision", version)]);

let res = reqwest::get(url).await?;
let res = res.error_for_status()?;
let revision: RevisionResponse = res.json().await?;
Expand All @@ -65,7 +62,7 @@ pub async fn get_dependency_url_remote(dependency: &Dependency, version: &str) -
}

pub async fn get_project_id(dependency_name: &str) -> Result<String> {
let url = format!("{}/api/v1/project?project_name={}", get_base_url(), dependency_name);
let url = api_url("project", &[("project_name", dependency_name)]);
let res = reqwest::get(url).await?;
let res = res.error_for_status()?;
let project: ProjectResponse = res.json().await?;
Expand All @@ -77,11 +74,8 @@ pub async fn get_project_id(dependency_name: &str) -> Result<String> {

pub async fn get_latest_forge_std() -> Result<Dependency> {
let dependency_name = "forge-std";
let url = format!(
"{}/api/v1/revision?project_name={}&offset=0&limit=1",
get_base_url(),
dependency_name
);
let url =
api_url("revision", &[("project_name", dependency_name), ("offset", "0"), ("limit", "1")]);
let res = reqwest::get(url).await?;
let res = res.error_for_status()?;
let revision: RevisionResponse = res.json().await?;
Expand All @@ -106,10 +100,9 @@ pub enum Versions {
pub async fn get_all_versions_descending(dependency_name: &str) -> Result<Versions> {
// TODO: provide a more efficient endpoint which already sorts by descending semver if possible
// and only returns the version strings
let url = format!(
"{}/api/v1/revision?project_name={}&offset=0&limit=10000",
get_base_url(),
dependency_name
let url = api_url(
"revision",
&[("project_name", dependency_name), ("offset", "0"), ("limit", "10000")],
);
let res = reqwest::get(url).await?;
let res = res.error_for_status()?;
Expand Down
46 changes: 25 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::{
use ignore::{WalkBuilder, WalkState};
use once_cell::sync::Lazy;
use regex::Regex;
use reqwest::Url;
use sha2::{Digest, Sha256};
use simple_home_dir::home_dir;
use std::{
borrow::Cow,
env,
ffi::OsStr,
fs,
Expand All @@ -25,6 +27,17 @@ static GIT_SSH_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(?:git@github\.com|git@gitlab)").expect("git ssh regex should compile")
});

pub static API_BASE_URL: Lazy<Url> = Lazy::new(|| {
let url = env::var("SOLDEER_API_URL").unwrap_or("https://api.soldeer.xyz".to_string());
Url::parse(&url).expect("SOLDEER_API_URL is invalid")
});

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum UrlType {
Git,
Http,
}

/// Get the current working directory
pub fn get_current_working_dir() -> PathBuf {
env::current_dir().expect("could not get current working directory")
Expand Down Expand Up @@ -52,15 +65,9 @@ pub fn read_file(path: impl AsRef<Path>) -> Result<Vec<u8>, std::io::Error> {
/// setting the `SOLDEER_LOGIN_FILE` environment variable.
/// For login, the custom path will only be used if the file already exists.
pub fn security_file_path() -> Result<PathBuf, std::io::Error> {
let custom_security_file = if cfg!(test) {
return Ok(PathBuf::from("./test_save_jwt"));
} else {
env::var("SOLDEER_LOGIN_FILE").ok()
};

if let Some(file) = custom_security_file {
if !file.is_empty() && Path::new(&file).exists() {
return Ok(file.into());
if let Ok(file_path) = env::var("SOLDEER_LOGIN_FILE") {
if !file_path.is_empty() && Path::new(&file_path).exists() {
return Ok(file_path.into());
}
}

Expand All @@ -74,25 +81,18 @@ pub fn security_file_path() -> Result<PathBuf, std::io::Error> {
Ok(security_file)
}

pub fn get_base_url() -> String {
if cfg!(test) {
env::var("base_url").unwrap_or("http://0.0.0.0".to_string())
} else {
"https://api.soldeer.xyz".to_string()
}
pub fn api_url(path: &str, params: &[(&str, &str)]) -> Url {
let mut url = API_BASE_URL.clone();
url.set_path(&format!("api/v1/{path}"));
url.query_pairs_mut().extend_pairs(params.iter());
url
}

/// Check if any file starts with a period
pub fn check_dotfiles(files: &[PathBuf]) -> bool {
files.iter().any(|file| file.file_name().unwrap_or_default().to_string_lossy().starts_with('.'))
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum UrlType {
Git,
Http,
}

pub fn get_url_type(dependency_url: &str) -> Result<UrlType, DownloadError> {
if GIT_SSH_REGEX.is_match(dependency_url) {
return Ok(UrlType::Git);
Expand Down Expand Up @@ -262,6 +262,10 @@ where
Ok(String::from_utf8(forge.stdout).expect("forge command output should be valid utf-8"))
}

fn get_api_base_url() -> String {
env::var("SOLDEER_API_URL").unwrap_or("https://api.soldeer.xyz".to_string())
}

#[cfg(test)]
mod tests {
use rand::{distributions::Alphanumeric, Rng as _};
Expand Down

0 comments on commit ea196fe

Please sign in to comment.