示例#1
0
    def setUpClass(self):
        """Database setup before the CRUD tests."""
        print("Creating a temporary datatbsse...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.session = session

        self.doc = doc_maker.create_doc(doc, self.HYDRUS_SERVER_URL,
                                        self.API_NAME)

        test_classes = doc_parse.get_classes(self.doc.generate())

        # Getting list of classes from APIDoc
        self.doc_collection_classes = [
            self.doc.collections[i]["collection"].class_.title
            for i in self.doc.collections
        ]
        print(self.doc_collection_classes)
        print(random.choice(self.doc_collection_classes))
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)
        print("Classes and properties added successfully.")
        print("Setup done, running tests...")
示例#2
0
def enter_url():
    global history
    history = []
    if request.method == 'GET':
        # print(render_template("index.html"))
        return render_template("index.html")
        # return '<html><h1>hello</h1></html>'
    if request.method == 'POST':
        if 'url' in request.form:
            url = request.form['url']
            session['url'] = url
            handle_data = querying_mechanism.HandleData()
            global api_doc
            global endpoints
            history.append(url)
            endpoints = handle_data.load_data(url)
            session['entrypoint'] = endpoints
            apidoc1 = handle_data.load_data(url + "/vocab")
            api_doc = doc_maker.create_doc(apidoc1)
            global facades
            facades = querying_mechanism.QueryFacades(api_doc, session['url'],
                                                      False)
            check_url = str.encode(url)
            querying_mechanism.check_url_exist(check_url, facades)
            print(url, endpoints)
            return render_template("query.html",
                                   apidoc=endpoints,
                                   history=history)
    def setUp(self):
        """Database setup before the tests."""
        super(AuthTestCase, self).setUp()
        print("Creating a temporary datatbase...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.doc = doc_maker.create_doc(doc_writer_sample.api_doc.generate(), self.HYDRUS_SERVER_URL, self.API_NAME)
        self.gs = Getter_setter(self.session, self.HYDRUS_SERVER_URL, self.API_NAME, self.doc, True)
        self.app = app_factory(self.API_NAME, self.gs)
        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)
        add_user(1, "test", self.session)
        self.auth_header = {"Authorization": "Basic " + b64encode(b"1:test").decode("utf-8")}
        self.wrong_id = {"Authorization": "Basic " + b64encode(b"2:test").decode("utf-8")}
        self.wrong_pass = {"Authorization": "Basic " + b64encode(b"1:test2").decode("utf-8")}
        print("Classes, Properties and Users added successfully.")

        print("Setting up Hydrus utilities... ")
        
        print("Creating utilities context... ")

        print("Setup done, running tests...")
示例#4
0
    def setUpClass(self):
        """Database setup before the tests."""
        print("Creating a temporary database...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.app = app_factory(self.API_NAME)
        self.doc = doc_maker.create_doc(doc_writer_sample.api_doc.generate(),
                                        self.HYDRUS_SERVER_URL, self.API_NAME)

        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)

        print("Classes and properties added successfully.")

        print("Setting up Hydrus utilities... ")
        self.api_name_util = set_api_name(self.app, self.API_NAME)
        self.session_util = set_session(self.app, self.session)
        self.doc_util = set_doc(self.app, self.doc)
        self.client = self.app.test_client()

        print("Creating utilities context... ")
        self.api_name_util.__enter__()
        self.session_util.__enter__()
        self.doc_util.__enter__()
        self.client.__enter__()

        print("Setup done, running tests...")
示例#5
0
 def setUp(self):
     url = "https://storage.googleapis.com/api4/api"
     vocab_url = url + "/" + "vocab"
     response = urllib.request.urlopen(vocab_url)
     apidoc = json.loads(response.read().decode('utf-8'))
     api_doc = doc_maker.create_doc(apidoc)
     self.query_facades = querying_mechanism.QueryFacades(api_doc, url, True)
     self.query_facades.initialize(True)
     self.test_database = redis.StrictRedis(host='localhost', port=6379, db=5)
