Ejemplo n.º 1
0
    def get(self, keywd):
        from flankers.tools import valid_uuid, families

        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'application/json'
        if not keywd:
            # keywd is None serves the entrypoint view
            from config.config import _VOCS, _REST_SERVICE
            self.response.headers['Access-Control-Expose-Headers'] = 'Link'
            self.response.headers[
                'Link'] = '<' + _HYDRA_VOCAB + '>;rel="http://www.w3.org/ns/hydra/core#apiDocumentation'
            results = [{
                "name":
                f[f.rfind('_') + 1:],
                "collection_ld+json_description":
                _VOCS['subsystems'] + f + '/' + '?format=jsonld',
                "collection_n-triples_description":
                _VOCS['subsystems'] + f,
                "go_to_collection":
                _REST_SERVICE + f
            } for f in families]
            return self.response.write(json.dumps(results, indent=2))
        elif keywd == 'ntriples' and self.request.get('uuid'):
            # url is "url/cots/ntriples?key=<uuid>"
            uuid = self.request.get('uuid')
            if valid_uuid(uuid):
                # return ntriples of the object
                return self.response.write(
                    format_message("N-Triples not yet implemented"))
        elif valid_uuid(keywd):
            # if the url parameter is an hex, this should be a uuid
            # print a single component (in JSON with a link to N-Triples)
            if self.request.get('format') and self.request.get(
                    'format') == 'jsonld':
                # if user asks for JSON-LD
                self.response.headers['Content-Type'] = 'application/ld+json'
                try:
                    body = Component.parse_to_jsonld(keywd)
                except ValueError as e:
                    return self.response.write(format_message(e))
                return self.response.write(body)
            else:
                # serve JSON
                try:
                    body = Component.parse_to_json(keywd)
                except ValueError as e:
                    return self.response.write(format_message(e))
                return self.response.write(body)

        elif keywd in families:
            # if the url parameter is a family name
            # print the list of all the components in that family
            results = Component.restify(Component.get_by_collection(keywd))
            return self.response.write(results)
        else:
            # wrong url parameters
            return self.response.write(
                format_message("Not a valid key/id in URL"))
    def get(self, keywd):
        from flankers.tools import valid_uuid, families

        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = 'application/json'
        if not keywd:
            # keywd is None serves the entrypoint view
            from config.config import _VOCS, _REST_SERVICE
            self.response.headers['Access-Control-Expose-Headers'] = 'Link'
            self.response.headers['Link'] = '<' + _HYDRA_VOCAB + '>;rel="http://www.w3.org/ns/hydra/core#apiDocumentation'
            results = [{"name": f[f.rfind('_') + 1:],
                        "collection_ld+json_description": _VOCS['subsystems'] + f + '/' + '?format=jsonld',
                        "collection_n-triples_description": _VOCS['subsystems'] + f,
                        "go_to_collection": _REST_SERVICE + f}
                       for f in families]
            return self.response.write(json.dumps(results, indent=2))
        elif keywd == 'ntriples' and self.request.get('uuid'):
            # url is "url/cots/ntriples?key=<uuid>"
            uuid = self.request.get('uuid')
            if valid_uuid(uuid):
                # return ntriples of the object
                return self.response.write(format_message("N-Triples not yet implemented"))
        elif valid_uuid(keywd):
            # if the url parameter is an hex, this should be a uuid
            # print a single component (in JSON with a link to N-Triples)
            if self.request.get('format') and self.request.get('format') == 'jsonld':
                # if user asks for JSON-LD
                self.response.headers['Content-Type'] = 'application/ld+json'
                try:
                    body = Component.parse_to_jsonld(keywd)
                except ValueError as e:
                    return self.response.write(format_message(e))
                return self.response.write(body)
            else:
                # serve JSON
                try:
                    body = Component.parse_to_json(keywd)
                except ValueError as e:
                    return self.response.write(format_message(e))
                return self.response.write(body)

        elif keywd in families:
            # if the url parameter is a family name
            # print the list of all the components in that family
            results = Component.restify(Component.get_by_collection(keywd))
            return self.response.write(results)
        else:
            # wrong url parameters
            return self.response.write(format_message("Not a valid key/id in URL"))
