Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
from flask.views import MethodView

class InformationAPI(MethodView):

    def get(self):
        information = Information.from_data(request.data)
        ...

app.add_url_rule('/<info>/informations/', view_func=InformationAPI.as_view('informations'))
  

falcon

Falcon routeannotations for web service operations (GET, PUT, POST, DELETE) are supported. 

In the following example, a default GET operation is ascribed to the functionon_get from GetResourceclass,and the POST and PUT operations to the on_putandon_postfunctions fromPut_PostResourcewith two differents urls routing:

importfalcon
 
classGetResource():
defon_get():
print('on_get function')
 
classPut_PostResource():
defon_put():
print('on_get function')
defon_post():
print('on_get function')
 
app=falcon.App()
 
app.add_route('', GetResource())
app.add_route('/url/example/1', Put_PostResource())
app.add_route('/url/example/2', Put_PostResource())

The link between the GET operation named after the routing URL "/"  and the called functionon_get is represented by an arrow pointing to the function:


Image Added

The name of a saved Web Service Operation object will be generated from the routing URL by adding a final slash when not present. In this example the name of the POST operations is "/url/example/1/" and  "/url/example/2/" after the routing url "/url/example/1" and "/url/example/2".



Image Added

Sinks are supported with the following rules : If no route matches a request, but the path in the requested URI matches a sink prefix, Falcon will pass control to the associated sink, regardless of the HTTP method requested. If the prefix overlaps a registered route template, the route will take precedence and mask the sink.

 In this case Web Service Operation objects generated as sinks will be named as/that/, and not as/this/since another Web Service Operation object exists with an overlapping url.

importfalcon
 
app=falcon.App()
 
classGetResource():
defon_get():
print('on_get function')
 
defsink_method(resp,**kwargs):
resp.body="Sink"
pass
 
app.add_route('this/is/the/way', GetResource())
app.add_sink(sink_method, prefix='/that')#get, post, put & delete routes will be created and linked to sink_method
app.add_sink(sink_method, prefix='/this')#no routes created because Url overlaps another route


Image Added

The optionnal suffixkeyword argument of Falcon add_route is supported. In this way, multiple closely-related routes can be mapped to the same resource.

importfalcon
app=falcon.App()
 
classPrefixResource(object):
 
defon_get(self, req, resp):
pass
 
defon_get_foo(self, req, resp):
pass
 
defon_post_foo(self, req, resp):
pass
 
defon_delete_bar(self, req, resp):
pass
 
app.add_route('get/without/prefix', PrefixResource())
app.add_route('get/and/post/prefix/foo', PrefixResource(), suffix='foo')
app.add_route('delete/prefix/bar', PrefixResource(), suffix='bar')


web2py

Example for GET request:

Code Block
languagepy
from gluon.tools import fetch
page = fetch('http://www.google.com/')



Database access

PEP 249

Simple database queries consistent with the Python Database API Specification (PEP 249) are recognized. This allows to support a large number of important libraries interfacing Python and SQL databases (SQLite, MySQL, etc). The analyzer identifies execute method calls as potential database queries and searches for generic SQL statements passed in as an argument ('SELECT ...", "INSERT ...)". In the example below data from the stocks table is retrieved via a SELECT statement passed explicitly by a string to the execute method of a cursor object.

Code Block
languagepy
# query.py
import sqlite3

conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('SELECT * FROM informations') 

In addition to execute method calls, the analyzer identifies raw method calls which are used in Django framework. SQL queries can be defined directly or via a method.

Code Block
languagepy
from django.db import models
...
def function(self):
    sql = 'SELECT * FROM informations'
    return model.objects.raw(sql)


...