From 0b00b8aeadb53abe4cc2bf14d132fe93812b387b Mon Sep 17 00:00:00 2001 From: colin Date: Sat, 4 May 2024 19:25:52 -0700 Subject: added defendpoint macro --- src/protocol.lisp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'src/protocol.lisp') diff --git a/src/protocol.lisp b/src/protocol.lisp index 19a4791..d25aa22 100644 --- a/src/protocol.lisp +++ b/src/protocol.lisp @@ -52,7 +52,7 @@ that the request has insufficient permissions to evoke the endpoint handler. ")) (:documentation "Signalled whenever a required slot is missing from a endpoint instance object.")) -(define-condition not-found (protocol-error) +(define-condition not-found (protocol-error) () (:default-initargs :status-code 404)) (defgeneric protocol-error-result (err) @@ -88,15 +88,18 @@ that the request has insufficient permissions to evoke the endpoint handler. ")) (invoke-debugger err) (http:abort-request-handler))))) -(defun protocol-error (error-class req &rest kwargs) +(defun protocol-error (error-class ep &rest kwargs) (apply #'error error-class :raw-request http:*request* - :class (class-of req) + :class (class-of ep) kwargs)) -(defun not-found (req) - (protocol-error 'not-found req)) + +(defun slot-required (ep slot) + "Signals a SLOT-REQUIRED condition" + (protocol-error 'slot-required ep :missing-slot slot)) + ;;; HANDLER PROTOCOL @@ -105,13 +108,13 @@ that the request has insufficient permissions to evoke the endpoint handler. ")) this. Called before handling, should be used to supply user-identifying data to the endpoint instance that might be needed by the handle function.") - (:method ((req t)) t)) + (:method ((ep t)) t)) (defgeneric authorize (endpoint) (:documentation "Returns a boolean. Any endpoint requiring special ownership permissions should implement this. Called before handling and after authenticate.") - (:method ((req t)) t)) + (:method ((ep t)) t)) (defgeneric handle (endpoint) (:documentation "The beef of the endpoint handling protocol. @@ -139,6 +142,19 @@ MUST be implemented for every endpoint class.") (unless (authorize endpoint) (protocol-error 'not-authorized endpoint)))) +;;; HANDLER TOOLS +(defun not-found (ep) + "Signals a NOT-FOUND condition. Usually called within HANDLE or +AUTHORIZE while handling endpoint-class instance EP." + (protocol-error 'not-found ep)) +(defun redirect (url) + "Redirect to URL." + (http:redirect url :code http:+http-see-other+)) +(defun redirect-to (class &rest kwargs) + "Redirect to another endpoint. CLASS can be either a symbol or a + class. KWARGS is a PLIST of keyword arguments supplied to the + CLASS' route builder function." + (redirect (apply #'route-to class kwargs))) -- cgit v1.2.3