Changes between Version 7 and Version 8 of DeveloperGuidelines/WebServices


Ignore:
Timestamp:
03/08/09 16:55:38 (13 years ago)
Author:
Fran Boon
Comment:

New tools.py Services

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/WebServices

    v7 v8  
    1 DeveloperGuidelines
    2 
    31== Web Services ==
    42
     
    1311}}}
    1412
    15 The underlying functions are very easy within Web2Py since 1.55:
     13The underlying output functions are very easy within Web2Py since 1.55:
    1614{{{
    1715def display_json():
     
    2321
    2422----
    25 Another approach (from T3: http://groups.google.com/group/web2py/browse_thread/thread/9ec8b06b158adb75):
     23{{{tools.py}}} now supports easy exposing of fucntions to XMLRPC or JSONRPC:
     24  * http://groups.google.com/group/web2py/browse_thread/thread/53086d5f89ac3ae2
     25(originally in T3: http://groups.google.com/group/web2py/browse_thread/thread/9ec8b06b158adb75):
    2626Model:
    2727{{{
    28 from gluon.storage import Storage
    29 settings=Storage()
    30 settings.rss_procedures=[]
    31 settings.exposed_procedures=[]
    32 settings.xmlrpc_procedures=[]
    33 settings.json_procedures=[]
    34 def rss(f):
    35     settings.rss_procedures.append(f.__name__)
    36     return f
    37 def expose(f):
    38     settings.exposed_procedures.append(f.__name__)
    39     return f
    40 def json(f):
    41     settings.json_procedures.append(f.__name__)
    42     return f
    43 def xmlrpc(f):
    44     settings.xmlrpc_procedures.append(f.__name__)
     28from gluon.tools import Service
     29service=Service(globals())
    4530}}}
    4631Controller:
    4732{{{
    48 def rss():
    49     if request.args and request.args[0] in settings.rss_procedures:
    50        feed=eval('%s(*request.args[1:],**dict
    51 (request.vars))'%request.args[0])
    52     else:
    53        t2._error()
    54     import gluon.contrib.rss2 as rss2
    55     rss = rss2.RSS2(
    56        title=feed['title'],
    57        link = feed['link'],
    58        description = feed['description'],
    59        lastBuildDate = feed['created_on'],
    60        items = [
    61           rss2.RSSItem(
    62             title = entry['title'],
    63             link = entry['link'],
    64             description = entry['description'],
    65             pubDate = entry['created_on']) for entry in feed
    66 ['entries']]
    67        )
    68     response.headers['Content-Type']='application/rss+xml'
    69     return rss2.dumps(rss)
    70 
    71 def run():
    72     if request.args and request.args[0] in
    73 settings.exposed_procedures:
    74        return eval('%s(*request.args[1:],**dict
    75 (request.vars))'%request.args[0])
    76     return 'Not Authorized'
    77 
    78 def xmlrpc():
    79     methods=[eval(x) for x in settings.xmlrpc_procedures]
    80     return response.xmlrpc(request,methods)
    81 
    82 def json():
    83     if request.args and request.args[0] in settings.json_procedures:
    84        import gluon.contrib.simplejson as sj
    85        return sj.dumps(eval('%s(*request.args[1:],**dict
    86 (request.vars))'%request.args[0]))
    87     return 'Not Authorized'
     33@service.jsonrpc
     34@service.xmlrpc
     35@service.amfrpc
     36def organisation():
     37    "RESTlike CRUD controller"
     38    return shn_rest_controller(module,'organisation')
    8839}}}
    8940----
     
    9546----
    9647DeveloperGuidelines
    97 DeveloperGuidelines