Ejemplo n.º 3
0
    def get(self, name=None):
        """
        publish EntryPoint, Collection and Resource JSON documents
        :return: Json
        """
        from flankers.tools import valid_uuid, families

        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = _CONTENT_TYPE
        self.response.headers['Access-Control-Expose-Headers'] = 'Link'
        self.response.headers[
            'Link'] = '<' + _HYDRA_VOCAB + '>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"'

        if not name:
            # return a hydra:EntryPoint > GET: /hydra/spacecraft/
            self.response.status = 200
            return self.response.write(
                json.dumps(
                    {
                        "@context": _SERVICE + "/hydra/contexts/EntryPoint",
                        "@id": _SERVICE + "/hydra/spacecraft/",
                        "@type": "EntryPoint",
                        "go_to_subsystems":
                        _SERVICE + "/hydra/spacecraft/subsystems",
                        "register_component": "under-construction"
                    },
                    indent=2))
            pass
        elif name == 'subsystems':
            uuid = self.request.get('uuid')
            if uuid and uuid in families:
                # return a hydra:Resource component > GET: /hydra/spacecraft/subsystems?uuid=<uuid>
                result = {
                    "@context":
                    _SERVICE + "/hydra/contexts/Subsystem",
                    "@id":
                    _SERVICE +
                    "/hydra/spacecraft/subsystems?uuid={}".format(uuid),
                    "@type":
                    "Subsystem",
                    "name":
                    uuid,
                    "application/n-triples":
                    "http://ontology.projectchronos.eu/subsystems/{}".format(
                        uuid),
                    "go_to_components":
                    _SERVICE +
                    "/hydra/spacecraft/components?uuid={}".format(uuid),
                    "application/ld+json":
                    "http://ontology.projectchronos.eu/subsystems/{}/?format=jsonld"
                    .format(uuid),
                    "is_open":
                    True,
                }
                return self.response.write(json.dumps(result, indent=2))
            # return hydra:Collection
            results = {
                "@context": _SERVICE + "/hydra/contexts/Collection",
                "@type": "Collection",
                "@id": _SERVICE + "/hydra/spacecraft/subsystems",
                "members": []
            }
            [
                results["members"].append({
                    "@id":
                    _SERVICE +
                    "/hydra/spacecraft/subsystems?uuid={}".format(f),
                    "@type":
                    "vocab:Subsystem"
                }) for f in families
            ]
            self.response.status = 200
            return self.response.write(json.dumps(results, indent=2))

        elif name == 'components':
            uuid = self.request.get('uuid')
            if uuid and valid_uuid(uuid):
                # return a hydra:Resource component > /hydra/spacecraft/components?uuid=<uuid>
                try:
                    from datastore.models import Component
                    body = Component.parse_to_jsonld(uuid)
                except ValueError as e:
                    return self.response.write(format_message(e))
                return self.response.write(body)
            elif uuid in families:
                # return hydra:Collection components by family > /hydra/spacecraft/components?uuid=<subsystem_name>
                from datastore.models import Component
                body = Component.hydrafy(Component.get_by_collection(uuid))
                return self.response.write(body)
            # return hydra:Collection > /hydra/spacecraft/subsystems
            self.response.status = 404
            return self.response.write(
                format_message(
                    '/hydra/spacecraft/components/ GET: needs a "uuid" parameter to be specified; it can be an hex or a subsystem-name',
                    root='hydra'))

        self.response.status = 404
        return self.response.write(format_message('Wrong URL', root='hydra'))
Ejemplo n.º 4
0
    def get(self, name=None):
        """
        publish EntryPoint, Collection and Resource JSON documents
        :return: Json
        """
        from flankers.tools import valid_uuid, families

        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Content-Type'] = _CONTENT_TYPE
        self.response.headers['Access-Control-Expose-Headers'] = 'Link'
        self.response.headers['Link'] = '<' + _HYDRA_VOCAB + '>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"'

        if not name:
            # return a hydra:EntryPoint > GET: /hydra/spacecraft/
            self.response.status = 200
            return self.response.write(json.dumps({
                "@context": _SERVICE + "/hydra/contexts/EntryPoint",
                "@id": _SERVICE + "/hydra/spacecraft/",
                "@type": "EntryPoint",
                "go_to_subsystems": _SERVICE + "/hydra/spacecraft/subsystems",
                "register_component": "under-construction"
            }, indent=2))
            pass
        elif name == 'subsystems':
            uuid = self.request.get('uuid')
            if uuid and uuid in families:
                # return a hydra:Resource component > GET: /hydra/spacecraft/subsystems?uuid=<uuid>
                result = {
                    "@context": _SERVICE + "/hydra/contexts/Subsystem",
                    "@id": _SERVICE + "/hydra/spacecraft/subsystems?uuid={}".format(uuid),
                    "@type": "Subsystem",
                    "name": uuid,
                    "application/n-triples": "http://ontology.projectchronos.eu/subsystems/{}".format(uuid),
                    "go_to_components": _SERVICE + "/hydra/spacecraft/components?uuid={}".format(uuid),
                    "application/ld+json": "http://ontology.projectchronos.eu/subsystems/{}/?format=jsonld".format(uuid),
                    "is_open": True,

                }
                return self.response.write(json.dumps(result, indent=2))
            # return hydra:Collection
            results = {
                "@context": _SERVICE + "/hydra/contexts/Collection",
                "@type": "Collection",
                "@id": _SERVICE + "/hydra/spacecraft/subsystems",
                "members": []
            }
            [results["members"].append({
                "@id": _SERVICE + "/hydra/spacecraft/subsystems?uuid={}".format(f),
                "@type": "vocab:Subsystem"})
                for f in families]
            self.response.status = 200
            return self.response.write(json.dumps(results, indent=2))

        elif name == 'components':
            uuid = self.request.get('uuid')
            if uuid and valid_uuid(uuid):
                # return a hydra:Resource component > /hydra/spacecraft/components?uuid=<uuid>
                try:
                    from datastore.models import Component
                    body = Component.parse_to_jsonld(uuid)
                except ValueError as e:
                    return self.response.write(format_message(e))
                return self.response.write(body)
            elif uuid in families:
                # return hydra:Collection components by family > /hydra/spacecraft/components?uuid=<subsystem_name>
                from datastore.models import Component
                body = Component.hydrafy(Component.get_by_collection(uuid))
                return self.response.write(body)
            # return hydra:Collection > /hydra/spacecraft/subsystems
            self.response.status = 404
            return self.response.write(
                format_message('/hydra/spacecraft/components/ GET: needs a "uuid" parameter to be specified; it can be an hex or a subsystem-name', root='hydra')
            )

        self.response.status = 404
        return self.response.write(
            format_message('Wrong URL', root='hydra')
        )