Beispiel #1
0
    def __init__(self, media_type=DEFAULT_MEDIA_TYPE,
                 request_type=Request, response_type=Response,
                 middleware=None, router=None,
                 independent_middleware=False):
        self._sinks = []
        self._media_type = media_type
        self._static_routes = []

        # set middleware
        self._middleware = helpers.prepare_middleware(
            middleware, independent_middleware=independent_middleware)
        self._independent_middleware = independent_middleware

        self._router = router or routing.DefaultRouter()
        self._router_search = helpers.make_router_search(self._router)

        self._request_type = request_type
        self._response_type = response_type

        self._error_handlers = []
        self._serialize_error = helpers.default_serialize_error

        self.req_options = RequestOptions()
        self.resp_options = ResponseOptions()

        self.req_options.default_media_type = media_type
        self.resp_options.default_media_type = media_type

        # NOTE(kgriffs): Add default error handlers
        self.add_error_handler(falcon.HTTPError, self._http_error_handler)
        self.add_error_handler(falcon.HTTPStatus, self._http_status_handler)
Beispiel #2
0
    def __init__(self,
                 media_type=DEFAULT_MEDIA_TYPE,
                 before=None,
                 after=None,
                 request_type=Request,
                 response_type=Response,
                 middleware=None,
                 router=None):
        self._sinks = []
        self._media_type = media_type

        self._before = helpers.prepare_global_hooks(before)
        self._after = helpers.prepare_global_hooks(after)

        # set middleware
        self._middleware = helpers.prepare_middleware(middleware)

        self._router = router or routing.DefaultRouter()

        self._request_type = request_type
        self._response_type = response_type

        self._error_handlers = []
        self._serialize_error = helpers.default_serialize_error
        self.req_options = RequestOptions()
Beispiel #3
0
    def __init__(self, media_type=DEFAULT_MEDIA_TYPE,
                 request_type=Request, response_type=Response,
                 middleware=None, router=None,
                 independent_middleware=True):
        self._sinks = []
        self._media_type = media_type
        self._static_routes = []

        # set middleware
        self._middleware = helpers.prepare_middleware(
            middleware, independent_middleware=independent_middleware)
        self._independent_middleware = independent_middleware

        self._router = router or routing.DefaultRouter()
        self._router_search = self._router.find

        self._request_type = request_type
        self._response_type = response_type

        self._error_handlers = []
        self._serialize_error = helpers.default_serialize_error

        self.req_options = RequestOptions()
        self.resp_options = ResponseOptions()

        self.req_options.default_media_type = media_type
        self.resp_options.default_media_type = media_type

        # NOTE(kgriffs): Add default error handlers
        self.add_error_handler(falcon.HTTPError, self._http_error_handler)
        self.add_error_handler(falcon.HTTPStatus, self._http_status_handler)
Beispiel #4
0
    def __init__(
        self,
        media_type=DEFAULT_MEDIA_TYPE,
        before=None,
        after=None,
        request_type=Request,
        response_type=Response,
        middleware=None,
        router=None,
    ):
        self._sinks = []
        self._media_type = media_type

        self._before = helpers.prepare_global_hooks(before)
        self._after = helpers.prepare_global_hooks(after)

        # set middleware
        self._middleware = helpers.prepare_middleware(middleware)

        self._router = router or routing.DefaultRouter()

        self._request_type = request_type
        self._response_type = response_type

        self._error_handlers = []
        self._serialize_error = helpers.default_serialize_error
        self.req_options = RequestOptions()
Beispiel #5
0
def swaggerify(app, name, version, **kwargs):
    assert(isinstance(app, falcon.api.API))

    swagger_def = {'info': {'name': name, 'version': version }}
    swagger_def['info'].update(kwargs.pop('info', {}))
    swagger_def.update(kwargs)
    falcon.API.__swagger__ = swagger_def
    app._middleware += helpers.prepare_middleware([CorsMiddleware()])
    app.add_route('/swagger.json', SwaggerConfigResource(app))
Beispiel #6
0
    def __init__(self,
                 media_type=DEFAULT_MEDIA_TYPE,
                 request_type=Request,
                 response_type=Response,
                 middleware=None,
                 router=None,
                 independent_middleware=True,
                 cors_enable=False):
        self._sinks = []
        self._media_type = media_type
        self._static_routes = []

        if cors_enable:
            cm = CORSMiddleware()

            if middleware is None:
                middleware = [cm]
            else:
                try:
                    # NOTE(kgriffs): Check to see if middleware is an
                    #   iterable, and if so, append the CORSMiddleware
                    #   instance.
                    iter(middleware)
                    middleware = list(middleware)
                    middleware.append(cm)
                except TypeError:
                    # NOTE(kgriffs): Assume the middleware kwarg references
                    #   a single middleware component.
                    middleware = [middleware, cm]

        # set middleware
        self._middleware = helpers.prepare_middleware(
            middleware, independent_middleware=independent_middleware)
        self._independent_middleware = independent_middleware

        self._router = router or routing.DefaultRouter()
        self._router_search = self._router.find

        self._request_type = request_type
        self._response_type = response_type

        self._error_handlers = []
        self._serialize_error = helpers.default_serialize_error

        self.req_options = RequestOptions()
        self.resp_options = ResponseOptions()

        self.req_options.default_media_type = media_type
        self.resp_options.default_media_type = media_type

        # NOTE(kgriffs): Add default error handlers
        self.add_error_handler(Exception, self._python_error_handler)
        self.add_error_handler(falcon.HTTPError, self._http_error_handler)
        self.add_error_handler(falcon.HTTPStatus, self._http_status_handler)
Beispiel #7
0
    def set_middleware(self, independent_middleware=True):
        
        middleware = [self.multipart_middleware]

        if self.auth:
            middleware.append(self.auth_middleware)
        
        if self.cors_origins:
            cors = CORS(allow_origins_list=self.cors_origins)
            middleware.append(cors.middleware)

        self._middleware = helpers.prepare_middleware(
            middleware, independent_middleware=independent_middleware)
        self._independent_middleware = independent_middleware        
Beispiel #8
0
def register_resource(app, prefix, resource):
    item_res = ItemFalconResource(resource)
    coll_res = CollectionFalconResource(resource)

    prefix += "/%s" % (resource._meta.resource_name)

    app.add_route(prefix + '/', coll_res)
    app.add_route(prefix + '/{%s}' % resource._meta.primary_key, item_res)

    for func_name, uri, http_methods in resource.custom_methods:
        func = getattr(resource, func_name)

        custom_res = create_func_resource(func, http_methods)
        app.add_route(prefix + uri, custom_res)

    if resource._meta.http_cache and resource._meta.cache:
        middleware = HTTPCache(resource._meta.cache, resource)
        app._middleware.extend(prepare_middleware([middleware]))
Beispiel #9
0
 def _install_middlewares(self):
     middlewares = [m(*margs, **mkwargs) for m, margs, mkwargs in self.middlewares]
     self.api._middleware = falcon_helpers.prepare_middleware(middlewares)
Beispiel #10
0
 def load_middlewares(self):
     middleware = [get_function(i)[0]() for i in self.config.MIDDLEWARES]
     self._middleware = helpers.prepare_middleware(middleware)