Skip to content
This repository has been archived by the owner on Sep 7, 2018. It is now read-only.

Dive Deeper

Daniel Däschle edited this page Feb 5, 2018 · 11 revisions

Shared Directory

If you want to share a directory directly over swapy you can simply use:

...
swapy.shared('shared')
...

In this case the directory shared can be reached over http://yourip/shared. If you use a different directory instead of shared the endpoint remains /shared. For example you can share your css or js files in this directory.

Environment Variables

You need global variables to get access from all modules? Simple use environment variables. It is very simple!

...
swapy.environment({
    'secret_key': 'crypto'
})
...

If you want to access this values you only need to do the follwing.

...
swapy.get_env('secret_key')  # returns 'crypto'
...

If you want to change an environment value during operation you can use swapy.set_env.

...
swapy.set_env('my_key', 'my_value')
...

SSL

This is only for test purposes!

All you need is your IP / Domain and optionally a path to your cert files.

...
swapy.ssl('127.0.0.1')
...

This allows you to use automatically generated SSL certificates for tests. The path parameters have to be defined normally. Otherwise, you have to install the python library OpenSSL using pip or easy_install.

Otherwise, you must add the path to your certificates (without file extensions).

...
swapy.ssl('127.0.0.1', 'path/to/cert')
...

Middlewares

There are actually some built in middlewares available:

  • JsonMiddleware
  • HtmlMiddleware
  • CorsMiddleware
  • ExpectKeysMiddleware (more coming soon)

Using middlewares is easy. First you have to import the desired middleware you want and then use it for all routes.

...
from swapy.middlewares import HtmlMiddleware

swapy.use(HtmlMiddleware)
...

This middleware is only valid for all routes in the module where it is used. If you want to exclude a route from this middleware define it above the use command.

...
# Not affected from JsonMiddleware
@on()
def root():
    return  'Hello swapy!'

use(JsonMiddleware)
...

If you include another module, this is not affected from the middleware. You have to use middlewares in every module seperatly.


You also can add a middleware to a single route. Because middlewares are just only decorators you can decorate the route below the on() decorator.

Example:

...
@on()
@JsonMiddleware
def root():
    return {'message': 'Hello swapy!'}
...

JsonMiddleware

Parses the output from the route to a JSON string.

Possible output types are:

  • str
  • dict
  • list
  • int
  • float
  • None
  • bool

HtmlMiddleware

Appends the Content-Type: application/json header to the response which makes the browser treating the content as HTML.

CorsMiddleware

Appends some CORS headers to the response.

What is CORS? Read this article

ExpectKeysMiddleware

This middleware catches every KeyError and send an error response to the client with status code 400. You can use it when you want to receive a JSON payload which should have some data which you expect.

Warning: It does not serialize the incoming data or return a detailed error to the client.

Exception Handlers

Coming Soon...

Config

Coming Soon...

Cookies

Coming Soon...

Sessions

Coming Soon...