izihawa_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::tar::*;
38pub use self::version::*;
39
40/// Create a test to verify that serializing a `ApiRequest` returns the expected
41/// url encoded string.
42///
43#[cfg(test)]
44macro_rules! serialize_url_test {
45    ($f: ident, $obj: expr, $exp: expr) => {
46        #[test]
47        fn $f() {
48            assert_eq!(::serde_urlencoded::to_string($obj), Ok($exp.to_string()))
49        }
50    };
51}
52
53/// Implements the `Serialize` trait for types that do not require
54/// serialization. This provides a workaround for a limitation in
55/// `serde_urlencoded`, that prevents unit structs from being serialized.
56///
57macro_rules! impl_skip_serialize {
58    ($ty: ty) => {
59        impl ::serde::Serialize for $ty {
60            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
61            where
62                S: ::serde::Serializer,
63            {
64                serializer.serialize_none()
65            }
66        }
67    };
68}
69
70mod add;
71mod bitswap;
72mod block;
73mod bootstrap;
74mod cat;
75mod commands;
76mod config;
77mod dag;
78mod dht;
79mod diag;
80mod dns;
81mod file;
82mod files;
83mod filestore;
84mod get;
85mod id;
86mod key;
87mod log;
88mod ls;
89mod name;
90mod object;
91mod pin;
92mod ping;
93mod pubsub;
94mod refs;
95mod shutdown;
96mod stats;
97mod swarm;
98mod tar;
99mod version;
100
101use http::uri::Uri;
102use serde::Serialize;
103
104/// A request that can be made against the Ipfs API.
105///
106pub trait ApiRequest: Serialize + Send {
107    /// Returns the API path that this request can be called on.
108    ///
109    /// All paths should begin with '/'.
110    ///
111    const PATH: &'static str;
112
113    /// Method used to make the request.
114    ///
115    const METHOD: http::Method = http::Method::POST;
116
117    /// Creates the absolute URL for an API resource given the base path
118    /// of the service.
119    ///
120    fn absolute_url(&self, base: &Uri) -> Result<Uri, crate::Error> {
121        format!(
122            "{}{}?{}",
123            base,
124            Self::PATH,
125            serde_urlencoded::to_string(self)?
126        )
127        .parse()
128        .map_err(crate::Error::from)
129    }
130}