示例#6
0
    def setUpClass(self):
        """Database setup before the tests."""
        print("Creating a temporary datatbase...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))

        self.session = session
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.app = app_factory(self.API_NAME)
        self.doc = doc_maker.create_doc(doc_writer_sample.api_doc.generate(),
                                        self.HYDRUS_SERVER_URL, self.API_NAME)

        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)
        add_user(1, "test", self.session)
        self.auth_header = {
            "X-Authentication": "",
            "Authorization": "Basic " + b64encode(b"1:test").decode("ascii")
        }
        self.wrong_id = {
            "X-Authentication": "",
            "Authorization": "Basic " + b64encode(b"2:test").decode("ascii")
        }
        self.wrong_pass = {
            "X-Authentication": "",
            "Authorization": "Basic " + b64encode(b"1:test2").decode("ascii")
        }
        print("Classes, Properties and Users added successfully.")

        print("Setting up Hydrus utilities... ")
        self.api_name_util = set_api_name(self.app, self.API_NAME)
        self.session_util = set_session(self.app, self.session)
        self.doc_util = set_doc(self.app, self.doc)
        self.auth_util = set_authentication(self.app, True)
        self.token_util = set_token(self.app, False)
        self.client = self.app.test_client()

        print("Creating utilities context... ")
        self.api_name_util.__enter__()
        self.session_util.__enter__()
        self.doc_util.__enter__()
        self.auth_util.__enter__()
        self.token_util.__enter__()
        self.client.__enter__()

        print("Setup done, running tests...")
    def setUpClass(self):
        """Database setup before the CRUD tests."""
        print("Creating a temporary datatbsse...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        session = scoped_session(sessionmaker(bind=engine))
        self.API_NAME = "demoapi"
        self.HYDRUS_SERVER_URL = "http://hydrus.com/"
        self.session = session
        self.doc = doc_maker.create_doc(
            doc_writer_sample.api_doc.generate(), self.HYDRUS_SERVER_URL, self.API_NAME)

        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)
        print("Classes and properties added successfully.")
        print("Setup done, running tests...")
示例#8
0
    def setUp(self):
        """Database setup before the tests."""
        super(ViewsTestCase, self).setUp()
        print("Creating a temporary database...")
        engine = create_engine('sqlite:///:memory:')
        Base.metadata.create_all(engine)
        self.session = scoped_session(sessionmaker(bind=engine))
        self.HYDRUS_SERVER_URL =  "http://hydrus.com"
        self.API_NAME = "demoapi"
        self.doc = doc_maker.create_doc(doc_writer_sample.api_doc.generate(), self.HYDRUS_SERVER_URL, self.API_NAME)
        self.gs = Getter_setter(self.session, self.HYDRUS_SERVER_URL, self.API_NAME, self.doc, False)
        self.app = app_factory(self.API_NAME, self.gs)
        test_classes = doc_parse.get_classes(self.doc.generate())
        test_properties = doc_parse.get_all_properties(test_classes)
        doc_parse.insert_classes(test_classes, self.session)
        doc_parse.insert_properties(test_properties, self.session)

        print("Classes and properties added successfully.")

        print("Setting up Hydrus utilities... ")
        print("Creating utilities context... ")
        print("Setup done, running tests...")
示例#9
0
def query(apidoc, url):
    """
    It uses only for query purpose.
    Querying still user wants or still user enters the exit.
    :param apidoc: Apidocumentation for the given url.
    :param url: url given by user.
    """
    redis_connection = RedisProxy()
    connection = redis_connection.get_connection()
    api_doc = doc_maker.create_doc(apidoc)
    facades = QueryFacades(api_doc, url, False)
    check_url = str.encode(url)
    check_url_exist(check_url,facades)

    while True:
        print("press exit to quit")
        query = input(">>>")
        if query == "exit":
            break
        elif query == "help":
            help()
        else:
            print(facades.user_query(query))
