Exemplo n.º 1
0
        def callfunction(self, *args, **kwargs):
            args, kwargs = wsme.rest.args.get_args(
                funcdef, args, kwargs, cherrypy.request.params, None,
                cherrypy.request.body,
                cherrypy.request.headers['Content-Type'])
            if funcdef.pass_request:
                kwargs[funcdef.pass_request] = cherrypy.request
            try:
                result = f(self, *args, **kwargs)
            except Exception:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    if isinstance(orig_exception, cherrypy.HTTPError):
                        orig_code = getattr(orig_exception, 'status', None)
                    else:
                        orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(exception_info)
                finally:
                    del exception_info

                cherrypy.response.status = 500
                if data['faultcode'] == 'client':
                    cherrypy.response.status = 400
                elif orig_code and is_valid_code(orig_code):
                    cherrypy.response.status = orig_code

                accept = cherrypy.request.headers.get('Accept', "").lower()
                accept = util.simplify_http_accept_header(accept)

                decorators = {'text/xml': wsme.rest.xml.encode_error}
                return decorators.get(accept,
                                      wsme.rest.json.encode_error)(None, data)

            return dict(datatype=funcdef.return_type, result=result)
Exemplo n.º 2
0
 def expose(func, *args, **kw):
     request.tg_template_enginename = view.base._choose_engine(template)[2]
     accept = request.headers.get('Accept', "").lower()
     accept = tg_util.simplify_http_accept_header(accept)
     if not hasattr(func, "_expose"):
         _build_rules(func)
     try:
         if hasattr(request, "in_transaction"):
             output = func._expose(func, accept, func._allow_json,
                         *args, **kw)
         else:
             request.in_transaction = True
             output = database.run_with_transaction(
                     func._expose, func, accept, func._allow_json,
                     *args, **kw)
     except NoApplicableMethods, e:
         args = e.args # args from the last generic function call
         if (args and args[0] and isinstance(args[0], tuple)
                 and args[0][0] is func):
             # The error refers to our call above. This means that
             # no suitable controller method was found (probably due
             # to wrong parameters). So we will raise a "not found"
             # error unless a specific error status was already set
             # (e.g. "unauthorized" was set by the identity provider):
             status = cherrypy.response.status
             if status and status // 100 == 4:
                 raise cherrypy.HTTPError(status)
             raise cherrypy.NotFound
         # If the error was raised elsewhere inside the controller,
         # handle it like all other exceptions ("server error"):
         raise
Exemplo n.º 3
0
 def expose(func, *args, **kw):
     accept = request.headers.get('Accept', "").lower()
     accept = tg_util.simplify_http_accept_header(accept)
     if not hasattr(func, "_expose"):
         _build_rules(func)
     if hasattr(request, "in_transaction"):
         output = func._expose(func, accept, func._allow_json,
                     *args, **kw)
     else:
         request.in_transaction = True
         output = profile_expose_method(_run_with_transaction, accept, args, func, kw,
                                        exclude_from_memory_profiling)
     return output
Exemplo n.º 4
0
 def expose(func, *args, **kw):
     accept = request.headers.get('Accept', "").lower()
     accept = tg_util.simplify_http_accept_header(accept)
     if not hasattr(func, "_expose"):
         _build_rules(func)
     if hasattr(request, "in_transaction"):
         output = func._expose(func, accept, func._allow_json,
                     *args, **kw)
     else:
         request.in_transaction = True
         output = profile_expose_method(_run_with_transaction, accept, args, func, kw,
                                        exclude_from_memory_profiling)
     return output
Exemplo n.º 5
0
 def expose(func, *args, **kw):
     accept = request.headers.get('Accept', "").lower()
     accept = tg_util.simplify_http_accept_header(accept)
     if not hasattr(func, "_expose"):
         _build_rules(func)
     if hasattr(request, "in_transaction"):
         output = func._expose(func, accept, func._allow_json,
                     *args, **kw)
     else:
         request.in_transaction = True
         output = database.run_with_transaction(
                 func._expose, func, accept, func._allow_json,
                 *args, **kw)
     return output
