# Copyright Hybrid Logic Ltd.  See LICENSE file for details.
"""
A HTTP REST API for controlling the Dataset Manager.
"""

import yaml

from twisted.python.filepath import FilePath
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.application.internet import StreamServerEndpointService

from klein import Klein

from ..restapi import structured
from .. import __version__


SCHEMA_BASE = FilePath(__file__).parent().child(b'schema')
SCHEMAS = {
    b'/v1/types.json': yaml.safe_load(
        SCHEMA_BASE.child(b'types.yml').getContent()),
    b'/v1/endpoints.json': yaml.safe_load(
        SCHEMA_BASE.child(b'endpoints.yml').getContent()),
    }


class DatasetAPIUserV1(object):
    """
    A user accessing the API.
    """
    app = Klein()

    def __init__(self, persistence_service):
        pass

    @app.route("/version", methods=['GET'])
    @structured(
        inputSchema={},
        outputSchema={'$ref': '/v1/endpoints.json#/definitions/versions'},
        schema_store=SCHEMAS
    )
    def version(self):
        """
        Return the ``flocker`` version string.
        """
        return {u"flocker":  __version__}

    @app.route("/configuration", methods=["GET"])
    def configuration(self):
        """
        Return the current configuration.
        """
        # For now we're sticking to current config. Later on this will
        # have a datasets key, and maybe omit applications if we have
        # none?
        return {"applications": ...,
                "application_deployment": ...}


def create_api_service(persistence_service, endpoint):
    """
    Create a Twisted Service that serves the API on the given endpoint.
    """
    api_root = Resource()
    api_root.putChild(
        'v1', DatasetAPIUserV1(persistence_service).app.resource())
    return StreamServerEndpointService(endpoint, Site(api_root))

@@ -823,16 +823,21 @@
 _service
+=None
 ):%0A     
@@ -1479,32 +1479,33 @@
 # none?%0A        
+#
 return %7B%22applica
@@ -1521,24 +1521,25 @@
 ..,%0A        
+#
         %22app
@@ -1565,16 +1565,29 @@
 %22: ...%7D%0A
+        pass%0A
 %0A%0Adef cr
@@ -1607,29 +1607,8 @@
 ice(
-persistence_service, 
 endp
@@ -1782,35 +1782,16 @@
 IUserV1(
-persistence_service
 ).app.re

Disable sketch to allow importing this.