示例#10
0
def startserver(adduser: Tuple, api: str, auth: bool, dburl: str,
                hydradoc: str, port: int, serverurl: str, token: bool,
                serve: None) -> None:
    """
    Python Hydrus CLI

    :param adduser <tuple>  : Contains the user credentials.
    :param api <str>                    : Sets the API name for the server.
    :param auth <bool>                  : Toggles the authentication.
    :param dburl <str>                  : Sets the database URL.
    :param hydradoc <str>               : Sets the link to the HydraDoc file.
    :param port <int>                   : Sets the API server port.
    :param serverurl <str>              : Sets the API server url.
    :param token <bool>                 : Toggle token based user auth.
    :param serve                        : Starts up the server.

    :return                             : None.
    """
    # The database connection URL
    # See http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlalchemy.create_engine for more info
    # DB_URL = 'sqlite:///database.db'
    DB_URL = dburl

    # Define the server URL, this is what will be displayed on the Doc
    HYDRUS_SERVER_URL = "{}:{}/".format(serverurl, str(port))

    # The name of the API or the EntryPoint, the api will be at http://localhost/<API_NAME>
    API_NAME = api

    click.echo("Setting up the database")
    # Create a connection to the database you want to use
    engine = create_engine(DB_URL)

    click.echo("Creating models")
    # Add the required Models to the database
    Base.metadata.create_all(engine)

    # Define the Hydra API Documentation
    # NOTE: You can use your own API Documentation and create a HydraDoc object using doc_maker
    #       Or you may create your own HydraDoc Documentation using doc_writer [see hydrus/hydraspec/doc_writer_sample]
    click.echo("Creating the API Documentation")
    apidoc = doc_maker.create_doc(json.loads(hydradoc.read()),
                                  HYDRUS_SERVER_URL, API_NAME)

    # Start a session with the DB and create all classes needed by the APIDoc
    session = scoped_session(sessionmaker(bind=engine))

    click.echo("Adding Classes and Properties")
    # Get all the classes from the doc
    # You can also pass dictionary defined in hydrus/hydraspec/doc_writer_sample_output.py
    classes = doc_parse.get_classes(apidoc.generate())

    # Get all the properties from the classes
    properties = doc_parse.get_all_properties(classes)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)

    click.echo("Adding authorized users")
    add_user(id_=adduser[0], paraphrase=adduser[1], session=session)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)

    click.echo("Creating the application")
    # Create a Hydrus app with the API name you want, default will be "api"
    app = app_factory(API_NAME)
    # Set the name of the API
    click.echo("Starting the application")
    with set_authentication(app, auth):
        # Use authentication for all requests
        with set_token(app, token):
            with set_api_name(app, api):
                # Set the API Documentation
                with set_doc(app, apidoc):
                    # Set HYDRUS_SERVER_URL
                    with set_hydrus_server_url(app, HYDRUS_SERVER_URL):
                        # Set the Database session
                        with set_session(app, session):
                            # Start the Hydrus app
                            http_server = WSGIServer(('', port), app)
                            click.echo("Server running at:")
                            click.echo(HYDRUS_SERVER_URL + API_NAME)
                            try:
                                http_server.serve_forever()
                            except KeyboardInterrupt:
                                pass
