#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Host {
hostname: String,
api_base: String,
}
impl Host {
#[must_use]
pub fn new(hostname: &str) -> Self {
let api_base = if hostname == "github.com" {
"https://api.github.com".to_string()
} else {
format!("https://{hostname}/api/v3")
};
Self {
hostname: hostname.to_string(),
api_base,
}
}
#[must_use]
pub fn hostname(&self) -> &str {
&self.hostname
}
#[must_use]
pub fn api_base(&self) -> &str {
&self.api_base
}
#[must_use]
pub fn api_url(&self, path: &str) -> String {
format!("{}{path}", self.api_base)
}
#[must_use]
pub fn device_code_url(&self) -> String {
format!("https://{}/login/device/code", self.hostname)
}
#[must_use]
pub fn access_token_url(&self) -> String {
format!("https://{}/login/oauth/access_token", self.hostname)
}
#[must_use]
pub fn device_activation_url(&self) -> String {
format!("https://{}/login/device", self.hostname)
}
}
impl Default for Host {
fn default() -> Self {
Self::new("github.com")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_github_dot_com() {
let host = Host::default();
assert_eq!(host.hostname(), "github.com");
assert_eq!(host.api_base(), "https://api.github.com");
}
#[test]
fn github_api_url() {
let host = Host::new("github.com");
assert_eq!(host.api_url("/user"), "https://api.github.com/user");
assert_eq!(
host.api_url("/repos/owner/repo"),
"https://api.github.com/repos/owner/repo"
);
}
#[test]
fn ghes_api_url() {
let host = Host::new("git.example.com");
assert_eq!(host.api_base(), "https://git.example.com/api/v3");
assert_eq!(host.api_url("/user"), "https://git.example.com/api/v3/user");
}
#[test]
fn device_code_url_github() {
let host = Host::new("github.com");
assert_eq!(
host.device_code_url(),
"https://github.com/login/device/code"
);
}
#[test]
fn device_code_url_ghes() {
let host = Host::new("git.example.com");
assert_eq!(
host.device_code_url(),
"https://git.example.com/login/device/code"
);
}
#[test]
fn access_token_url_github() {
let host = Host::new("github.com");
assert_eq!(
host.access_token_url(),
"https://github.com/login/oauth/access_token"
);
}
#[test]
fn device_activation_url() {
let host = Host::new("github.com");
assert_eq!(
host.device_activation_url(),
"https://github.com/login/device"
);
}
}