use crate::spec::prelude::*;
use candid::define_function;
define_function!(pub CallbackFunc : () -> () query);
pub type HeaderField = (String, String);
#[derive(CandidType, Clone, Deserialize)]
pub struct HttpRequest {
pub url: String,
pub method: String,
pub headers: Vec<HeaderField>,
pub body: Vec<u8>,
}
#[derive(CandidType, Clone, Copy, Debug, Deserialize, Serialize)]
pub enum HttpStatus {
Ok,
BadRequest,
NotFound,
InternalError,
}
impl HttpStatus {
#[must_use]
pub const fn code(&self) -> u16 {
match self {
Self::Ok => 200,
Self::BadRequest => 400,
Self::NotFound => 404,
Self::InternalError => 500,
}
}
}
#[derive(CandidType, Clone, Deserialize)]
pub struct HttpResponse {
pub status_code: u16,
pub headers: Vec<HeaderField>,
pub body: Vec<u8>,
pub streaming_strategy: Option<StreamingStrategy>,
}
impl HttpResponse {
#[must_use]
pub fn error(status: HttpStatus, message: &str) -> Self {
Self {
status_code: status.code(),
headers: vec![("Content-Type".into(), "text/plain; charset=utf-8".into())],
body: message.as_bytes().to_vec(),
streaming_strategy: None,
}
}
}
#[derive(CandidType, Clone, Deserialize, Serialize)]
pub struct StreamingCallbackToken {
pub asset_id: String,
pub chunk_index: candid::Nat,
pub headers: Vec<HeaderField>,
}
#[derive(CandidType, Deserialize)]
pub struct StreamingCallbackHttpResponse {
pub body: Vec<u8>,
pub token: Option<StreamingCallbackToken>,
}
#[derive(CandidType, Clone, Deserialize)]
pub enum StreamingStrategy {
Callback {
token: StreamingCallbackToken,
callback: CallbackFunc,
},
}