示例#11
0
文件: cli.py 项目: tiger871/hydrus
def startserver(adduser: Tuple, api: str, auth: bool, dburl: str,
                hydradoc: str, port: int, serverurl: str, token: bool,
                serve: None) -> None:
    """
    Python Hydrus CLI

    :param openapi:         : Sets the link to the Open Api Doc file.
    :param adduser <tuple>  : Contains the user credentials.
    :param api <str>        : Sets the API name for the server.
    :param auth <bool>      : Toggles the authentication.
    :param dburl <str>      : Sets the database URL.
    :param hydradoc <str>   : Sets the link to the HydraDoc file
                            (Supported formats - [.py, .jsonld, .yaml])
    :param port <int>       : Sets the API server port.
    :param serverurl <str>  : Sets the API server url.
    :param token <bool>     : Toggle token based user auth.
    :param serve            : Starts up the server.

    :return                 : None.
    """
    # The database connection URL
    # See http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html for more info
    # DB_URL = 'sqlite:///database.db'
    DB_URL = dburl

    # Define the server URL, this is what will be displayed on the Doc
    HYDRUS_SERVER_URL = "{}:{}/".format(serverurl, str(port))

    # The name of the API or the EntryPoint, the api will be at
    # http://localhost/<API_NAME>
    API_NAME = api

    click.echo("Setting up the database")
    # Create a connection to the database you want to use
    engine = create_engine(DB_URL)

    click.echo("Creating models")
    # Add the required Models to the database
    Base.metadata.create_all(engine)

    # Define the Hydra API Documentation
    # NOTE: You can use your own API Documentation and create a HydraDoc object
    # using doc_maker or you may create your own HydraDoc Documentation using
    # doc_writer [see hydrus/hydraspec/doc_writer_sample]
    click.echo("Creating the API Documentation")

    if hydradoc:
        # Getting hydradoc format
        # Currently supported formats [.jsonld, .py, .yaml]
        try:
            hydradoc_format = hydradoc.split(".")[-1]
            if hydradoc_format == 'jsonld':
                with open(hydradoc, 'r') as f:
                    doc = json.load(f)
            elif hydradoc_format == 'py':
                doc = SourceFileLoader("doc",
                                       "./examples/drones/doc.py")\
                    .load_module().doc
            elif hydradoc_format == 'yaml':
                with open(hydradoc, 'r') as stream:
                    doc = parse(yaml.load(stream))
            else:
                raise ("Error - hydradoc format not supported.")

            click.echo("Using %s as hydradoc" % hydradoc)
            apidoc = doc_maker.create_doc(doc, HYDRUS_SERVER_URL, API_NAME)

        except BaseException:
            click.echo("Problem parsing specified hydradoc file, "
                       "using sample hydradoc as default.")
            apidoc = doc_maker.create_doc(api_document, HYDRUS_SERVER_URL,
                                          API_NAME)
    else:
        click.echo("No hydradoc specified, using sample hydradoc as default.")
        apidoc = doc_maker.create_doc(api_document, HYDRUS_SERVER_URL,
                                      API_NAME)

    # Start a session with the DB and create all classes needed by the APIDoc
    session = scoped_session(sessionmaker(bind=engine))

    click.echo("Adding Classes and Properties")
    # Get all the classes from the doc
    # You can also pass dictionary defined in
    # hydrus/hydraspec/doc_writer_sample_output.py
    classes = doc_parse.get_classes(apidoc.generate())

    # Get all the properties from the classes
    properties = doc_parse.get_all_properties(classes)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)

    click.echo("Adding authorized users")
    add_user(id_=adduser[0], paraphrase=adduser[1], session=session)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)

    click.echo("Creating the application")
    # Create a Hydrus app with the API name you want, default will be "api"
    app = app_factory(API_NAME)
    # Set the name of the API
    click.echo("Starting the application")
    with set_authentication(app, auth):
        # Use authentication for all requests
        with set_token(app, token):
            with set_api_name(app, api):
                # Set the API Documentation
                with set_doc(app, apidoc):
                    # Set HYDRUS_SERVER_URL
                    with set_hydrus_server_url(app, HYDRUS_SERVER_URL):
                        # Set the Database session
                        with set_session(app, session):
                            # Start the Hydrus app
                            http_server = WSGIServer(('', port), app)
                            click.echo("Server running at:")
                            click.echo("{}{}".format(HYDRUS_SERVER_URL,
                                                     API_NAME))
                            try:
                                http_server.serve_forever()
                            except KeyboardInterrupt:
                                pass
