Next: , Previous: , Up: Top   [Contents][Index]

3 API definition

APIs are defined using the DEFINE-API macro. APIs contain resources and resources contain api-functions.

Common Lisp Macro: (define-apiname superclasses options &body resources)

Define an api.

This is the syntax:

(define-api <api-name> (&rest <superclasses>) <options-plist>
   &rest
   <resources>)

Next: , Up: API definition   [Contents][Index]

3.1 API options


Next: , Previous: , Up: API definition   [Contents][Index]

3.2 Resources

Resources have the following syntax:

(<resource-name> <resource-options> <api-functions>)

Resources can be added to an already defined API via the :cl:function::with-api and define-api-resource macros

Common Lisp Macro: (with-apiapi &body body)
Execute body under api scope.

Example: (with-api test-api

(define-resource-operation get-user :get (:url-prefix “users/{id}”)

‘((:id :integer))))

Common Lisp Macro: (define-api-resourcename options &body functions)

Define an api resource.


Up: Resources   [Contents][Index]

3.2.1 Resource options


Next: , Previous: , Up: API definition   [Contents][Index]

3.3 Resource operations

Resources provide a set of operations to access them.

They have the following syntax:

(<resource-operation-name> <resource-operation-options> <resource-operation-arguments>)

New operations can be added to an already defined resource via the with-api-resource

Common Lisp Macro: (with-api-resourceresource &body body)
Execute body under resource scope.

Example: (with-api-resource users

(define-resource-operation get-user :get (:url-prefix “users/{id}”)

‘((:id :integer))))


Next: , Up: Resource operations   [Contents][Index]

3.3.1 Resource operation options


Previous: , Up: Resource operations   [Contents][Index]

3.3.2 Resource operation arguments

Arguments lists have the following syntax:

(*<required-arguments> &optional <optional-arguments>)

Required arguments are those appearing in the api function path between {}. They are specified like this:

(<argument-name> <argument-type> <documentation-string>)

Argument type can be one of: string, integer, boolean, list.

Optional arguments are those that can be passed after the ? in the url. For instance, the page parameter in this url: /users?page=1. They are listed after the &optional symbol, and have the following syntax:

(<argument-name> <argument-type> <default-value> <documentation-string>)

Here is an example of an api function arguments list:

((id :integer "The user id")
  &optional (boolean :boolean nil "A boolean parameter")
            (integer :integer nil "An integer parameter")
            (string :string nil "A string parameter")
            (list :list nil "A list parameter"))

Previous: , Up: API definition   [Contents][Index]

3.4 API example

Here is a complete example of an API interface:

(define-api api-test ()
    (:title "Api test"
            :documentation "This is an api test")
  (parameters (:produces (:json)
                         :consumes (:json)
                         :documentation "Parameters test"
                         :path "/parameters")
              (parameters (:produces (:json)
                                     :consumes (:json)
                                     :documentation "Parameters test"
                                     :path "/parameters")
                          (&optional (boolean :boolean nil "A boolean parameter")
                                     (integer :integer nil "An integer parameter")
                                     (string :string nil "A string parameter")
                                     (list :list nil "A list parameter"))))
  (users (:produces (:json :xml)
                    :consumes (:json)
                    :documentation "Users operations"
                    :models (user)
                    :path "/users")
         (get-users (:request-method :get
                                     :produces (:json)
                                     :path "/users"
                                     :documentation "Retrive the users list")
                    (&optional (page :integer 1 "The page")
                               (expand :list nil "Attributes to expand")))
         (get-user (:request-method :get
                                    :produces (:json)
                                    :path "/users/{id}"
                                    :documentation "Retrive an user")
                   ((id :integer "The user id")
                    &optional
                    (expand :list nil "Attributes to expand")))))

Previous: , Up: API definition   [Contents][Index]