Skip to content

Commit

Permalink
chore: BREAKING CHANGE: Update for HTTPS Outcalls transform function …
Browse files Browse the repository at this point in the history
…with context. (#326)

* Update for HTTPS Outcalls transform function with context.

* fix errors

* another fix

* working

* update changelog

* fix test

* bump dfx for examples

* changelog

Co-authored-by: Linwei Shang <linwei.shang@dfinity.org>
  • Loading branch information
qj-yu and lwshang authored Nov 4, 2022
1 parent 4610c4f commit 14267d8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:

env:
rust-version: 1.60.0
dfx-version: 0.12.0-beta.3
dfx-version: 0.12.0-beta.6

jobs:
test:
Expand Down
6 changes: 3 additions & 3 deletions examples/management_canister/src/caller/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ mod http_request {
method: HttpMethod::GET,
headers: vec![],
body: None,
transform: Some(TransformType::from_transform_function(transform)),
transform: Some(TransformContext::new(transform, vec![])),
};
let response = http_request(arg).await.unwrap().0;
assert_eq!(response.status, 200);
Expand All @@ -110,13 +110,13 @@ mod http_request {

// transform function must be a *query* method of the canister
#[query]
fn transform(arg: HttpResponse) -> HttpResponse {
fn transform(arg: TransformArgs) -> HttpResponse {
HttpResponse {
headers: vec![HttpHeader {
name: "custom-header".to_string(),
value: "test".to_string(),
}],
..arg
..arg.response
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ic-cdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ic-cdk-macros"
version = "0.6.4"
version = "0.6.5"
authors = ["DFINITY Stiftung <sdk@dfinity.org>"]
edition = "2021"
description = "Canister Developer Kit macros."
Expand Down
9 changes: 8 additions & 1 deletion src/ic-cdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [0.6.5] - 2022-11-04

### Changed

BREAKING CHANGE of experimental API:
- `http_request` to support `context` field in callback function. (#326)

## [0.6.4] - 2022-10-28

### Added
Expand All @@ -20,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.6.2] - 2022-10-24

## Refactored
### Refactored

- Separate `ic0` crate for system API. (#324)

Expand Down
3 changes: 2 additions & 1 deletion src/ic-cdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ic-cdk"
version = "0.6.4"
version = "0.6.5"
authors = ["DFINITY Stiftung <sdk@dfinity.org>"]
edition = "2021"
description = "Canister Developer Kit for the Internet Computer."
Expand All @@ -18,6 +18,7 @@ rust-version = "1.60.0"
candid = "0.8"
cfg-if = "1.0.0"
serde = "1.0.110"
serde_bytes = "0.11.7"
ic0 = { path = "../ic0", version = "0.18.4" }

[dev-dependencies]
Expand Down
64 changes: 44 additions & 20 deletions src/ic-cdk/src/api/management_canister/http_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl CandidType for TransformFunc {
fn _ty() -> Type {
Type::Func(Function {
modes: vec![FuncMode::Query],
args: vec![HttpResponse::ty()],
args: vec![TransformArgs::ty()],
rets: vec![HttpResponse::ty()],
})
}
Expand All @@ -27,36 +27,60 @@ impl CandidType for TransformFunc {
}
}

/// "transform" reference function type:
/// `opt variant { function: func (http_response) -> (http_response) query }`
#[derive(CandidType, Deserialize, Debug, PartialEq, Clone)]
pub enum TransformType {
/// reference function with signature: `func (http_response) -> (http_response) query`
#[serde(rename = "function")]
Function(TransformFunc),
/// Type used for encoding/decoding:
/// `record {
/// response : http_response;
/// context : blob;
/// }`
#[derive(CandidType, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TransformArgs {
/// Raw response from remote service, to be transformed
pub response: HttpResponse,

/// Context for response transformation
#[serde(with = "serde_bytes")]
pub context: Vec<u8>,
}

/// Type used for encoding/decoding:
/// `record {
// function : func (record {response : http_response; context : blob}) -> (http_response) query;
// context : blob;
// }`
#[derive(CandidType, Clone, Debug, Deserialize, PartialEq)]
pub struct TransformContext {
/// Reference function with signature: `func (record {response : http_response; context : blob}) -> (http_response) query;`.
pub function: TransformFunc,

/// Context to be passed to `transform` function to transform HTTP response for consensus
#[serde(with = "serde_bytes")]
pub context: Vec<u8>,
}

impl TransformType {
impl TransformContext {
/// Construct `TransformType` from a transform function.
///
/// # example
///
/// ```ignore
/// #[ic_cdk_macros::query]
/// fn my_transform(arg: HttpResponse) -> HttpResponse {
/// fn my_transform(arg: TransformArgs) -> HttpResponse {
/// ...
/// }
///
/// let transform = TransformType::from_transform_function(my_transform);
/// ```
pub fn from_transform_function<T>(func: T) -> Self
pub fn new<T>(func: T, context: Vec<u8>) -> Self
where
T: Fn(HttpResponse) -> HttpResponse,
T: Fn(TransformArgs) -> HttpResponse,
{
Self::Function(TransformFunc(candid::Func {
principal: crate::id(),
method: get_function_name(func).to_string(),
}))
Self {
function: TransformFunc(candid::Func {
principal: crate::id(),
method: get_function_name(func).to_string(),
}),
context,
}
}
}

Expand Down Expand Up @@ -110,8 +134,8 @@ pub struct CanisterHttpRequestArgument {
pub headers: Vec<HttpHeader>,
/// Optionally provide request body.
pub body: Option<Vec<u8>>,
/// Name of the transform function which is `func (http_response) -> (http_response) query`.
pub transform: Option<TransformType>,
/// Name of the transform function which is `func (transform_args) -> (http_response) query`.
pub transform: Option<TransformContext>,
}

/// The returned HTTP response.
Expand Down Expand Up @@ -170,7 +194,7 @@ mod tests {
body: None,
transform: None,
};
assert_eq!(http_request_required_cycles(&arg), 716500000u128);
assert_eq!(http_request_required_cycles(&arg), 718500000u128);
}

#[test]
Expand All @@ -184,7 +208,7 @@ mod tests {
body: None,
transform: None,
};
assert_eq!(http_request_required_cycles(&arg), 210130900000u128);
assert_eq!(http_request_required_cycles(&arg), 210132900000u128);
}

#[test]
Expand Down

0 comments on commit 14267d8

Please sign in to comment.