Request lifecycle

In this document you can find how a request is processed by fantastico framework. By default WSGI applications use a dictionary that contains various useful keys:

  • HTTP Headers
  • HTTP Cookies
  • Helper keys (e.g file wrapper).

In fantastico we want to hide the complexity of this dictionary and allow developers to use some standardized objects. Fantastico framework follows a Request / Response paradigm. This mean that for every single http request only one single http response will be generated. Below, you can find a simple example of how requests are processed by fantastico framework:

../_images/request_response_sd.png

In order to not reinvent the wheels fantastico relies on WebOb python framework in order to correctly generate request and response objects. For more information read WebOB Doc [http://docs.webob.org/en/latest/reference.html].

Request middleware

To have very good control of how WSGI environ is wrapped into WebOb request object a middleware component is configured. This is the first middleware that is executed for every single http request.

class fantastico.middleware.request_middleware.RequestMiddleware(app)[source]

This class provides the middleware responsible for converting wsgi environ dictionary into a request. The result is saved into current WSGI environ under key fantastico.request. In addition each new request receives an identifier. If subsequent requests are triggered from that request then they will also receive the same request id.

Request context

In comparison with WebOb Fantastico provides a nice improvement. For facilitating easy development of code, each fantastico request has a special attribute called context. Below you can find the attributes of a request context object:

  • settings facade (Fantastico settings)
  • session (not yet supported)
  • language The current preferred by user. This is determined based on user lang header.
  • user (not yet supported)
class fantastico.middleware.request_context.RequestContext(settings, language)[source]

This class holds various attributes useful giving a context to an http request. Among other things we need to be able to access current language, current session and possible current user profile.

language[source]

Property that holds the current language that must be used during this request.

settings[source]

Property that holds the current settings facade used for accessing fantastico configuration.

wsgi_app[source]

Property that holds the WSGI application instance under which the request is handled.

Obtain request language

class fantastico.locale.language.Language(code)[source]

Class used to define how does language object looks like. There are various use cases for using language but the simplest one is in request context object:

language = request.context.language

if language.code = "en_us":
   print("English (US) language").
else:
   raise Exception("Language %s is not supported." % language.code) 
code[source]

Property that holds the language code. This is readonly because once instantiated we mustn’t be able to change it.

Obtain settings using request

It is recommended to use request.context object to obtain fantastico settings. This hides the complexity of choosing the right configuration and accessing attributes from it.

installed_middleware = request.context.settings.get("installed_middleware")

print(installed_middleware)

For more information about how to configure Fantastico please read Fantastico settings.

Redirect using request

In Fantastico is fairly simply to redirect client to a given location.

class fantastico.routing_engine.custom_responses.RedirectResponse(destination, query_params=None, redirect_status=302)[source]

This class encapsulates the logic for programatically redirecting client from a fantastico controller.

@Controller(url="/redirect/example")
def redirect_to_google(self, request):
    return request.redirect("http://www.google.ro/")

There are some special cases when you would like to pass some query parameters to redirect destination. This is also easily achievable in Fantastico:

@Controller(url="/redirect/example")
def redirect_to_google(self, request):
    return request.redirect("http://www.google.ro/search",
                            query_params=[("q", "hello world")])

The above example will redirect client browser to http://www.google.ro/search?q=hello world [http://www.google.ro/search?q=helloworld]