Exemplo n.º 6
0
 def expose(func, *args, **kw):
     accept = request.headers.get('Accept', "").lower()
     accept = tg_util.simplify_http_accept_header(accept)
     if not hasattr(func, "_expose"):
         _build_rules(func)
     if hasattr(request, "in_transaction"):
         output = func._expose(func, accept, func._allow_json,
                               *args, **kw)
     else:
         request.in_transaction = True
         output = database.run_with_transaction(
             func._expose, func, accept, func._allow_json, *args,
             **kw)
     return output
Exemplo n.º 7
0
        def callfunction(self, *args, **kwargs):
            args, kwargs = wsme.rest.args.get_args(
                funcdef, args, kwargs,
                cherrypy.request.params, None,
                cherrypy.request.body,
                cherrypy.request.headers['Content-Type']
            )
            if funcdef.pass_request:
                kwargs[funcdef.pass_request] = cherrypy.request
            try:
                result = f(self, *args, **kwargs)
            except:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    if isinstance(orig_exception, cherrypy.HTTPError):
                        orig_code = getattr(orig_exception, 'status', None)
                    else:
                        orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(exception_info)
                finally:
                    del exception_info

                cherrypy.response.status = 500
                if data['faultcode'] == 'client':
                    cherrypy.response.status = 400
                elif orig_code and is_valid_code(orig_code):
                    cherrypy.response.status = orig_code

                accept = cherrypy.request.headers.get('Accept', "").lower()
                accept = util.simplify_http_accept_header(accept)

                decorators = {'text/xml': wsme.rest.xml.encode_error}
                return decorators.get(
                    accept,
                    wsme.rest.json.encode_error
                )(None, data)

            return dict(
                datatype=funcdef.return_type,
                result=result
            )
Exemplo n.º 8
0
def request_format():
    '''Return the output format that was requested by the user.

    The user is able to specify a specific output format using either the
    ``Accept:`` HTTP header or the ``tg_format`` query parameter.  This
    function checks both of those to determine what format the reply should
    be in.

    :rtype: string
    :returns: The requested format.  If none was specified, 'default' is
        returned

    .. versionchanged:: 0.3.17
        Return symbolic names for json, html, xhtml, and xml instead of
        letting raw mime types through
    '''
    output_format = cherrypy.request.params.get('tg_format', '').lower()
    if not output_format:
        ### TODO: Two problems with this:
        # 1) TG lets this be extended via as_format and accept_format.  We need
        #    tie into that as well somehow.
        # 2) Decide whether to standardize on "json" or "application/json"
        accept = tg_util.simplify_http_accept_header(
            request.headers.get('Accept', 'default').lower())
        if accept in ('text/javascript', 'application/json'):
            output_format = 'json'
        elif accept == 'text/html':
            output_format = 'html'
        elif accept == 'text/plain':
            output_format = 'plain'
        elif accept == 'text/xhtml':
            output_format = 'xhtml'
        elif accept == 'text/xml':
            output_format = 'xml'
        else:
            output_format = accept
    return output_format
Exemplo n.º 9
0
def request_format():
    '''Return the output format that was requested by the user.

    The user is able to specify a specific output format using either the
    ``Accept:`` HTTP header or the ``tg_format`` query parameter.  This
    function checks both of those to determine what format the reply should
    be in.

    :rtype: string
    :returns: The requested format.  If none was specified, 'default' is
        returned

    .. versionchanged:: 0.3.17
        Return symbolic names for json, html, xhtml, and xml instead of
        letting raw mime types through
    '''
    output_format = cherrypy.request.params.get('tg_format', '').lower()
    if not output_format:
        ### TODO: Two problems with this:
        # 1) TG lets this be extended via as_format and accept_format.  We need
        #    tie into that as well somehow.
        # 2) Decide whether to standardize on "json" or "application/json"
        accept = tg_util.simplify_http_accept_header(
            request.headers.get('Accept', 'default').lower())
        if accept in ('text/javascript', 'application/json'):
            output_format = 'json'
        elif accept == 'text/html':
            output_format = 'html'
        elif accept == 'text/plain':
            output_format = 'plain'
        elif accept == 'text/xhtml':
            output_format = 'xhtml'
        elif accept == 'text/xml':
            output_format = 'xml'
        else:
            output_format = accept
    return output_format