def test_it_creates_attrs_on_registry_if_not_present( self, versions, pyramid_config): links.register_link(_service_link(), versions, pyramid_config.registry) assert hasattr(pyramid_config.registry, "api_links") assert "v1" in pyramid_config.registry.api_links assert "v2" in pyramid_config.registry.api_links
def test_it_registers_link_for_every_version(self, versions, pyramid_config): link = _service_link() links.register_link(link, versions, pyramid_config.registry) assert link in pyramid_config.registry.api_links["v1"] assert link in pyramid_config.registry.api_links["v2"]
def test_it_does_not_register_link_for_unsupported_versions( self, versions, pyramid_config): first_service = _service_link() second_service = _service_link("doodad") links.register_link(first_service, versions, pyramid_config.registry) links.register_link(second_service, ["v1"], pyramid_config.registry) assert first_service in pyramid_config.registry.api_links["v2"] assert second_service not in pyramid_config.registry.api_links["v2"]
def add_api_view( config, view, versions, link_name=None, description=None, enable_preflight=True, **settings ): """ Add a view configuration for an API view. This configuration takes care of some common tasks for configuring API views: * It registers the view with Pyramid using some appropriate defaults for API method views, e.g. JSON and CORs support. As part of this, it also configures views to respond to requests for different versions of the API, via Accept header negotiation. * If ``link_name`` is present, the view will be registered as one of the available "links" that are returned by the ``api.index`` route for its version(s). :param config: :type config: :class:`pyramid.config.Configurator` :param view: The view callable :param versions: API versions this view supports. Each entry must be one of the versions defined in :py:const:`h.views.api.API_VERSIONS` :type versions: list[string] or None :param str link_name: Dotted path of the metadata for this route in the output of the `api.index` view :param str description: Description of the view to use in `api.index` :param bool enable_preflight: If ```True``, add support for CORS preflight requests for this view. If ``True``, a `route_name` must be specified. :param dict settings: Arguments to pass on to ``config.add_view`` """ settings.setdefault("renderer", "json") settings.setdefault("decorator", (cors_policy, version_media_type_header)) if link_name: link = links.ServiceLink( name=link_name, route_name=settings.get("route_name"), method=settings.get("request_method", "GET"), description=description, ) links.register_link(link, versions, config.registry) if API_VERSION_DEFAULT in versions: # If this view claims to support the default API version, register it # with the default (application/json) media-type accept handler settings.setdefault("accept", "application/json") config.add_view(view=view, **settings) for version in versions: if version not in API_VERSIONS: raise ValueError( "API Configuration Error: Unrecognized API version " + version ) # config.add_view only allows one, string value for `accept`, so we # have to re-invoke it to add additional accept headers settings["accept"] = media_type_for_version(version) config.add_view(view=view, **settings) if enable_preflight: cors.add_preflight_view(config, settings["route_name"], cors_policy)