Yichun Zhang (章亦春)
and 1 contributors


OpenResty::CheatSheet - Cheat sheet for OpenResty


This 'cheat sheet' is a handy reference, meant for beginning OpenResty users. Not everything is mentioned, but most of the common features in OpenResty::Spec::REST may already be included.

Note that in the following small examples the plain password passing on the URL is merely a hack in the currrent implementation (just for my development convenience). It is supposed to be encrypted on the client side first (with a random salt obtained from the OpenResty server) and all go through SSL in the real thing.


    GET /=/login/agentzh.Admin/password   Login as account "agentzh" and
                                          role "Admin" using encrypted
                                          password "password"
    DELETE /=/view/Blah?_user=agentzh.Admin&_password=password


Model Lists
    GET /=/model                    Read model list
    DELETE /=/model                 Delete all models
    GET /=/model/MyModel            Read the info for MyModel
    POST /=/model/MyModel           Create a model MyModel
        { "description":"Model description",
              "type":"text","default":"No title",
          ] }

    PUT /=/model/MyModel            Alter the model MyModel
        { "name":"NewName","description":"New Description" }

    DELETE /=/model/MyModel         Delete model MyModel
    GET /=/model/MyModel/mycol      Read the info for MyModel's mycol
    POST /=/model/MyModel/mycol     Add a new column named mycol
        { "label":"My column","type":"integer","default":0 }

    PUT /=/model/MyModel/mycol      Alter the column mycol
        { "name":"new_name", "type":"real" }

    DELETE /=/model/MyModel/mycol   Remove column mycol
    GET /=/model/MyModel/id/3       Read the row with id 3
    POST /=/model/MyModel/~/~       Insert new rows
        [ { "col1":"", "col2":"", ... }, ... ]

    POST /=/model/MyModel/~/~       Insert a single new row
        { "col1":"", "col2":"", ... }

    PUT /=/model/MyModel/id/2       Update the row with id 2
        { "col1":"", "col2":"", ... }

    DELETE /=/model/MyModel/id/2    Delete the row with id 2
    DELETE /=/model/MyModel/~/~     Delete all the rows
    GET /=/model/MyModel/~/~?_order_by=col1              order by col1
    GET /=/model/MyModel/~/~?_order_by=col1,col2         order by col1, col2
    GET /=/model/MyModel/~/~?_order_by=col1:asc,col2:desc
                                                order by col1 asc, col2 desc
    GET /=/model/MyModel/~/~?_offset=10&_count=20         offset 10 limit 20


View lists
    GET /=/view                     Get a list of all the views available
    DELETE /=/view                  Delete all the views
    POST /=/view/MyView             Create a new view named MyView which
    { "description":"My view",        takes the parameters "table" and "min"
        "select * from Post, $table where Post.id >= $min"

    GET /=/view/MyView              View the definition of the view MyView
    PUT /=/view/MyView              Update the information of MyView
    { "name":"NewName", "description":"New desc" }

    DELETE /=/view/NewName           Remove the NewName view
Calling views
    GET /=/view/MyView/~/~?table=Comment&min=100

    # assuming the PostsByMonth view has 2 parameters, "year" and "month"
    GET /=/view/PostsByMonth/~/~?year=2008&month=3?
    GET /=/view/PostsByMonth/year/2008?month=3
    GET /=/view/PostsByMonth/month/3?year=2008


Note: Actions have not yet been implemented.

Action lists
    GET /=/action           Get the list of all the existing actions
    DELETE /=/action        Remove all the actions (except built-in ones)
    POST /=/action/PostComment              Create an action for posting
    { "description":"Post a comment",         new comments
        POST /=/model/Comment/~/~
        { \"author\":   $author,
          \"content\":  $content,
          \"post\":     $post };
        update Post set comments=comments+1
        where post = $post;" }

    PUT /=/action/PostComment               Update an existing action
    { "name":"SendComment",
      "description":"Send a comment" }
Calling actions
    POST /=/action/PostComment/~/~       Invoke the PostComment action
    { "author":"agentzh",                  defined above
      "content":"Good post!",
      "post":125 }


    GET /=/view/PostsByMonth/~/~?_callback=foo        Returns "foo(<JSON>);"
                                                     instead of "<JSON>"
    GET /=/view/PostsByMonth/~/~?_var=foo             Returns "foo=<JSON>;"
                                                     instead of "<JSON>"

    GET /=/view/Blah?_user=agentzh.Public             Per-request-login
    GET /=/view/Blah?_session=23EC98F2-F1A...         Specify the session

Path Suffixes

    GET /=/model.json           Get model list in JSON format (the default)
    GET /=/model.yaml           ditto, but in YAML format
    GET /=/model.yml            .yml is an alias for .yaml


Role lists
    GET /=/role             Get the role list
    DELETE /=/role          Delete all roles (except built-in ones)
    GET /=/role/MyRole           Get the info for the "MyRole" role
    POST /=/role/Reader          Create an anoymous role named "Reader"
    { "description":"My Role",
      "login":"anonymous" }

    POST /=/role/Poster                        Create a "Poster" role
    { "description":"Article Poster",            which can ony
      "login":"password",                        login via passwords
      "password":"encrypted password"
ACL rules for a role
    GET /=/role/MyRole/~/~              Get all the ACL rules for MyRole
    GET /=/role/MyRole/method/GET       Filter out ACL rules with GET method
    POST /=/role/MyRole/~/~             Allow MyRole to insert new rows into
    { "method":"POST", "/=/model/Post" }  the Post model

    PUT /=/role/MyRole/id/5              Modify the ACL rule with id 5
    { "method":"PUT" }

    DELETE /=/role/MyRole/method/DELETE  Banned delete operations from MyRole
    DELETE /=/role/MyRole/~/~            Delete all the ACL rules for MyRole


    POST /=/feed/Post,                   Create a feed named "Post" using the
    { "description":"My feed",           RecentPosts view

    GET /=/feed/Post                    Read info for the Post feed back

    PUT /=/feed/Post                    Update attributes of the Post feed
    {"author":"New author",

    GET /=/feed/Post/~/~                Get the RSS 2.0 feed in XML

    DELETE /=/feed/Post                 Remove the Post feed
    DELETE /=/feed                      Remove all the feeds


Admin Setup
    POST /=/role/Commenter              Create a CommentPoster role
    { "description":"Comment Poster",
      "login":"captcha" }

    POST /=/role/Comment/~/~            Assign some permissions to it

    { "method":"POST",
      "url":"/=/model/Comment/~/~" }
Use in clients
    GET /=/captcha/id                   Get a captcha ID
    GET /=/captcha/id/<ID>              Get the captcha image

    POST /=/model/Comment/~/~?_user=account.Commenter&_captcha=<ID>:<solution>
                                        Use the captcha ID and solution
                                        to do the permitted action


Agent Zhang (agentzh) <agentzh at yahoo.cn>.


OpenResty::Spec::REST, OpenResty.