1use crate::http::{AuthMethod, HTTPBody, HTTPMethod};
2use std::{collections::HashMap, time::Duration};
3
4pub trait Target {
5 fn base_url(&self) -> &'static str;
6 fn method(&self) -> HTTPMethod;
7 fn path(&self) -> String;
8 fn query(&self) -> HashMap<&'static str, &'static str>;
9 fn headers(&self) -> HashMap<&'static str, &'static str>;
10 fn authentication(&self) -> Option<AuthMethod>;
11 fn body(&self) -> HTTPBody;
12 fn timeout(&self) -> Option<Duration>;
13
14 fn query_string(&self) -> String {
16 self.query()
17 .iter()
18 .map(|(k, v)| format!("{}={}", k, v))
19 .collect::<Vec<_>>()
20 .join("&")
21 }
22
23 fn absolute_url(&self) -> String {
24 let mut url = format!("{}{}", self.base_url(), self.path());
25 if !self.query_string().is_empty() {
26 url = format!("{}?{}", url, self.query_string());
27 }
28 url
29 }
30}
31
32#[cfg(feature = "jsonrpc")]
33pub trait JsonRpcTarget: Target {
34 fn method_name(&self) -> &'static str;
35 fn params(&self) -> Vec<serde_json::Value>;
36}