1use candid::CandidType;
2pub use ic_cdk::*;
3pub use ic_cdk_timers::*;
4use serde::{Deserialize, Serialize};
5
6#[derive(CandidType, Debug, Clone, Serialize, Deserialize, Default)]
7pub struct Response {
8 pub status_code: u16,
9 pub headers: Vec<(String, String)>,
10 pub body: Vec<u8>,
11 pub upgrade: Option<bool>,
12}
13
14#[derive(CandidType, Debug, Clone, Serialize, Deserialize)]
15pub struct Request {
16 pub method: String,
17 pub url: String,
18 pub headers: Vec<(String, String)>,
19 pub body: Vec<u8>,
20}
21
22impl Response {
23 pub fn build<T>(body: T) -> Self
24 where
25 T: serde::Serialize,
26 {
27 let body = serde_json::to_vec(&body).unwrap();
28
29 Response {
30 status_code: 200,
31 headers: vec![
32 ("Content-Type".to_string(), "application/json".to_string()),
33 ("Content-Length".to_string(), body.len().to_string()),
34 ],
35 body,
36 upgrade: None,
37 }
38 }
39
40 pub fn with_status(mut self, status: u16) -> Self {
41 self.status_code = status;
42 self
43 }
44
45 pub fn with_upgrade(mut self) -> Self {
46 self.upgrade = Some(true);
47 self
48 }
49
50 pub fn upgrade() -> Self {
51 Response {
52 upgrade: Some(true),
53 ..Default::default()
54 }
55 }
56}