示例#12
0
    # The name of the API or the EntryPoint, the api will be at http://localhost/<API_NAME>
    API_NAME = "serverapi"

    print("Setting up the database")
    # Create a connection to the database you want to use
    engine = create_engine(DB_URL)

    print("Creating models")
    # Add the required Models to the database
    Base.metadata.create_all(engine)

    # Define the Hydra API Documentation
    # NOTE: You can use your own API Documentation and create a HydraDoc object using doc_maker
    #       Or you may create your own HydraDoc Documentation using doc_writer [see hydrus/hydraspec/doc_writer_sample]
    print("Creating the API Documentation")
    apidoc = doc_maker.create_doc(doc, HYDRUS_SERVER_URL, API_NAME)

    # Start a session with the DB and create all classes needed by the APIDoc
    session = scoped_session(sessionmaker(bind=engine))

    print("Adding Classes and Properties")
    # Get all the classes from the doc
    classes = doc_parse.get_classes(
        apidoc.generate()
    )  # You can also pass dictionary defined in hydrus/hydraspec/doc_writer_sample_output.py

    # Get all the properties from the classes
    properties = doc_parse.get_all_properties(classes)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
示例#13
0
def startserver(adduser, api, auth, hydradoc, port, serve):
    """Python Hydrus CLI"""
    # The database connection URL
    # See http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlalchemy.create_engine for more info
    # DB_URL = 'sqlite:///database.db'
    DB_URL = 'sqlite:///:memory:'

    # Define the server URL, this is what will be displayed on the Doc
    HYDRUS_SERVER_URL = "http://localhost:" + str(port) + "/"

    # The name of the API or the EntryPoint, the api will be at http://localhost/<API_NAME>
    API_NAME = api

    click.echo("Setting up the database")
    # Create a connection to the database you want to use
    engine = create_engine(DB_URL)

    click.echo("Creating models")
    # Add the required Models to the database
    Base.metadata.create_all(engine)

    # Define the Hydra API Documentation
    # NOTE: You can use your own API Documentation and create a HydraDoc object using doc_maker
    #       Or you may create your own HydraDoc Documentation using doc_writer [see hydrus/hydraspec/doc_writer_sample]
    click.echo("Creating the API Documentation")
    apidoc = doc_maker.create_doc(json.loads(hydradoc.read()),
                                  HYDRUS_SERVER_URL, API_NAME)

    # Start a session with the DB and create all classes needed by the APIDoc
    session = scoped_session(sessionmaker(bind=engine))

    click.echo("Adding Classes and Properties")
    # Get all the classes from the doc
    classes = doc_parse.get_classes(
        apidoc.generate()
    )  # You can also pass dictionary defined in hydrus/hydraspec/doc_writer_sample_output.py

    # Get all the properties from the classes
    properties = doc_parse.get_all_properties(classes)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)

    click.echo("Adding authorized users")
    add_user(id_=adduser[0], paraphrase=adduser[1], session=session)

    # Insert them into the database
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)

    getter_setter = Getter_setter(session, HYDRUS_SERVER_URL, API_NAME, apidoc,
                                  True)

    print("Creating the application")
    # Create a Hydrus app with the API name you want, default will be "api"

    app = app_factory(API_NAME, getter_setter)
    # Set the name of the API

    print("Starting the application")

    http_server = WSGIServer(('', 8080), app)

    print("Server running at:")

    print(HYDRUS_SERVER_URL + API_NAME)

    try:
        http_server.serve_forever()
    except KeyboardInterrupt:
        pass
示例#14
0
from hydrus.hydraspec import doc_maker
from astrodemo.hydra_doc import doc as api_document

import yaml

if __name__ == "__main__":
    with open("./astrodemo/astrodemo.yaml", 'r') as stream:
        try:
            documentation = yaml.load(stream)
        except yaml.YAMLError as exc:
            print(exc)
    api_doc = parse(documentation)
    f = open("./astrodemo/hydra_doc.py", "w")
    f.write(api_doc)
    f.close()
    api = doc_maker.create_doc(api_document, HYDRUS_SERVER_URL, API_NAME)

    engine = create_engine(DB_URL)
    session = sessionmaker(bind=engine)()

    print("Droping database if exist")

    Base.metadata.drop_all(engine)
    print("Creating models....")
    Base.metadata.create_all(engine)
    print("Done")
    classes = doc_parse.get_classes(api_document)

    properties = doc_parse.get_all_properties(classes)
    doc_parse.insert_classes(classes, session)
    doc_parse.insert_properties(properties, session)