Request lifecycle — fantastico 0.1.0-b1 documentation

### Navigation

- index
- next |
- previous |
- fantastico 0.1.0-b1 documentation »
- Fantastico features »

# 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:

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.

## 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)

  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.

## 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)

  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`

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

  `settings`

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

## Obtain request language

class `fantastico.locale.language.` `Language`(code)

  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`

    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.

### Navigation

- index
- next |
- previous |
- fantastico 0.1.0-b1 documentation »
- Fantastico features »