def get_operation(self, rule, view, parent=None): annotation = resolve_annotations(view, 'docs', parent) docs = merge_recursive(annotation.options) operation = { 'responses': self.get_responses(view, parent), 'parameters': self.get_parameters(rule, view, docs, parent), } docs.pop('params', None) return merge_recursive([operation, docs])
def get_parameters(self, rule, view, docs, parent=None): annotation = resolve_annotations(view, 'args', parent) args = merge_recursive(annotation.options) schema = args.get('args', {}) if is_instance_or_subclass(schema, Schema): converter = swagger.schema2parameters elif callable(schema): schema = schema(request=None) if is_instance_or_subclass(schema, Schema): converter = swagger.schema2parameters else: converter = swagger.fields2parameters else: converter = swagger.fields2parameters options = copy.copy(args.get('kwargs', {})) locations = options.pop('locations', None) if locations: options['default_in'] = locations[0] if parse_version(apispec.__version__) < parse_version('0.20.0'): options['dump'] = False options['spec'] = self.app.config.get('APISPEC_SPEC', None) rule_params = rule_to_params(rule, docs.get('params')) or [] extra_params = converter(schema, **options) if args else [] return extra_params + rule_params
def wrapped(*args, **kwargs): instance = args[0] if func.__apispec__.get('ismethod') else None annotation = utils.resolve_annotations(func, 'wrapper', instance) wrapper_cls = utils.merge_recursive(annotation.options).get( 'wrapper', Wrapper) wrapper = wrapper_cls(func, instance) return wrapper(*args, **kwargs)
def get_parameters(self, rule, view, docs, parent=None): annotation = resolve_annotations(view, 'args', parent) args = merge_recursive(annotation.options) converter = (swagger.schema2parameters if is_instance_or_subclass( args.get('args', {}), Schema) else swagger.fields2parameters) options = copy.copy(args.get('kwargs', {})) locations = options.pop('locations', None) if locations: options['default_in'] = locations[0] return converter(args.get('args', {}), dump=False, ** options) + rule_to_params(rule, docs.get('params'))
def marshal_result(self, unpacked, status_code): config = flask.current_app.config format_response = config.get('APISPEC_FORMAT_RESPONSE', flask.jsonify) or identity annotation = utils.resolve_annotations(self.func, 'schemas', self.instance) schemas = utils.merge_recursive(annotation.options) schema = schemas.get(status_code, schemas.get('default')) if schema and annotation.apply is not False: schema = utils.resolve_instance(schema['schema']) output = schema.dump(unpacked[0]).data else: output = unpacked[0] return format_output((format_response(output), ) + unpacked[1:])
def marshal_result(self, unpacked, status_code): config = flask.current_app.config format_response = config.get('APISPEC_FORMAT_RESPONSE', flask.jsonify) or identity annotation = utils.resolve_annotations(self.func, 'schemas', self.instance) schemas = utils.merge_recursive(annotation.options) schema = schemas.get(status_code, schemas.get('default')) if schema and annotation.apply is not False: schema = utils.resolve_schema(schema['schema'], request=flask.request) dumped = schema.dump(unpacked[0]) output = dumped.data if MARSHMALLOW_VERSION_INFO[0] < 3 else dumped else: output = unpacked[0] return format_output((format_response(output), ) + unpacked[1:])
def get_parameters(self, rule, view, docs, parent=None): annotation = resolve_annotations(view, 'args', parent) args = merge_recursive(annotation.options) converter = (swagger.schema2parameters if is_instance_or_subclass( args.get('args', {}), Schema) else swagger.fields2parameters) options = copy.copy(args.get('kwargs', {})) locations = options.pop('locations', None) if locations: options['default_in'] = locations[0] if parse_version(apispec.__version__) < parse_version('0.20.0'): options['dump'] = False rule_params = rule_to_params(rule, docs.get('params')) or [] extra_params = converter(args.get('args', {}), ** options) if args else [] return rule_params + extra_params
def get_parameters(self, rule, view, docs, parent=None): annotation = resolve_annotations(view, 'args', parent) args = merge_recursive(annotation.options) converter = ( swagger.schema2parameters if is_instance_or_subclass(args.get('args', {}), Schema) else swagger.fields2parameters ) options = copy.copy(args.get('kwargs', {})) locations = options.pop('locations', None) if locations: options['default_in'] = locations[0] return converter( args.get('args', {}), dump=False, **options ) + rule_to_params(rule, docs.get('params'))
def get_responses(self, view, parent=None): annotation = resolve_annotations(view, 'schemas', parent) options = [] for option in annotation.options: exploded = {} for status_code, meta in option.items(): if self.spec.openapi_version.major < 3: meta.pop('content_type', None) exploded[status_code] = meta else: content_type = meta['content_type'] or 'application/json' exploded[status_code] = { 'content': { content_type: { 'schema': meta['schema'] } } } if meta['description']: exploded[status_code]['description'] = meta['description'] options.append(exploded) return merge_recursive(options)
def get_parameters(self, rule, view, docs, parent=None): openapi = self.marshmallow_plugin.openapi annotation = resolve_annotations(view, 'args', parent) args = merge_recursive(annotation.options) schema = args.get('args', {}) if is_instance_or_subclass(schema, Schema): converter = openapi.schema2parameters elif callable(schema): schema = schema(request=None) if is_instance_or_subclass(schema, Schema): converter = openapi.schema2parameters else: converter = openapi.fields2parameters else: converter = openapi.fields2parameters options = copy.copy(args.get('kwargs', {})) locations = options.pop('locations', None) if locations: options['default_in'] = locations[0] rule_params = rule_to_params(rule, docs.get('params')) or [] extra_params = converter(schema, **options) if args else [] return extra_params + rule_params
def get_responses(self, view, parent=None): annotation = resolve_annotations(view, 'schemas', parent) return merge_recursive(annotation.options)
def wrapped(*args, **kwargs): instance = args[0] if func.__apispec__.get('ismethod') else None annotation = utils.resolve_annotations(func, 'wrapper', instance) wrapper_cls = utils.merge_recursive(annotation.options).get('wrapper', Wrapper) wrapper = wrapper_cls(func, instance) return wrapper(*args, **kwargs)