Web2py – Services


Web2py – Services


”;


web2py provides support for various protocols like XML, JSON, RSS, CSV, XMLRPC, JSONRPC, AMFRPC, and SOAP. Each of those protocols is supported in multiple ways, and we make a distinction between −

  • Rendering the output of a function in a given format.
  • Remote Procedure Calls.

Rendering a Dictionary

Consider the following code which maintains the count of the sessions.

def count():
   session.counter = (session.counter or 0) + 1
   return dict(counter = session.counter, now = request.now)

The above function increases the number of counts as and when the user visits the page. Suppose the given function is defined in “default.py” controller of web2py application. The page can be requested with the following URL − http://127.0.0.1:8000/app/default/count

web2py can render the above page in different protocols and by adding extension to the URL, like −

http://127.0.0.1:8000/app/default/count.html

http://127.0.0.1:8000/app/default/count.xml

http://127.0.0.1:8000/app/default/count.json

The dictionary returned by the above action will be rendered in HTML, XML and JSON.

Remote Procedure Calls

web2py framework provides a mechanism which converts a function into a web service. The mechanism described here differs from the mechanism described before because −

  • Inclusion of arguments in function.
  • The function must be defined in a model.
  • It enforces a more strict URL naming convention.
  • It works for a fixed set of protocols and it is easily extensible.
  • To use this feature it is necessary to import and initiate a service object.

To implement this mechanism, at first, you must import and instantiate a service object.

from gluon.tools import Service
service = Service()

This is implemented in the “db.py” model file in the scaffolding application. Db.py model is the default model in web2py framework, which interacts with the database and the controller to achieve the desired output to the users.

After implementing, the service in model can be accessed from the controllers as and when required.

The following example shows various implementations of remote procedure calls using web services and many more.

Web Services

Web Services can be defined as a standardized way of integrating Web-based applications using the protocols like XML, SOAP, WSDL and UDDI.

web2py supports most of them, but the integration will be quite tricky.

Consuming a web2py JSON service with jQuery

There are many ways to return JSON form web2py, but here we consider the case of a JSON service. For example −

def consumer():return dict()@service.json
def get_days():return ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
def call():return service()

Here, we observe that −

  • the function just returns an empty dictionary to render the view, which will consume the service.

  • get_days defines the service, and the function call exposes all registered services.

  • get_days does not need to be in the controller, and can be in a model.

  • call is always in the default.py scaffolding controller.

View with the consumer actions are as follows −

{{extend ''layout.html''}}
<div id = "target"></div>

<script>
   jQuery.getJSON("{{= URL(''call'',args = [''json'',''get_days''])}}",
      function(msg){
         jQuery.each(msg, function(){ jQuery("#target").
         append(this &plus; "<br />"); } )
      }
   );
</script>

The first argument of jQuery.getJSON is the URL of the following service − http://127.0.0.1:8000/app/default/call/json/get_days

This always follows the pattern −

http://<domain>/<app>/<controller>/call/<type>/<service>

The URL is in between {{…}}, because it is resolved at the server-side, while everything else is executed at the client-side. The second argument of jQuery.getJSON is a callback, which will be passed the JSON response.

In this case, the callback loops over each item in the response (a list of week days as strings), and appends each string, followed by a <br/> to the <div id = “target”>.

In this way, web2py manages implementation of web services using jQuery.getJSON.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *