def signature(*args, **kw): sig = wsme.signature(*args, **kw) def decorator(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def wrapper(*args, **kwargs): args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, flask.request.args, flask.request.form, flask.request.data, flask.request.mimetype ) if funcdef.pass_request: kwargs[funcdef.pass_request] = flask.request dataformat = get_dataformat() try: result = f(*args, **kwargs) # NOTE: Support setting of status_code with default 20 status_code = funcdef.status_code if isinstance(result, wsme.api.Response): status_code = result.status_code result = result.obj res = flask.make_response( dataformat.encode_result( result, funcdef.return_type ) ) res.mimetype = dataformat.content_type res.status_code = status_code except: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception(exception_info) finally: del exception_info res = flask.make_response(dataformat.encode_error(None, data)) if data['faultcode'] == 'client': res.status_code = 400 elif orig_code and is_valid_code(orig_code): res.status_code = orig_code else: res.status_code = 500 return res wrapper.wsme_func = f return wrapper return decorator
def wrap(wrapped): # adapted from wsmeext.flask (MIT-licensed) wsme.signature(return_type, *arg_types, **options)(wrapped) funcdef = wsme.api.FunctionDefinition.get(wrapped) funcdef.resolve_types(wsme.types.registry) @functools.wraps(wrapped) def replacement(*args, **kwargs): data_only = kwargs.pop('_data_only_', False) if data_only: return wrapped(*args, **kwargs) try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, request.args, request.form, request.data, request.mimetype) except wsme.exc.ClientSideError as e: raise BadRequest(e.faultstring) result = wrapped(*args, **kwargs) # if this is a Response (e.g., a redirect), just return it if isinstance(result, werkzeug.Response): return result # parse the result, if it was a tuple code = 200 headers = {} if isinstance(result, tuple): if len(result) == 2: if isinstance(result[1], dict): result, headers = result else: result, code = result else: result, code, headers = result assert 200 <= code < 299 # convert the objects into jsonable simple types, also checking # the type at the same time result = wsme.rest.json.tojson(funcdef.return_type, result) # and hand to render_response, which will actually # generate the appropriate string form h = _get_handler() return h.render_response(result, code, headers) replacement.__apidoc__ = wrapped.__doc__ return replacement
def wsexpose(*args, **kwargs): pecan_json_decorate = pecan.expose( template='wsmejson:', content_type='application/json', generic=False) pecan_xml_decorate = pecan.expose( template='wsmexml:', content_type='application/xml', generic=False ) sig = wsme.signature(*args, **kwargs) def decorate(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def callfunction(self, *args, **kwargs): try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, pecan.request.params, None, pecan.request.body, pecan.request.content_type ) if funcdef.pass_request: kwargs[funcdef.pass_request] = pecan.request result = f(self, *args, **kwargs) # NOTE: Support setting of status_code with default 201 pecan.response.status = funcdef.status_code if isinstance(result, wsme.api.Response): pecan.response.status = result.status_code result = result.obj except Exception: raise data = wsme.api.format_exception( sys.exc_info(), pecan.conf.get('wsme', {}).get('debug', False) ) if data['faultcode'] == 'Client': pecan.response.status = 400 else: pecan.response.status = 500 return data return dict( datatype=funcdef.return_type, result=result ) pecan_xml_decorate(callfunction) pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction return decorate
def signature(*args, **kwargs): sig = wsme.signature(*args, **kwargs) def decorate(f): args = inspect.getargspec(f)[0] with_self = args[0] == 'self' if args else False f = sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def callfunction(*args): if with_self: if len(args) == 1: self = args[0] request = self.request elif len(args) == 2: self, request = args else: raise ValueError("Cannot do anything with these arguments") else: request = args[0] request.override_renderer = 'wsme' + get_outputformat(request) try: args, kwargs = combine_args(funcdef, ( args_from_args(funcdef, (), request.matchdict), args_from_params(funcdef, request.params), args_from_body(funcdef, request.body, request.content_type) )) wsme.runtime.check_arguments(funcdef, args, kwargs) if funcdef.pass_request: kwargs[funcdef.pass_request] = request if with_self: args.insert(0, self) result = f(*args, **kwargs) return { 'datatype': funcdef.return_type, 'result': result } except Exception: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception(exception_info) if orig_code is not None: data['orig_code'] = orig_code return data finally: del exception_info callfunction.wsme_func = f return callfunction return decorate
def wsexpose(*args, **kwargs): pecan_json_decorate = pecan.expose(template='wsmejson:', content_type='application/json', generic=False) pecan_xml_decorate = pecan.expose(template='wsmexml:', content_type='application/xml', generic=False) sig = wsme.signature(*args, **kwargs) def decorate(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def callfunction(self, *args, **kwargs): try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, pecan.request.params, None, pecan.request.body, pecan.request.content_type) if funcdef.pass_request: kwargs[funcdef.pass_request] = pecan.request result = f(self, *args, **kwargs) # NOTE: Support setting of status_code with default 201 pecan.response.status = funcdef.status_code if isinstance(result, wsme.api.Response): pecan.response.status = result.status_code result = result.obj except Exception: raise data = wsme.api.format_exception( sys.exc_info(), pecan.conf.get('wsme', {}).get('debug', False)) if data['faultcode'] == 'Client': pecan.response.status = 400 else: pecan.response.status = 500 return data return dict(datatype=funcdef.return_type, result=result) pecan_xml_decorate(callfunction) pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction return decorate
def wsexpose(*args, **kwargs): sig = wsme.signature(*args, **kwargs) def decorate(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def callfunction(self, *args, **kwargs): return_type = funcdef.return_type try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, pecan.request.params, None, pecan.request.body, pecan.request.content_type ) if funcdef.pass_request: kwargs[funcdef.pass_request] = pecan.request result = f(self, *args, **kwargs) # NOTE: Support setting of status_code with default 201 pecan.response.status = funcdef.status_code if isinstance(result, wsme.api.Response): pecan.response.status = result.status_code # NOTE(lucasagomes): If the return code is 204 # (No Response) we have to make sure that we are not # returning anything in the body response and the # content-length is 0 if result.status_code == 204: return_type = None elif not isinstance(result.return_type, wsme.types.UnsetType): return_type = result.return_type result = result.obj except: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception( exception_info, pecan.conf.get('wsme', {}).get('debug', False) ) finally: del exception_info if orig_code and is_valid_code(orig_code): pecan.response.status = orig_code else: pecan.response.status = 500 return data if return_type is None: pecan.request.pecan['content_type'] = None pecan.response.content_type = None return '' return dict( datatype=return_type, result=result ) if 'xml' in funcdef.rest_content_types: pecan_xml_decorate(callfunction) pecan_text_xml_decorate(callfunction) if 'json' in funcdef.rest_content_types: pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction return decorate
def wsexpose(*args, **kwargs): sig = wsme.signature(*args, **kwargs) def decorate(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def callfunction(self, *args, **kwargs): try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, pecan.request.params, None, pecan.request.body, pecan.request.content_type ) if funcdef.pass_request: kwargs[funcdef.pass_request] = pecan.request result = f(self, *args, **kwargs) # NOTE: Support setting of status_code with default 201 pecan.response.status = funcdef.status_code if isinstance(result, wsme.api.Response): pecan.response.status = result.status_code result = result.obj except: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception( exception_info, pecan.conf.get('wsme', {}).get('debug', False) ) finally: del exception_info if orig_code and is_valid_code(orig_code): pecan.response.status = orig_code else: pecan.response.status = 500 return data if funcdef.return_type is None: pecan.request.pecan['content_type'] = None pecan.response.content_type = None return '' return dict( datatype=funcdef.return_type, result=result ) if 'xml' in funcdef.rest_content_types: pecan_xml_decorate(callfunction) pecan_text_xml_decorate(callfunction) if 'json' in funcdef.rest_content_types: pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction return decorate
def wsexpose(*args, **kwargs): sig = wsme.signature(*args, **kwargs) def decorate(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def callfunction(self, *args, **kwargs): try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, pecan.request.params, None, pecan.request.body, pecan.request.content_type ) if funcdef.pass_request: kwargs[funcdef.pass_request] = pecan.request result = f(self, *args, **kwargs) # NOTE: Support setting of status_code with default 201 pecan.response.status = funcdef.status_code if isinstance(result, wsme.api.Response): pecan.response.status = result.status_code result = result.obj # for download file in rest api if result == pecan.response: return result except: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception( exception_info, pecan.conf.get('wsme', {}).get('debug', False) ) finally: del exception_info if orig_code and is_valid_code(orig_code): pecan.response.status = orig_code else: pecan.response.status = 500 return data if funcdef.return_type is None: pecan.request.pecan['content_type'] = None pecan.response.content_type = None return '' return dict( datatype=funcdef.return_type, result=result ) pecan_xml_decorate(callfunction) pecan_text_xml_decorate(callfunction) pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction return decorate
def signature(*args, **kw): sig = wsme.signature(*args, **kw) def decorator(f): args = inspect.getargspec(f)[0] ismethod = args and args[0] == 'self' sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(wsme.types.registry) @functools.wraps(f) def wrapper(*args, **kwargs): if ismethod: self, args = args[0], args[1:] args, kwargs = wsme.rest.args.get_args(funcdef, args, kwargs, flask.request.args, flask.request.form, flask.request.data, flask.request.mimetype) if funcdef.pass_request: kwargs[funcdef.pass_request] = flask.request dataformat = get_dataformat() try: if ismethod: args = [self] + list(args) result = f(*args, **kwargs) # NOTE: Support setting of status_code with default 20 status_code = funcdef.status_code if isinstance(result, wsme.api.Response): status_code = result.status_code result = result.obj res = flask.make_response( dataformat.encode_result(result, funcdef.return_type)) res.mimetype = dataformat.content_type res.status_code = status_code except: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception(exception_info) finally: del exception_info res = flask.make_response(dataformat.encode_error(None, data)) if data['faultcode'] == 'client': res.status_code = 400 elif orig_code and is_valid_code(orig_code): res.status_code = orig_code else: res.status_code = 500 return res wrapper.wsme_func = f return wrapper return decorator
def expose(*args, **kwargs): sig = wsme.signature(*args, **kwargs) def decorate(f): sig(f) funcdef = wsme.api.FunctionDefinition.get(f) funcdef.resolve_types(atypes.registry) @functools.wraps(f) def callfunction(self, *args, **kwargs): return_type = funcdef.return_type try: args, kwargs = wsme.rest.args.get_args( funcdef, args, kwargs, pecan.request.params, None, pecan.request.body, pecan.request.content_type) result = f(self, *args, **kwargs) # NOTE: Support setting of status_code with default 201 pecan.response.status = funcdef.status_code if isinstance(result, atypes.Response): pecan.response.status = result.status_code # NOTE(lucasagomes): If the return code is 204 # (No Response) we have to make sure that we are not # returning anything in the body response and the # content-length is 0 if result.status_code == 204: return_type = None elif not isinstance(result.return_type, atypes.UnsetType): return_type = result.return_type result = result.obj except Exception: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = format_exception(exception_info, cfg.CONF.debug_tracebacks_in_api) finally: del exception_info if orig_code and orig_code in http_client.responses: pecan.response.status = orig_code else: pecan.response.status = 500 return data if return_type is None: pecan.request.pecan['content_type'] = None pecan.response.content_type = None return '' return dict(datatype=return_type, result=result) pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction return decorate