///|
pub struct WebhookResponse {
status_code : Int
body : String
retryable : Bool
} derive(Eq, Debug)
///|
pub fn WebhookResponse::new(
status_code : Int,
body : String,
retryable : Bool,
) -> WebhookResponse {
{ status_code, body, retryable }
}
///|
pub fn accepted_response() -> WebhookResponse {
WebhookResponse::new(202, "{\"status\":\"accepted\"}", false)
}
///|
pub fn rejected_response(reason : String) -> WebhookResponse {
WebhookResponse::new(
401,
"{\"status\":\"rejected\",\"reason\":\"" + reason + "\"}",
false,
)
}
///|
pub fn ignored_response(reason : String) -> WebhookResponse {
WebhookResponse::new(
204,
"{\"status\":\"ignored\",\"reason\":\"" + reason + "\"}",
false,
)
}
///|
pub fn retry_response(reason : String) -> WebhookResponse {
WebhookResponse::new(
503,
"{\"status\":\"retry\",\"reason\":\"" + reason + "\"}",
true,
)
}
///|
pub fn response_for_output(output : GuardPipelineOutput) -> WebhookResponse {
if !output.verification.is_ok() {
rejected_response(output.risk.level)
} else if !output.route.matched {
ignored_response(output.route.reason)
} else {
accepted_response()
}
}
///|
pub fn response_text(response : WebhookResponse) -> String {
"status=\{response.status_code} retryable=\{response.retryable} body=\{response.body}"
}