canic_cdk/spec/ic/
http.rs

1use candid::{CandidType, Deserialize, define_function};
2use serde::Serialize;
3
4// Define the callback function type using the macro
5define_function!(pub CallbackFunc : () -> () query);
6
7///
8/// HeaderField
9///
10
11pub type HeaderField = (String, String);
12
13///
14/// HttpRequest
15///
16
17#[derive(CandidType, Clone, Deserialize)]
18pub struct HttpRequest {
19    pub url: String,
20    pub method: String,
21    pub headers: Vec<HeaderField>,
22    pub body: Vec<u8>,
23}
24
25///
26/// HttpResponse
27///
28
29#[derive(CandidType, Clone, Deserialize)]
30pub struct HttpResponse {
31    pub status_code: u16,
32    pub headers: Vec<HeaderField>,
33    pub body: Vec<u8>,
34    pub streaming_strategy: Option<StreamingStrategy>,
35}
36
37///
38/// StreamingCallbackToken
39///
40
41#[derive(CandidType, Clone, Deserialize, Serialize)]
42pub struct StreamingCallbackToken {
43    pub asset_id: String,
44    pub chunk_index: candid::Nat,
45    pub headers: Vec<HeaderField>,
46}
47
48///
49/// StreamingCallbackHttpResponse
50///
51
52#[derive(CandidType, Deserialize)]
53pub struct StreamingCallbackHttpResponse {
54    pub body: Vec<u8>,
55    pub token: Option<StreamingCallbackToken>,
56}
57
58///
59/// StreamingStrategy
60///
61
62#[derive(CandidType, Clone, Deserialize)]
63pub enum StreamingStrategy {
64    Callback {
65        token: StreamingCallbackToken,
66        callback: CallbackFunc,
67    },
68}