def backend(self, adapter):
AUTHOMATIC_HEADER = '<STR_LIT>'<EOL>request_type = adapter.params.get('<STR_LIT:type>', '<STR_LIT>')<EOL>json_input = adapter.params.get('<STR_LIT>')<EOL>credentials = adapter.params.get('<STR_LIT>')<EOL>url = adapter.params.get('<STR_LIT:url>')<EOL>method = adapter.params.get('<STR_LIT>', '<STR_LIT:GET>')<EOL>body = adapter.params.get('<STR_LIT:body>', '<STR_LIT>')<EOL>params = adapter.params.get('<STR_LIT>')<EOL>params = json.loads(params) if params else {}<EOL>headers = adapter.params.get('<STR_LIT>')<EOL>headers = json.loads(headers) if headers else {}<EOL>ProviderClass = Credentials.deserialize(<EOL>self.config, credentials).provider_class<EOL>if request_type == '<STR_LIT>':<EOL><INDENT>jsonp = params.get('<STR_LIT>')<EOL>if ProviderClass.supports_jsonp and method is '<STR_LIT:GET>':<EOL><INDENT>request_type = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>if jsonp:<EOL><INDENT>params.pop('<STR_LIT>')<EOL><DEDENT>request_type = '<STR_LIT>'<EOL><DEDENT><DEDENT>if request_type == '<STR_LIT>':<EOL><INDENT>response = self.access(<EOL>credentials, url, params, method, headers, body)<EOL>result = response.content<EOL>adapter.status = str(response.status) + '<STR_LIT:U+0020>' + str(response.reason)<EOL>for k, v in response.getheaders():<EOL><INDENT>logging.info('<STR_LIT>'.format(k, v))<EOL>adapter.set_header(k, v)<EOL><DEDENT><DEDENT>elif request_type == '<STR_LIT>':<EOL><INDENT>if json_input:<EOL><INDENT>result = self.request_elements(<EOL>json_input=json_input, return_json=True)<EOL><DEDENT>else:<EOL><INDENT>result = self.request_elements(credentials=credentials,<EOL>url=url,<EOL>method=method,<EOL>params=params,<EOL>headers=headers,<EOL>body=body,<EOL>return_json=True)<EOL><DEDENT>adapter.set_header('<STR_LIT:Content-Type>', '<STR_LIT:application/json>')<EOL><DEDENT>else:<EOL><INDENT>result = '<STR_LIT>'<EOL><DEDENT>adapter.set_header(AUTHOMATIC_HEADER, request_type)<EOL>adapter.write(result)<EOL>
Converts a *request handler* to a JSON backend which you can use with
:ref:`authomatic.js <js>`.

Just call it inside a *request handler* like this:

::

    class JSONHandler(webapp2.RequestHandler):
        def get(self):
            authomatic.backend(Webapp2Adapter(self))

:param adapter:
    The only argument is an :doc:`adapter <adapters>`.

The *request handler* will now accept these request parameters:

:param str type:
    Type of the request. Either ``auto``, ``fetch`` or ``elements``.
    Default is ``auto``.

:param str credentials:
    Serialized :class:`.Credentials`.

:param str url:
    URL of the **protected resource** request.

:param str method:
    HTTP method of the **protected resource** request.

:param str body:
    HTTP body of the **protected resource** request.

:param JSON params:
    HTTP params of the **protected resource** request as a JSON object.

:param JSON headers:
    HTTP headers of the **protected resource** request as a
    JSON object.

:param JSON json:
    You can pass all of the aforementioned params except ``type``
    in a JSON object.

    .. code-block:: javascript

        {
            "credentials": "######",
            "url": "https://example.com",
            "method": "POST",
            "params": {"foo": "bar"},
            "headers": {"baz": "bing"},
            "body": "the body of the request"
        }

Depending on the ``type`` param, the handler will either write
a JSON object with *request elements* to the response,
and add an ``Authomatic-Response-To: elements`` response header, ...

.. code-block:: javascript

    {
        "url": "https://example.com/api",
        "method": "POST",
        "params": {
            "access_token": "###",
            "foo": "bar"
        },
        "headers": {
            "baz": "bing",
            "Authorization": "Bearer ###"
        }
    }

... or make a fetch to the **protected resource** and forward
it's response content, status and headers with an additional
``Authomatic-Response-To: fetch`` header to the response.

.. warning::

    The backend will not work if you write anything to the
    response in the handler!