def from_http_req(cls, req): """ :param req: 'falcon.request.Request' object that is queried for various attributes :raises `falcon.HTTPBadRequest` if sort by and sort dir lists are not same length. """ ctx = context.from_http_req(req) limit = req.get_param_as_int('limit') or DEFAULT_LIMIT_RESULTS marker = req.get_param('marker') sort_by = req.get_param_as_list('sort_by') or [] sort_dir = req.get_param_as_list('sort_dir') or [] if len(sort_dir) > 0 and ( len(sort_dir) != len(sort_by)): msg = ("If you supply sort_dir values, you must supply the same " "number of values as sort_by values.") raise falcon.HTTPBadRequest('Bad Request', msg) elif sort_by and not sort_dir: sort_dir = ["asc" for f in sort_by] group_by = req.get_param_as_list('group_by') or [] filters = req._params.copy() for name in ('limit', 'marker', 'sort_by', 'sort_dir', 'group_by'): if name in filters: del filters[name] return cls(ctx, limit=limit, marker=marker, sort_by=sort_by, sort_dir=sort_dir, group_by=group_by, filters=filters)
def inner(resource, request, response, *args, **kwargs): ctx = context.from_http_req(request) ctx.authenticated = authenticate(ctx, request) if ctx.authenticated: return fn(resource, request, response, *args, **kwargs) msg = "Authentication failed." raise falcon.HTTPUnauthorized('Authentication required.', msg, scheme=get_auth_uri())
def from_http_req(cls, req, **overrides): """ Given a supplied dict or list coming from an HTTP API request body that has been deserialized), validate that properties of the list or dict match the JSONSchema for the HTTP method of the object, and return an appropriate object. :param req: The `falcon.request.Request` object. :param **overrides: Any attributes that should be overridden in the serialized body. Useful for when we are dealing with a collection resource's subcollection. For example, when creating a new public key for a user, the HTTP request looks like: POST /users/{user_id}/keys We override the value of the body's userId field (if set) with the value of the {user_id} from the URI itself. :returns An object of the appropriate subclass. :raises `exc.BadInput` if the object model's schema does not match the supplied instance structure or if the request could not be deserialized. """ api_version = version.tuple_from_request(req) ctx = rest_context.from_http_req(req) deser_body = rest_helpers.deserialize(req) if overrides: deser_body.update(overrides) schema = JSONSCHEMA_CATALOG.schema_for_version(req.method, cls._SINGULAR_NAME, api_version) try: jsonschema.validate(deser_body, schema) except jsonschema.ValidationError as e: raise exc.BadInput(e) values = cls.field_names_to_capnp(deser_body) return cls(cls._CAPNP_OBJECT.new_message(**values), ctx=ctx)
def _find_ctx(ctx_or_req): if isinstance(ctx_or_req, context.Context): return ctx_or_req else: return rest_context.from_http_req(ctx_or_req)