Skip to content

Expose requests module to library users for _with_options requests like add_with_options (fixes #26) #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions ipfs-api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl IpfsClient {
}

impl IpfsClient {
/// Add file to Ipfs.
/// Add a file to IPFS.
///
/// # Examples
///
Expand All @@ -470,7 +470,38 @@ impl IpfsClient {

form.add_reader("path", data);

self.request(&request::Add, Some(form))
self.request(&request::Add::default(), Some(form))
}

/// Add a file to IPFS with options.
///
/// # Examples
///
/// ```no_run
/// # extern crate ipfs_api;
/// #
/// use ipfs_api::IpfsClient;
/// use std::io::Cursor;
///
/// # fn main() {
/// let client = IpfsClient::default();
/// let data = Cursor::new("Hello World!");
/// let mut add = ipfs_api::request::Add::default();
/// add.chunker = Some("rabin-512-1024-2048");
/// let req = client.add_with_options(data, &add);
/// # }
/// ```
///
#[inline]
pub fn add_with_options<R>(&self, data: R, add: &request::Add) -> AsyncResponse<response::AddResponse>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this take in B: Borrow<request::Add> instead to allow it to take an owned request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good idea.

where
R: 'static + Read + Send,
{
let mut form = multipart::Form::default();

form.add_reader("path", data);

self.request(add, Some(form))
}

/// Add a path to Ipfs. Can be a file or directory.
Expand Down Expand Up @@ -546,7 +577,7 @@ impl IpfsClient {
}

Box::new(
self.request_stream_json(&request::Add, Some(form))
self.request_stream_json(&request::Add::default(), Some(form))
.collect()
.map(|mut responses: Vec<response::AddResponse>| responses.pop().unwrap()),
)
Expand Down
2 changes: 1 addition & 1 deletion ipfs-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,5 @@ pub use request::{KeyType, Logger, LoggingLevel, ObjectTemplate};
mod client;
mod header;
mod read;
mod request;
pub mod request;
pub mod response;
38 changes: 35 additions & 3 deletions ipfs-api/src/request/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,43 @@
use http::Method;
use request::ApiRequest;

pub struct Add;
#[derive(Serialize)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can derive Default to save some typing!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point -- I completely forgot that the default for options is none when writing this.

#[serde(rename_all = "kebab-case")]
pub struct Add<'a, 'b> {
pub recursive: Option<bool>,
pub progress: Option<bool>,
pub trickle: Option<bool>,
pub only_hash: Option<bool>,
pub chunker: Option<&'a str>,
pub pin: Option<bool>,
pub raw_leaves: Option<bool>,
pub fscache: Option<bool>,
pub cid_version: Option<isize>,
pub hash: Option<&'b str>,
pub inline: Option<bool>,
pub inline_limit: Option<isize>
}

impl_skip_serialize!(Add);
impl<'a,'b> Default for Add<'a,'b> {
fn default() -> Self {
Self {
recursive: None,
progress: None,
trickle: None,
only_hash: None,
chunker: None,
pin: None,
raw_leaves: None,
fscache: None,
cid_version: None,
hash: None,
inline: None,
inline_limit: None,
}
}
}

impl ApiRequest for Add {
impl<'a, 'b> ApiRequest for Add<'a,'b> {
const PATH: &'static str = "/add";

const METHOD: &'static Method = &Method::POST;
Expand Down