summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerris Tseng <ferristseng@fastmail.fm>2018-01-23 20:49:08 -0500
committerFerris Tseng <ferristseng@fastmail.fm>2018-01-23 20:49:08 -0500
commit648517312fd0aa754c938abde66af038e2628c13 (patch)
tree2af6f572a8508882b8aa5adc6daa26552f7c289c
parent1661d3e4a90ea2797b3b0485c9396b0b79473e0c (diff)
add /object/new and /object/data
-rw-r--r--ipfs-api/src/client.rs46
-rw-r--r--ipfs-api/src/lib.rs2
-rw-r--r--ipfs-api/src/request/object.rs37
-rw-r--r--ipfs-api/src/response/object.rs2
4 files changed, 82 insertions, 5 deletions
diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs
index 7a4fbed..5b02f95 100644
--- a/ipfs-api/src/client.rs
+++ b/ipfs-api/src/client.rs
@@ -1654,7 +1654,26 @@ impl IpfsClient {
)
}
- // TODO /object/data
+ /// Output the raw bytes of an Ipfs object.
+ ///
+ /// ```no_run
+ /// # extern crate ipfs_api;
+ /// # extern crate tokio_core;
+ /// #
+ /// use ipfs_api::IpfsClient;
+ /// use tokio_core::reactor::Core;
+ ///
+ /// # fn main() {
+ /// let mut core = Core::new().unwrap();
+ /// let client = IpfsClient::default(&core.handle());
+ /// let req = client.object_data("/ipfs/QmVrLsEDn27sScp3k23sgZNefVTjSAL3wpgW1iWPi4MgoY");
+ /// # }
+ /// ```
+ ///
+ #[inline]
+ pub fn object_data(&self, key: &str) -> AsyncStreamResponse<Chunk> {
+ self.request_stream_bytes(&request::ObjectData { key }, None)
+ }
/// Returns the diff of two Ipfs objects.
///
@@ -1725,7 +1744,30 @@ impl IpfsClient {
self.request(&request::ObjectLinks { key }, None)
}
- // TODO /object/new
+ /// Create a new object.
+ ///
+ /// ```no_run
+ /// # extern crate ipfs_api;
+ /// # extern crate tokio_core;
+ /// #
+ /// use ipfs_api::{IpfsClient, ObjectTemplate};
+ /// use tokio_core::reactor::Core;
+ ///
+ /// # fn main() {
+ /// let mut core = Core::new().unwrap();
+ /// let client = IpfsClient::default(&core.handle());
+ /// let req = client.object_new(None);
+ /// let req = client.object_new(Some(ObjectTemplate::UnixFsDir));
+ /// # }
+ /// ```
+ ///
+ #[inline]
+ pub fn object_new(
+ &self,
+ template: Option<request::ObjectTemplate>,
+ ) -> AsyncResponse<response::ObjectNewResponse> {
+ self.request(&request::ObjectNew { template }, None)
+ }
// TODO /object/patch/add-link
diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs
index 47a84cc..db63b35 100644
--- a/ipfs-api/src/lib.rs
+++ b/ipfs-api/src/lib.rs
@@ -88,7 +88,7 @@ extern crate tokio_core;
extern crate tokio_io;
pub use client::IpfsClient;
-pub use request::{KeyType, Logger, LoggingLevel};
+pub use request::{KeyType, Logger, LoggingLevel, ObjectTemplate};
mod request;
pub mod response;
diff --git a/ipfs-api/src/request/object.rs b/ipfs-api/src/request/object.rs
index a13cfaa..acfab61 100644
--- a/ipfs-api/src/request/object.rs
+++ b/ipfs-api/src/request/object.rs
@@ -7,6 +7,16 @@
//
use request::ApiRequest;
+use serde::ser::{Serialize, Serializer};
+
+#[derive(Serialize)]
+pub struct ObjectData<'a> {
+ #[serde(rename = "arg")] pub key: &'a str,
+}
+
+impl<'a> ApiRequest for ObjectData<'a> {
+ const PATH: &'static str = "/object/data";
+}
#[derive(Serialize)]
pub struct ObjectDiff<'a> {
@@ -37,6 +47,33 @@ impl<'a> ApiRequest for ObjectLinks<'a> {
const PATH: &'static str = "/object/links";
}
+#[derive(Copy, Clone)]
+pub enum ObjectTemplate {
+ UnixFsDir,
+}
+
+impl Serialize for ObjectTemplate {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ let s = match self {
+ &ObjectTemplate::UnixFsDir => "unixfs-dir",
+ };
+
+ serializer.serialize_str(s)
+ }
+}
+
+#[derive(Serialize)]
+pub struct ObjectNew {
+ #[serde(rename = "arg")] pub template: Option<ObjectTemplate>,
+}
+
+impl ApiRequest for ObjectNew {
+ const PATH: &'static str = "/object/new";
+}
+
#[derive(Serialize)]
pub struct ObjectStat<'a> {
#[serde(rename = "arg")] pub key: &'a str,
diff --git a/ipfs-api/src/response/object.rs b/ipfs-api/src/response/object.rs
index 78fd271..5bfecb0 100644
--- a/ipfs-api/src/response/object.rs
+++ b/ipfs-api/src/response/object.rs
@@ -9,8 +9,6 @@
use response::{serde, IpfsHeader};
use std::collections::HashMap;
-pub type ObjectDataResponse = Vec<u8>;
-
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ObjectDiff {