-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement rust client SDK APIs
- Loading branch information
Showing
5 changed files
with
331 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use candid::{ | ||
utils::{encode_args, ArgumentEncoder}, | ||
CandidType, Decode, Principal, | ||
}; | ||
use ic_agent::{identity::Identity, Agent}; | ||
use ic_oss_types::format_error; | ||
|
||
pub async fn build_agent(host: &str, identity: Box<dyn Identity>) -> Result<Agent, String> { | ||
let agent = Agent::builder() | ||
.with_url(host) | ||
.with_boxed_identity(identity) | ||
.with_verify_query_signatures(true) | ||
.build() | ||
.map_err(format_error)?; | ||
if host.starts_with("http://") { | ||
agent.fetch_root_key().await.map_err(format_error)?; | ||
} | ||
|
||
Ok(agent) | ||
} | ||
|
||
pub async fn update_call<In, Out>( | ||
agent: &Agent, | ||
canister_id: &Principal, | ||
method_name: &str, | ||
args: In, | ||
) -> Result<Out, String> | ||
where | ||
In: ArgumentEncoder + Send, | ||
Out: CandidType + for<'a> candid::Deserialize<'a>, | ||
{ | ||
let input = encode_args(args).map_err(format_error)?; | ||
let res = agent | ||
.update(canister_id, method_name) | ||
.with_arg(input) | ||
.call_and_wait() | ||
.await | ||
.map_err(format_error)?; | ||
let output = Decode!(res.as_slice(), Out).map_err(format_error)?; | ||
Ok(output) | ||
} | ||
|
||
pub async fn query_call<In, Out>( | ||
agent: &Agent, | ||
canister_id: &Principal, | ||
method_name: &str, | ||
args: In, | ||
) -> Result<Out, String> | ||
where | ||
In: ArgumentEncoder + Send, | ||
Out: CandidType + for<'a> candid::Deserialize<'a>, | ||
{ | ||
let input = encode_args(args).map_err(format_error)?; | ||
let res = agent | ||
.query(canister_id, method_name) | ||
.with_arg(input) | ||
.call() | ||
.await | ||
.map_err(format_error)?; | ||
let output = Decode!(res.as_slice(), Out).map_err(format_error)?; | ||
Ok(output) | ||
} |
Oops, something went wrong.