rspotify_http/
common.rs

1use std::collections::HashMap;
2use std::fmt;
3
4use maybe_async::maybe_async;
5use serde_json::Value;
6
7pub type Headers = HashMap<String, String>;
8pub type Query<'a> = HashMap<&'a str, &'a str>;
9pub type Form<'a> = HashMap<&'a str, &'a str>;
10
11/// This trait represents the interface to be implemented for an HTTP client,
12/// which is kept separate from the Spotify client for cleaner code. Thus, it
13/// also requires other basic traits that are needed for the Spotify client.
14///
15/// When a request doesn't need to pass parameters, the empty or default value
16/// of the payload type should be passed, like `json!({})` or `Query::new()`.
17/// This avoids using `Option<T>` because `Value` itself may be null in other
18/// different ways (`Value::Null`, an empty `Value::Object`...), so this removes
19/// redundancy and edge cases (a `Some(Value::Null), for example, doesn't make
20/// much sense).
21#[cfg_attr(target_arch = "wasm32", maybe_async(?Send))]
22#[cfg_attr(not(target_arch = "wasm32"), maybe_async)]
23pub trait BaseHttpClient: Send + Default + Clone + fmt::Debug {
24    type Error;
25
26    // This internal function should always be given an object value in JSON.
27    async fn get(
28        &self,
29        url: &str,
30        headers: Option<&Headers>,
31        payload: &Query,
32    ) -> Result<String, Self::Error>;
33
34    async fn post(
35        &self,
36        url: &str,
37        headers: Option<&Headers>,
38        payload: &Value,
39    ) -> Result<String, Self::Error>;
40
41    async fn post_form(
42        &self,
43        url: &str,
44        headers: Option<&Headers>,
45        payload: &Form<'_>,
46    ) -> Result<String, Self::Error>;
47
48    async fn put(
49        &self,
50        url: &str,
51        headers: Option<&Headers>,
52        payload: &Value,
53    ) -> Result<String, Self::Error>;
54
55    async fn delete(
56        &self,
57        url: &str,
58        headers: Option<&Headers>,
59        payload: &Value,
60    ) -> Result<String, Self::Error>;
61}