reqwest_enum/
target.rs

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