def _build_swagger_20_schema_views(config): spec = config.registry.settings['pyramid_swagger.schema20'] walker = NodeWalkerForRefFiles() all_files = walker.walk(spec) file_map = {} def view_for_swagger_schema(request): path, ext = os.path.splitext(request.path) ext = ext.lstrip('.') actual_fname = file_map[request.path] with spec.resolver.resolving(actual_fname) as spec_dict: clean_response = strip_xscope(spec_dict) ref_walker = NodeWalkerForCleaningRefs() fixed_spec = ref_walker.walk(clean_response, ext) return fixed_spec for ref_fname in all_files: ref_fname_parts = os.path.splitext(ref_fname) for schema_format in ['yaml', 'json']: route_name = 'pyramid_swagger.swagger20.api_docs.{0}.{1}'\ .format(ref_fname.replace('/', '.'), schema_format) path = '/{0}.{1}'.format(ref_fname_parts[0], schema_format) file_map[path] = ref_fname yield PyramidEndpoint( path=path, route_name=route_name, view=view_for_swagger_schema, renderer=schema_format, )
def _build_dereferenced_swagger_20_schema_views(config): def view_for_swagger_schema(request): settings = config.registry.settings resolved_dict = settings.get('pyramid_swagger.schema20_resolved') if not resolved_dict: spec = settings['pyramid_swagger.schema20'] spec_copy = copy.deepcopy(spec.client_spec_dict) spec_file_name = os.path.basename(urlparse(spec.origin_url).path) defs_dict = {} resolved_dict = resolve_refs(spec, spec_copy, ['/'], spec_file_name, defs_dict) if len(defs_dict) > 0: resolved_dict.update({'definitions': defs_dict}) settings['pyramid_swagger.schema20_resolved'] = resolved_dict return resolved_dict for schema_format in ['yaml', 'json']: route_name = 'pyramid_swagger.swagger20.api_docs.{0}'\ .format(schema_format) yield PyramidEndpoint( path='/swagger.{0}'.format(schema_format), view=view_for_swagger_schema, route_name=route_name, renderer=schema_format, )
def build_swagger_12_resource_listing(resource_listing): """ :param resource_listing: JSON representing a Swagger 1.2 resource listing :type resource_listing: dict :rtype: :class:`pyramid_swagger.model.PyramidEndpoint` """ def view_for_resource_listing(request): # Thanks to the magic of closures, this means we gracefully return JSON # without file IO at request time. return resource_listing return PyramidEndpoint(path='', route_name='pyramid_swagger.swagger12.api_docs', view=view_for_resource_listing, renderer='json')
def build_swagger_20_swagger_schema_views(config): def view_for_swagger_schema(request): settings = config.registry.settings resolved_dict = settings.get('pyramid_swagger.schema20_resolved') if not resolved_dict: spec = settings['pyramid_swagger.schema20'] spec_copy = copy.deepcopy(spec.client_spec_dict) resolved_dict = resolve_refs(spec, spec_copy) settings['pyramid_swagger.schema20_resolved'] = resolved_dict return resolved_dict yield PyramidEndpoint( path='/swagger.json', route_name='pyramid_swagger.swagger20.api_docs.json', view=view_for_swagger_schema, renderer='json', ) yield PyramidEndpoint( path='/swagger.yaml', route_name='pyramid_swagger.swagger20.api_docs.yaml', view=view_for_swagger_schema, renderer='yaml', )
def build_swagger_12_api_declaration(resource_name, api_declaration): """ :param resource_name: The `path` parameter from the resource listing for this resource. :type resource_name: string :param api_declaration: JSON representing a Swagger 1.2 api declaration :type api_declaration: dict :rtype: :class:`pyramid_swagger.model.PyramidEndpoint` """ # NOTE: This means our resource paths are currently constrained to be valid # pyramid routes! (minus the leading /) route_name = 'pyramid_swagger.swagger12.apidocs-{0}'.format(resource_name) return PyramidEndpoint( path='/{0}'.format(resource_name), route_name=route_name, view=build_swagger_12_api_declaration_view(api_declaration), renderer='json')
def _build_dereferenced_swagger_20_schema_views(config): def view_for_swagger_schema(request): settings = config.registry.settings resolved_dict = settings.get('pyramid_swagger.schema20_resolved') if not resolved_dict: resolved_dict = settings['pyramid_swagger.schema20'].flattened_spec settings['pyramid_swagger.schema20_resolved'] = resolved_dict return resolved_dict for schema_format in ['yaml', 'json']: route_name = 'pyramid_swagger.swagger20.api_docs.{0}'\ .format(schema_format) yield PyramidEndpoint( path='/swagger.{0}'.format(schema_format), view=view_for_swagger_schema, route_name=route_name, renderer=schema_format, )