From 648517312fd0aa754c938abde66af038e2628c13 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Jan 2018 20:49:08 -0500 Subject: add /object/new and /object/data --- ipfs-api/src/client.rs | 46 +++++++++++++++++++++++++++++++++++++++-- ipfs-api/src/lib.rs | 2 +- ipfs-api/src/request/object.rs | 37 +++++++++++++++++++++++++++++++++ ipfs-api/src/response/object.rs | 2 -- 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 { + 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, + ) -> AsyncResponse { + 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(&self, serializer: S) -> Result + 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, +} + +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; - #[derive(Debug, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct ObjectDiff { -- cgit v1.2.3