def _service_link(name="api.example_service"):
    return links.ServiceLink(
        name="name",
        route_name="api.example_service",
        method="POST",
        description="Create a new Foo",
    )
Ejemplo n.º 2
0
 def test_primary_method_returns_correct_HTTP_method(
     self, name, route_name, method, description, expected_method
 ):
     assert (
         links.ServiceLink(name, route_name, method, description).primary_method()
         == expected_method
     )
Ejemplo n.º 3
0
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)