The restful Reference Manual

This is the restful Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Feb 26 17:47:37 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 restful

Spin up new REST entities like madman

Author

Florian Margaine <>

License

MIT License

Dependencies
  • hunchentoot (system).
  • alexandria (system).
  • cl-ppcre (system).
  • jonathan (system).
  • closer-mop (system).
Source

restful.asd.

Child Component

src (module).


3 Modules

Modules are listed depth-first from the system components tree.


3.1 restful/src

Source

restful.asd.

Parent Component

restful (system).

Child Components

4 Files

Files are sorted by type and then listed depth-first from the systems components trees.


4.1 Lisp


4.1.1 restful/restful.asd

Source

restful.asd.

Parent Component

restful (system).

ASDF Systems

restful.


4.1.2 restful/src/package.lisp

Source

restful.asd.

Parent Component

src (module).

Packages

restful.


4.1.3 restful/src/condition.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface

4.1.4 restful/src/storage.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface

4.1.5 restful/src/storages/memory.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface

4.1.6 restful/src/resource-metaclass.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface
Internals

4.1.7 restful/src/slot.lisp

Source

restful.asd.

Parent Component

src (module).

Internals

4.1.8 restful/src/resource.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface
Internals

4.1.9 restful/src/collection.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface
Internals

4.1.10 restful/src/http.lisp

Source

restful.asd.

Parent Component

src (module).

Internals

http-error (function).


4.1.11 restful/src/acceptor.lisp

Source

restful.asd.

Parent Component

src (module).

Public Interface
Internals

5 Packages

Packages are listed by definition order.


5.1 restful

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

6 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


6.1 Public Interface


6.1.1 Generic functions

Generic Function: create-resource (resource request-data)

Creates a new resource based on the request data.

Package

restful.

Source

resource.lisp.

Methods
Method: create-resource ((resource resource) request-data)

Creates a new resource in the storage based on the request data.

Generic Function: delete-item (storage identifier)

Deletes a single item in the storage.

Package

restful.

Source

storage.lisp.

Methods
Method: delete-item ((storage memory-storage) identifier)

Deletes the item from the hash table. The identifier is the key of the hash table.

Source

storages/memory.lisp.

Generic Function: delete-resource (resource)

Deletes an existing resource.

Package

restful.

Source

resource.lisp.

Methods
Method: delete-resource ((resource resource))

Deletes an existing resource. If the resource doesn’t exist, an error was already thrown earlier thanks to load-resource.

Generic Function: get-item (storage identifier)

Gets a single item in the storage. Returns a plist.

Package

restful.

Source

storage.lisp.

Methods
Method: get-item ((storage memory-storage) identifier)

Gets a single item according to its identifier. Returns a plist. The identifier is the key of the hash table.

Source

storages/memory.lisp.

Generic Function: get-items (storage)

Gets all the items available in the storage.

This should be overridden for a specific storage if sorting/filtering options want to be added.

Returns a list of plists.

Package

restful.

Source

storage.lisp.

Methods
Method: get-items ((storage memory-storage))

Gets all the items stored in the current instance.

Source

storages/memory.lisp.

Generic Function: has-permission (resource method)

Determines if the request has permission to hit the resource. If it doesn’t, returns NIL.

Package

restful.

Source

resource.lisp.

Methods
Method: has-permission ((resource resource) method)

Returns T. Override this method to change the behavior.

Generic Function: load-resource (resource)

Loads a resource based on its identifier.

Package

restful.

Source

resource.lisp.

Methods
Method: load-resource ((resource resource))

Loads the resource using its storage. A resource-not-found-error error is raised if the resource was not found.

Generic Reader: parent (object)
Package

restful.

Methods
Reader Method: parent ((collection collection))

The parent resource, if any. If there’s no parent, its value is NIL.

Source

collection.lisp.

Target Slot

parent.

Reader Method: parent ((resource resource))

Stores the parent of the resource in case of
hierarchy. For example, if hitting foo/bar/baz/qux, the resource with identifier ’qux’ will have ’bar’ as parent. If there’s no parent, its value is NIL.

