#[derive(Debug, Default, Clone)]
pub struct Config {
pub protocol: String,
pub host: String,
pub port: u32,
pub version: String,
pub content_type: String,
pub authorization: Authorization,
pub request_timeout: u32,
}
impl Config {
pub fn new(auth: &str) -> Config {
let authorization = Authorization::new(auth);
Config {
protocol: "https".into(),
host: "api.vika.cn".into(),
port: 443,
version: "v1".into(),
content_type: "application/json".into(),
authorization,
request_timeout: 60000,
}
}
pub fn set_protocol(&mut self, protocol: &str) {
self.protocol = protocol.into();
}
pub fn set_host(&mut self, host: &str) {
self.host = host.into();
}
pub fn set_port(&mut self, port: u32) {
self.port = port;
}
pub fn set_version(&mut self, version: &str) {
self.version = version.into();
}
pub fn set_content_type(&mut self, content_type: &str) {
self.content_type = content_type.into();
}
pub fn set_request_timeout(&mut self, request_timeout: u32) {
self.request_timeout = request_timeout;
}
pub fn set_authorization(&mut self, authorization: &str) {
self.authorization = Authorization::new(authorization);
}
pub fn set_authorization_token(&mut self, token: &str) {
self.authorization.token = token.into();
}
pub fn get_url(&self) -> String {
format!(
"{}://{}:{}/fusion/{}",
self.protocol, self.host, self.port, self.version
)
}
}
#[derive(Debug, Default, Clone)]
pub struct Authorization {
r#type: String,
token: String,
}
impl Authorization {
pub fn new(token: &str) -> Self {
Self {
r#type: "Bearer".into(),
token: token.into(),
}
}
}
impl From<&str> for Authorization {
fn from(token: &str) -> Self {
Self::new(token)
}
}
impl Into<String> for Authorization {
fn into(self) -> String {
format!("{} {}", self.r#type, self.token)
}
}
impl std::fmt::Display for Authorization {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.r#type, self.token)
}
}