Skip to content
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

feat: add http_request_next method (large asset support) #129

Merged
merged 7 commits into from
Mar 19, 2021
Merged
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
31 changes: 31 additions & 0 deletions ic-utils/src/canister.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::call::{AsyncCaller, SyncCaller};
use candid::de::ArgumentDecoder;
use candid::parser::value::IDLValue;
use candid::ser::IDLBuilder;
use candid::CandidType;
use ic_agent::{Agent, AgentError};
Expand Down Expand Up @@ -192,6 +193,25 @@ impl Argument {
}
}

/// Add an IDLValue Argument. If the current value of Argument is Raw, will set the
/// result to an error. If the current value is an error, will do nothing.
pub fn push_value_arg(&mut self, arg: IDLValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IDLValue implements CandidType, so you should be able to use push_idl_arg with an IDLValue. Was there any issue with using that other function?

Copy link
Member Author

Choose a reason for hiding this comment

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

match self.0 {
Ok(ArgumentType::Idl(ref mut idl_builder)) => {
let result = idl_builder.value_arg(&arg);
if let Err(e) = result {
self.0 = Err(AgentError::CandidError(Box::new(e)))
}
}
Ok(ArgumentType::Raw(_)) => {
self.0 = Err(AgentError::MessageError(
"Cannot overwrite a Raw Argument with a non-raw argument.".to_owned(),
))
}
_ => {}
}
}

/// Set the argument as raw, replacing any value that was there before. If the
/// current argument was an error, does nothing.
pub fn set_raw_arg(&mut self, arg: Vec<u8>) {
Expand Down Expand Up @@ -258,6 +278,17 @@ impl<'agent, 'canister: 'agent, Interface> SyncCallBuilder<'agent, 'canister, In
self
}

/// Add an argument to the candid argument list. This requires Candid arguments, if
/// there is a raw argument set (using [with_arg_raw]), this will fail.
/// TODO: make this method unnecessary https://github.com/dfinity/agent-rs/issues/132
pub fn with_value_arg(
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above.

mut self,
arg: IDLValue,
) -> SyncCallBuilder<'agent, 'canister, Interface> {
self.arg.push_value_arg(arg);
self
}

/// Replace the argument with raw argument bytes. This will overwrite the current
/// argument set, so calling this method twice will discard the first argument.
pub fn with_arg_raw(mut self, arg: Vec<u8>) -> SyncCallBuilder<'agent, 'canister, Interface> {
Expand Down
20 changes: 19 additions & 1 deletion ic-utils/src/interfaces/http_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::call::SyncCall;
use crate::canister::CanisterBuilder;
use crate::Canister;
use candid::parser::value::IDLValue;
use candid::{CandidType, Deserialize};
use ic_agent::export::Principal;
use ic_agent::Agent;
Expand All @@ -9,7 +10,7 @@ use std::fmt::Debug;
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct HttpRequestCanister;

#[derive(CandidType, Deserialize)]
#[derive(CandidType, Clone, Deserialize)]
pub struct HeaderField(pub String, pub String);

#[derive(CandidType, Deserialize)]
Expand All @@ -27,6 +28,14 @@ pub struct HttpResponse {
pub headers: Vec<HeaderField>,
#[serde(with = "serde_bytes")]
pub body: Vec<u8>,
pub next_token: Option<IDLValue>,
}

#[derive(CandidType, Deserialize)]
pub struct NextHttpResponse {
#[serde(with = "serde_bytes")]
pub body: Vec<u8>,
pub next_token: Option<IDLValue>,
}

impl HttpRequestCanister {
Expand Down Expand Up @@ -67,4 +76,13 @@ impl<'agent> Canister<'agent, HttpRequestCanister> {
})
.build()
}

pub fn http_request_next<'canister: 'agent>(
&'canister self,
token: IDLValue,
) -> impl 'agent + SyncCall<(NextHttpResponse,)> {
self.query_("http_request_next")
.with_value_arg(token)
.build()
}
}