ipfs_api_prelude/request/
mod.rs

1// Copyright 2017 rust-ipfs-api Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7//
8
9pub use self::add::*;
10pub use self::bitswap::*;
11pub use self::block::*;
12pub use self::bootstrap::*;
13pub use self::cat::*;
14pub use self::commands::*;
15pub use self::config::*;
16pub use self::dag::*;
17pub use self::dht::*;
18pub use self::diag::*;
19pub use self::dns::*;
20pub use self::file::*;
21pub use self::files::*;
22pub use self::filestore::*;
23pub use self::get::*;
24pub use self::id::*;
25pub use self::key::*;
26pub use self::log::*;
27pub use self::ls::*;
28pub use self::name::*;
29pub use self::object::*;
30pub use self::pin::*;
31pub use self::ping::*;
32pub use self::pubsub::*;
33pub use self::refs::*;
34pub use self::shutdown::*;
35pub use self::stats::*;
36pub use self::swarm::*;
37pub use self::swarm_connect::*;
38pub use self::tar::*;
39pub use self::version::*;
40
41/// Create a test to verify that serializing a `ApiRequest` returns the expected
42/// url encoded string.
43///
44#[cfg(test)]
45macro_rules! serialize_url_test {
46    ($f: ident, $obj: expr, $exp: expr) => {
47        #[test]
48        fn $f() {
49            assert_eq!(::serde_urlencoded::to_string($obj), Ok($exp.to_string()))
50        }
51    };
52}
53
54/// Implements the `Serialize` trait for types that do not require
55/// serialization. This provides a workaround for a limitation in
56/// `serde_urlencoded`, that prevents unit structs from being serialized.
57///
58macro_rules! impl_skip_serialize {
59    ($ty: ty) => {
60        impl ::serde::Serialize for $ty {
61            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
62            where
63                S: ::serde::Serializer,
64            {
65                serializer.serialize_none()
66            }
67        }
68    };
69}
70
71mod add;
72mod bitswap;
73mod block;
74mod bootstrap;
75mod cat;
76mod commands;
77mod config;
78mod dag;
79mod dht;
80mod diag;
81mod dns;
82mod file;
83mod files;
84mod filestore;
85mod get;
86mod id;
87mod key;
88mod log;
89mod ls;
90mod name;
91mod object;
92mod pin;
93mod ping;
94mod pubsub;
95mod refs;
96mod shutdown;
97mod stats;
98mod swarm;
99mod swarm_connect;
100mod tar;
101mod version;
102
103use http::uri::Uri;
104use serde::Serialize;
105
106/// A request that can be made against the Ipfs API.
107///
108pub trait ApiRequest: Serialize + Send {
109    /// Returns the API path that this request can be called on.
110    ///
111    /// All paths should begin with '/'.
112    ///
113    const PATH: &'static str;
114
115    /// Method used to make the request.
116    ///
117    const METHOD: http::Method = http::Method::POST;
118
119    /// Creates the absolute URL for an API resource given the base path
120    /// of the service.
121    ///
122    fn absolute_url(&self, base: &Uri) -> Result<Uri, crate::Error> {
123        format!(
124            "{}{}?{}",
125            base,
126            Self::PATH,
127            serde_urlencoded::to_string(self)?
128        )
129        .parse()
130        .map_err(crate::Error::from)
131    }
132}