Source

resource.lisp.

Target Slot

parent.

Generic Function: patch-resource (resource request-data)

Patches an existing resource based on the request data.

Package

restful.

Source

resource.lisp.

Methods
Method: patch-resource ((resource resource) request-data)

Patches an existing resource based on the request data.

Generic Function: replace-resource (resource request-data)

Replaces a resource based on the request data.

Package

restful.

Source

resource.lisp.

Methods
Method: replace-resource ((resource resource) request-data)

Replaces the resources based on the request data.

Generic Function: resource-action (resource)

Lets you handle actions on the resource.
The :identifier slot lets you know which action is called. This method is called for the POST requests, and routing should be handled by yourself. Here is a typical example of what it can look like:

(defmethod restful:resource-action ((res custom-resource)) (cond ((string= (identifier res) "login") #’handle-login) (t (http-page-not-found))))

Package

restful.

Source

resource.lisp.

Methods
Method: resource-action ((resource resource))

Returns a 404 Page Not Found. Raising a resource-not-found error doesn’t make sense.

Generic Function: save-item (storage resource)

Saves an item in the storage, creating it if needed.

Since resources are created or updated with PUT requests, as long as you have an ID, a resource exists. Except if the user doesn’t have permission to PUT non-existing resources, but that’s handled at the application level, not at the storage level, which only cares that a resource has an identifier.

Package

restful.

Source

storage.lisp.

Methods
Method: save-item ((storage memory-storage) resource)

Saves or updates an item in the hash table. Since ‘(setf (gethash [...]))‘ is used, the ’save or update’ feature is very simply done.

Source

storages/memory.lisp.

Generic Reader: storage (object)
Package

restful.

Methods
Reader Method: storage ((collection collection))

The storage object that satisfies the interface of the ‘restful:storage‘ class.

Source

collection.lisp.

Target Slot

storage.

Reader Method: storage ((resource resource))

The storage object that satisfies the interface of the ‘restful:storage‘ class.

Source

resource.lisp.

Target Slot

storage.

Reader Method: storage ((memory-storage memory-storage))

The hash table used to store the items in memory.

Source

storages/memory.lisp.

Target Slot

storage.

Generic Function: view-collection (collection)

Returns an object that will be
serialized to json using the jonathan library.

Package

restful.

Source

collection.lisp.

Methods
Method: view-collection ((collection collection))

Returns an object populated by the collection’s storage.

Generic Function: view-resource (resource)

Returns an object that will be serialized to json using the jonathan library.

Package

restful.

Source

resource.lisp.

Methods
Method: view-resource ((resource resource))

Returns a plist representing the resource.


6.1.2 Standalone methods

Method: %to-json ((resource resource))

Serializes a resource to json using the jonathan library.

Package

jonathan.encode.

Source

resource.lisp.

Method: acceptor-dispatch-request ((acceptor acceptor) request)

Dispatches requests to the internal handle-uri function if the request matches the requirements. Namely, accepting application/json.

This method also defines the application/json response header.

This method catches the following errors:

- ‘resource-not-found-error‘: returns a 404 page not found response. - ‘request-data-missing‘: returns a 400 bad request response.
- ‘permission-rejected‘: returns a 403 forbidden response.
- ‘error‘: returns a 500 internal server error response.

Package

hunchentoot.

Source

acceptor.lisp.

Method: compute-effective-slot-definition ((class resource-metaclass) name direct-slot-definitions)
Package

sb-mop.

Source

resource-metaclass.lisp.

Method: direct-slot-definition-class ((class resource-metaclass) &rest initargs)
Package

sb-mop.

Source

resource-metaclass.lisp.

Method: effective-slot-definition-class ((class resource-metaclass) &rest initargs)
Package

sb-mop.

Source

resource-metaclass.lisp.

Method: validate-superclass ((class resource-metaclass) (superclass standard-class))
Package

sb-mop.

Source

resource-metaclass.lisp.


6.1.3 Conditions

Condition: permission-rejected

Raised when a request doesn’t have access to the
requested resource.

This error is raised when the has-permission method of a resource returns NIL.

When this error is handled, the response will have the 403 status code.

This error can be raised with every method.

Package

restful.

Source

condition.lisp.

Direct superclasses

error.

Condition: request-data-missing

Raised when a request body doesn’t fulfill the
resource’s schema.

For example, if a resource is the following:

(defclass foo (restful:resource)
((id :is-identifier t)
(name :required t))
(:metaclass restful:resource-metaclass))

And the request body is the following:

{"id":"bar"}

This error will be raised.

When this error is handled, the response will have a 400 status code.

This error can be raised in the following requests: PUT, POST.

Package

restful.

Source

condition.lisp.

Direct superclasses

error.

Condition: resource-not-found-error

Raised when a request resource is not found.

When this error is handled, the response will have the 404 status code.

This error can be raised in the following requests: GET, PATCH, DELETE, POST.

Package

restful.

Source

condition.lisp.

Direct superclasses

error.


6.1.4 Classes

Class: acceptor

Base class for the acceptor, subclassing
a hunchentoot acceptor to be able to handle incoming requests.

This class defines the following slot:

- ‘resource-definition‘: defines the list of resources and
how to handle them. This is a hash table that must:
- define a string key being the prefix for the resources
- for each key, define a value being a hash table. This
hash table must:
- define a keyword key named ‘:class‘ being the resource class.
- define a keyword named ‘:collection‘ being the collection class.
- define a keyword named ‘:storage‘ being the storage instance. - define a keyword named ‘:children‘ being a new resource definition, if necessary.

This is an example of a resource definition (assuming the readers macros to define hash tables using brackets):

{
"article" {
:class ’article
:collection ’restful:collection
:storage (make-instance ’restful:memory-storage) :children {
"comment" {
:class ’comment
:collection ’restful:collection
:storage (make-instance ’restful:memory-storage) }
}
}
}

The API will be available through the following endpoints:

- ‘/article‘: collection endpoint.
- ‘/article/foo‘: resource ’foo’ of instance ‘article‘ endpoint.
- ‘/article/foo/comment/bar‘: resource ’bar’ of instance ‘comment‘, having for parent ’foo’ of instance ‘article‘ endpoint.

This class should be used to instantiate objects to be used
with hunchentoot.

Here is an example of such usage:

(hunchentoot:start
(make-instance ’restful:acceptor
:port 4242
:resource-definition *resource-definition*))

Package

restful.

Source

acceptor.lisp.

Direct superclasses

acceptor.

Direct methods

acceptor-dispatch-request.

Direct slots
Slot: resource-definition

The resource definition, documented in the class documentation.

Type

hash-table

Initargs

:resource-definition

Class: collection

Base class for restful collections. There’s
not much reason to extend it with the current features.

Package

restful.

Source

collection.lisp.

Direct methods
Direct slots
Slot: parent

The parent resource, if any. If there’s no parent, its value is NIL.

Initargs

:parent

Readers

parent.

Writers

This slot is read-only.

Slot: storage

The storage object that satisfies the interface of the ‘restful:storage‘ class.

Initargs

:storage

Readers

storage.

Writers

This slot is read-only.

Slot: class-of-resource

The resource’s class of this collection.

Initargs

:class-of-resource

Readers

class-of-resource.

Writers

This slot is read-only.

Class: memory-storage

Restful’s storage by storing the data in memory.

This is of course not very good for persistence matters (duh), but is perfect for development, and simply to show how a storage class can be built.

This storage just uses a hash table stored in a slot.

Package

restful.

Source

storages/memory.lisp.

Direct methods
Direct slots
Slot: storage

The hash table used to store the items in memory.

Initform

(make-hash-table :test (quote equal))

Readers

storage.

Writers

This slot is read-only.

Class: resource

Base class for resources. All the resources
should extend this class to have the default (required) slots.

Package

restful.

Source

resource.lisp.

Direct methods
Direct slots
Slot: parent

Stores the parent of the resource in case of
hierarchy. For example, if hitting foo/bar/baz/qux, the resource with identifier ’qux’ will have ’bar’ as parent. If there’s no parent, its value is NIL.

Type

restful:resource

Initargs

:parent

Readers

parent.

Writers

This slot is read-only.

Slot: storage

The storage object that satisfies the interface of the ‘restful:storage‘ class.

Type

restful:storage

Initargs

:storage

Readers

storage.

Writers

This slot is read-only.

Class: resource-metaclass

The metaclass for resources, required to be
used by all the resources.

This metaclass allows resources to use new slot options:

- ‘is-identifier‘: defaults to NIL. Only one slot per resource should set this option to T. It will make the slot the identifier of the resource. The identifier is used to find the resource in the API. When set to T, the slot option ‘required‘ is implicitly set to T too.
- ‘required‘: defaults to NIL. When set to T, this slot will be required in the API requests.
- ‘default‘: defaults to ‘""‘. If the slot is not required, this value will be used to fill in the slot value if no value is provided.
- ‘excluded‘: defaults to NIL. When to set T, this slot will be ignored for the resource’s CRUD actions. For example, the ‘restful:resource‘ class uses it for its ‘parent‘ and ‘storage‘ slots.

Package

restful.

Source

resource-metaclass.lisp.

Direct superclasses

standard-class.

Direct methods

6.2 Internals


6.2.1 Ordinary functions

Function: equal-resource (resource1 resource2)
Package

restful.

Source

resource.lisp.

Function: find-identifier-slot (class)
Package

restful.

Source

slot.lisp.

Function: get-resource-slots (resource)
Package

restful.

Source

slot.lisp.

Function: handle-collection (resource-hash-value parent)
Package

restful.

Source

acceptor.lisp.

Function: handle-delete-resource (resource)
Package

restful.

Source

acceptor.lisp.

Function: handle-get-resource (resource)
Package

restful.

Source

acceptor.lisp.

Function: handle-patch-resource (resource)
Package

restful.

Source

acceptor.lisp.

Function: handle-post-resource (resource)
Package

restful.

Source

acceptor.lisp.

Function: handle-put-resource (resource)
Package

restful.

Source

acceptor.lisp.

Function: handle-resource (parts resource-hash-value parent)
Package

restful.

Source

acceptor.lisp.

Function: handle-resource-method (method resource)
Package

restful.

Source

acceptor.lisp.

Function: handle-uri (parts resources &optional parent)
Package

restful.

Source

acceptor.lisp.

Function: http-error (code)
Package

restful.

Source

http.lisp.

Function: is-slot-excluded (slot)
Package

restful.

Source

slot.lisp.

Function: normalize-keyword (symbol)
Package

restful.

Source

slot.lisp.

Function: normalize-keywords (symbols)
Package

restful.

Source

slot.lisp.

Function: normalize-resource (resource)
Package

restful.

Source

slot.lisp.

Function: populate-resource (resource filler)
Package

restful.

Source

resource.lisp.

Function: populate-resources (collection items)
Package

restful.

Source

collection.lisp.

Function: populate-slot (resource filler)
Package

restful.

Source

slot.lisp.

Function: slot-default-value (resource slot)
Package

restful.

Source

slot.lisp.

Function: slot-is-required (resource slot)
Package

restful.

Source

slot.lisp.


6.2.2 Generic functions

Generic Reader: class-of-resource (object)
Package

restful.

Methods
Reader Method: class-of-resource ((collection collection))

The resource’s class of this collection.

Source

collection.lisp.

Target Slot

class-of-resource.


6.2.3 Classes

Class: resource-standard-direct-slot-definition
Package

restful.

Source

resource-metaclass.lisp.

Direct superclasses

standard-direct-slot-definition.

Direct slots
Slot: is-identifier
Initargs

:is-identifier

Slot: required
Initargs

:required

Slot: default
Initargs

:default

Slot: excluded
Initargs

:excluded

Class: resource-standard-effective-slot-definition
Package

restful.

Source

resource-metaclass.lisp.

Direct superclasses

standard-effective-slot-definition.

Direct slots
Slot: is-identifier
Initargs

:is-identifier

Slot: required
Initargs

:required

Slot: default
Initargs

:default

Slot: excluded
Initargs

:excluded


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   %  
A   C   D   E   F   G   H   I   L   M   N   P   R   S   V  
Index Entry  Section

%
%to-json: Public standalone methods

A
acceptor-dispatch-request: Public standalone methods

C
class-of-resource: Private generic functions
class-of-resource: Private generic functions
compute-effective-slot-definition: Public standalone methods
create-resource: Public generic functions
create-resource: Public generic functions

D
delete-item: Public generic functions
delete-item: Public generic functions
delete-resource: Public generic functions
delete-resource: Public generic functions
direct-slot-definition-class: Public standalone methods

E
effective-slot-definition-class: Public standalone methods
equal-resource: Private ordinary functions

F
find-identifier-slot: Private ordinary functions
Function, equal-resource: Private ordinary functions
Function, find-identifier-slot: Private ordinary functions
Function, get-resource-slots: Private ordinary functions
Function, handle-collection: Private ordinary functions
Function, handle-delete-resource: Private ordinary functions
Function, handle-get-resource: Private ordinary functions
Function, handle-patch-resource: Private ordinary functions
Function, handle-post-resource: Private ordinary functions
Function, handle-put-resource: Private ordinary functions
Function, handle-resource: Private ordinary functions
Function, handle-resource-method: Private ordinary functions
Function, handle-uri: Private ordinary functions
Function, http-error: Private ordinary functions
Function, is-slot-excluded: Private ordinary functions
Function, normalize-keyword: Private ordinary functions
Function, normalize-keywords: Private ordinary functions
Function, normalize-resource: Private ordinary functions
Function, populate-resource: Private ordinary functions
Function, populate-resources: Private ordinary functions
Function, populate-slot: Private ordinary functions
Function, slot-default-value: Private ordinary functions
Function, slot-is-required: Private ordinary functions

G
Generic Function, class-of-resource: Private generic functions
Generic Function, create-resource: Public generic functions
Generic Function, delete-item: Public generic functions
Generic Function, delete-resource: Public generic functions
Generic Function, get-item: Public generic functions
Generic Function, get-items: Public generic functions
Generic Function, has-permission: Public generic functions
Generic Function, load-resource: Public generic functions
Generic Function, parent: Public generic functions
Generic Function, patch-resource: Public generic functions
Generic Function, replace-resource: Public generic functions
Generic Function, resource-action: Public generic functions
Generic Function, save-item: Public generic functions
Generic Function, storage: Public generic functions
Generic Function, view-collection: Public generic functions
Generic Function, view-resource: Public generic functions
get-item: Public generic functions
get-item: Public generic functions
get-items: Public generic functions
get-items: Public generic functions
get-resource-slots: Private ordinary functions

H
handle-collection: Private ordinary functions
handle-delete-resource: Private ordinary functions
handle-get-resource: Private ordinary functions
handle-patch-resource: Private ordinary functions
handle-post-resource: Private ordinary functions
handle-put-resource: Private ordinary functions
handle-resource: Private ordinary functions
handle-resource-method: Private ordinary functions
handle-uri: Private ordinary functions
has-permission: Public generic functions
has-permission: Public generic functions
http-error: Private ordinary functions

I
is-slot-excluded: Private ordinary functions

L
load-resource: Public generic functions
load-resource: Public generic functions

M
Method, %to-json: Public standalone methods
Method, acceptor-dispatch-request: Public standalone methods
Method, class-of-resource: Private generic functions
Method, compute-effective-slot-definition: Public standalone methods
Method, create-resource: Public generic functions
Method, delete-item: Public generic functions
Method, delete-resource: Public generic functions
Method, direct-slot-definition-class: Public standalone methods
Method, effective-slot-definition-class: Public standalone methods
Method, get-item: Public generic functions
Method, get-items: Public generic functions
Method, has-permission: Public generic functions
Method, load-resource: Public generic functions
Method, parent: Public generic functions
Method, parent: Public generic functions
Method, patch-resource: Public generic functions
Method, replace-resource: Public generic functions
Method, resource-action: Public generic functions
Method, save-item: Public generic functions
Method, storage: Public generic functions
Method, storage: Public generic functions
Method, storage: Public generic functions
Method, validate-superclass: Public standalone methods
Method, view-collection: Public generic functions
Method, view-resource: Public generic functions

N
normalize-keyword: Private ordinary functions
normalize-keywords: Private ordinary functions
normalize-resource: Private ordinary functions

P
parent: Public generic functions
parent: Public generic functions
parent: Public generic functions
patch-resource: Public generic functions
patch-resource: Public generic functions
populate-resource: Private ordinary functions
populate-resources: Private ordinary functions
populate-slot: Private ordinary functions

R
replace-resource: Public generic functions
replace-resource: Public generic functions
resource-action: Public generic functions
resource-action: Public generic functions

S
save-item: Public generic functions
save-item: Public generic functions
slot-default-value: Private ordinary functions
slot-is-required: Private ordinary functions
storage: Public generic functions
storage: Public generic functions
storage: Public generic functions
storage: Public generic functions

V
validate-superclass: Public standalone methods
view-collection: Public generic functions
view-collection: Public generic functions
view-resource: Public generic functions
view-resource: Public generic functions


A.4 Data types

Jump to:   A   C   F   H   M   P   R   S  
Index Entry  Section

A
acceptor: Public classes
acceptor.lisp: The restful/src/acceptor․lisp file

C
Class, acceptor: Public classes
Class, collection: Public classes
Class, memory-storage: Public classes
Class, resource: Public classes
Class, resource-metaclass: Public classes
Class, resource-standard-direct-slot-definition: Private classes
Class, resource-standard-effective-slot-definition: Private classes
collection: Public classes
collection.lisp: The restful/src/collection․lisp file
Condition, permission-rejected: Public conditions
Condition, request-data-missing: Public conditions
Condition, resource-not-found-error: Public conditions
condition.lisp: The restful/src/condition․lisp file

F
File, acceptor.lisp: The restful/src/acceptor․lisp file
File, collection.lisp: The restful/src/collection․lisp file
File, condition.lisp: The restful/src/condition․lisp file
File, http.lisp: The restful/src/http․lisp file
File, package.lisp: The restful/src/package․lisp file
File, resource-metaclass.lisp: The restful/src/resource-metaclass․lisp file
File, resource.lisp: The restful/src/resource․lisp file
File, restful.asd: The restful/restful․asd file
File, slot.lisp: The restful/src/slot․lisp file
File, storage.lisp: The restful/src/storage․lisp file
File, storages/memory.lisp: The restful/src/storages/memory․lisp file

H
http.lisp: The restful/src/http․lisp file

M
memory-storage: Public classes
Module, src: The restful/src module

P
Package, restful: The restful package
package.lisp: The restful/src/package․lisp file
permission-rejected: Public conditions

R
request-data-missing: Public conditions
resource: Public classes
resource-metaclass: Public classes
resource-metaclass.lisp: The restful/src/resource-metaclass․lisp file
resource-not-found-error: Public conditions
resource-standard-direct-slot-definition: Private classes
resource-standard-effective-slot-definition: Private classes
resource.lisp: The restful/src/resource․lisp file
restful: The restful system
restful: The restful package
restful.asd: The restful/restful․asd file

S
slot.lisp: The restful/src/slot․lisp file
src: The restful/src module
storage.lisp: The restful/src/storage․lisp file
storages/memory.lisp: The restful/src/storages/memory․lisp file
System, restful: The restful system