sharry::api::Uri handling

This commit is contained in:
Jörn-Michael Miehe 2025-06-04 22:01:17 +00:00
parent fd79455de4
commit f8a748ec07
2 changed files with 10 additions and 16 deletions

View file

@ -8,7 +8,7 @@ use super::api::Uri;
#[derive(Serialize, Deserialize, Debug, Hash)]
pub struct Alias {
pub(super) api_uri: String,
pub(super) uri: Uri,
pub(super) id: String,
}
@ -23,15 +23,12 @@ impl<B> SharryAlias for RequestBuilder<B> {
}
impl Alias {
pub fn new(api_uri: Uri, id: impl Into<String>) -> Self {
Self {
api_uri: api_uri.into(),
id: id.into(),
}
pub fn new(uri: Uri, id: impl Into<String>) -> Self {
Self { uri, id: id.into() }
}
pub(super) fn get_endpoint(&self, endpoint: impl Display + Debug) -> String {
let uri = format!("{}/{}", self.api_uri, endpoint);
let uri = format!("{}/{}", self.uri.to_string(), endpoint);
debug!("endpoint uri: {uri:?}");
uri

View file

@ -1,5 +1,8 @@
use std::fmt::Display;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Hash)]
pub struct Uri {
protocol: String,
base_url: String,
@ -14,15 +17,9 @@ impl Uri {
}
}
impl From<&str> for Uri {
fn from(value: &str) -> Self {
Self::with_protocol("https", value)
}
}
impl From<Uri> for String {
fn from(val: Uri) -> Self {
format!("{}://{}/api/v2", val.protocol, val.base_url)
impl Display for Uri {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}://{}/api/v2", self.protocol, self.base_url)
}
}