use crate::error::OxenError;
use crate::model::{Remote, RemoteRepository, RepoNew};
use url::Url;
pub const API_NAMESPACE: &str = "/api/repos";
pub fn get_scheme(host: impl AsRef<str>) -> String {
RepoNew::scheme_default(host.as_ref())
}
pub fn url_from_host_and_scheme(
host: impl AsRef<str>,
uri: impl AsRef<str>,
scheme: impl AsRef<str>,
) -> String {
format!(
"{}://{}{API_NAMESPACE}{}",
scheme.as_ref(),
host.as_ref(),
uri.as_ref()
)
}
pub fn url_from_host(host: &str, uri: &str) -> String {
format!("{}://{host}{API_NAMESPACE}{uri}", get_scheme(host))
}
pub fn remote_url_from_namespace_name(host: &str, namespace: &str, name: &str) -> String {
format!("{}://{host}/{namespace}/{name}", get_scheme(host))
}
pub fn remote_url_from_namespace_name_scheme(
host: &str,
namespace: &str,
name: &str,
scheme: &str,
) -> String {
format!("{scheme}://{host}/{namespace}/{name}")
}
pub fn remote_url_from_name(host: &str, name: &str) -> String {
format!("{}://{host}/{name}", get_scheme(host))
}
pub fn remote_url_from_name_and_scheme(host: &str, name: &str, scheme: &str) -> String {
format!("{scheme}://{host}/{name}")
}
pub fn url_from_remote(remote: &Remote, uri: &str) -> Result<String, OxenError> {
match Url::parse(&remote.url) {
Ok(mut parsed_url) => {
let new_path = format!("{}{}{}", API_NAMESPACE, parsed_url.path(), uri);
parsed_url.set_path("");
let mut remote_url = parsed_url.to_string();
remote_url.pop(); Ok(format!("{remote_url}{new_path}"))
}
Err(e) => {
log::warn!("Invalid remote url: {:?}\n{:?}", remote.url, e);
Err(OxenError::invalid_set_remote_url(&remote.url))
}
}
}
pub fn url_from_repo(repo: &RemoteRepository, uri: &str) -> Result<String, OxenError> {
url_from_remote(&repo.remote, uri)
}