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...")
def fetch_apidoc(self) -> dict: if hasattr(self, 'api_doc'): return self.api_doc else: jsonld_api_doc = super().get(self.entrypoint_url + '/vocab').json() self.api_doc = doc_maker.create_doc(jsonld_api_doc) return self.api_doc
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 {}".format(b64encode(b"1:test").decode("ascii")) } self.wrong_id = { "X-Authentication": "", "Authorization": "Basic {}".format(b64encode(b"2:test").decode("ascii")) } self.wrong_pass = { "X-Authentication": "", "Authorization": "Basic {}".format(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 test_doc(constants): """ Generate a test HydraDoc object from a Api Documentation """ HYDRUS_SERVER_URL = constants['HYDRUS_SERVER_URL'] API_NAME = constants['API_NAME'] doc = doc_maker.create_doc(doc_writer_sample.api_doc.generate(), HYDRUS_SERVER_URL, API_NAME) return doc
def fetch_apidoc(self) -> HydraDoc: """Fetches API DOC from Link Header by checking the hydra apiDoc relation and passes the obtained JSON-LD to doc_maker module of hydra_python_core to return HydraDoc which is used by the agent. :return HydraDoc created from doc_maker module """ try: res = super().get(self.entrypoint_url) api_doc_url = res.links[ 'http://www.w3.org/ns/hydra/core#apiDocumentation']['url'] jsonld_api_doc = super().get(api_doc_url).json() self.api_doc = doc_maker.create_doc(jsonld_api_doc, self.entrypoint, self.api_name) return self.api_doc except: print("Error parsing your API Documentation") raise SyntaxError
def test_output(self, mock_doc): """ Test method to check if HydraDoc are instantiated with proper arguments and all necessary functions are called. """ server_url = "http://hydrus.com/" api_name = "test_api" doc_name = 'vocab' class_count = 0 collection_count = 0 # find out the number of classes for class_ in self.doc["supportedClass"]: if 'manages' not in class_: class_count += 1 else: collection_count += 1 # check if apidoc has been created with proper args apidoc = doc_maker.create_doc(self.doc, server_url, api_name) mock_doc.assert_called_once_with(api_name, "Title for the API Documentation", "Description for the API Documentation", api_name, server_url, doc_name) # check if all context keys has been added to apidoc self.assertEqual(mock_doc.return_value.add_to_context.call_count, len( self.doc["@context"].keys())) # check if all classes has been added to apidoc self.assertEqual( mock_doc.return_value.add_supported_class.call_count, class_count-3) self.assertEqual( mock_doc.return_value.add_supported_collection.call_count, collection_count) # check if all base resource and classes has been added self.assertEqual( mock_doc.return_value.add_baseResource.call_count, 1) self.assertEqual( mock_doc.return_value.add_baseCollection.call_count, 1) self.assertEqual( mock_doc.return_value.gen_EntryPoint.call_count, 1) self.assertIsInstance(apidoc, doc_writer.HydraDoc)
def test_fragments(self): server_url = "http://hydrus.com/" api_name = "test_api" self.doc = doc_writer_sample_output.doc apidoc = doc_maker.create_doc(self.doc, server_url, api_name) for class_ in apidoc.parsed_classes: resource = apidoc.parsed_classes[class_]['class'] resource_id = resource.id_ regex = r"(\W*\?resource=\W*)([a-zA-Z]+)" match_groups = re.search(regex, resource_id) assert match_groups.groups()[0] is not None assert match_groups.groups()[1] == class_ for collection in apidoc.collections: resource_id = apidoc.collections[collection]['collection'].collection_id regex = r"(\W*\?resource=\W*)([a-zA-Z]+)" match_groups = re.search(regex, resource_id) assert match_groups.groups()[0] is not None assert match_groups.groups()[1] == collection
def test_output(self, mock_doc): """ Test method to check if HydraDoc are instantiated with proper arguments and all necessary functions are called. """ server_url = "test_url" api_name = "test_api" class_count = 0 # find out the number of classes for class_ in self.doc["supportedClass"]: collection = re.match(r'(.*)Collection(.*)', class_["title"], re.M | re.I) if not collection: class_count += 1 # check if apidoc has been created with proper args apidoc = doc_maker.create_doc(self.doc, server_url, api_name) mock_doc.assert_called_once_with(api_name, self.doc["title"], self.doc["description"], api_name, server_url) # check if all context keys has been added to apidoc self.assertEqual(mock_doc.return_value.add_to_context.call_count, len( self.doc["@context"].keys())) # check if all classes has been added to apidoc self.assertEqual( mock_doc.return_value.add_supported_class.call_count, class_count - 2) # check if all base resource and classes has been added self.assertEqual( mock_doc.return_value.add_baseResource.call_count, 1) self.assertEqual( mock_doc.return_value.add_baseCollection.call_count, 1) self.assertEqual( mock_doc.return_value.gen_EntryPoint.call_count, 1) self.assertIsInstance(apidoc, doc_writer.HydraDoc)
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) print("going for create doc") 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...")
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(">>>").strip() if query == "exit": break elif query == "help": help() else: print(facades.user_query(query))
def handle(self, *args, **options): hydra_settings = settings.REST_HDYRA PORT = hydra_settings["PORT"] API_NAME = hydra_settings["API_NAME"] ENTRY_POINT = hydra_settings["ENTRY_POINT"] HYDRUS_SERVER_URL = hydra_settings["HYDRUS_SERVER_URL"] if options["path"]: # Load file from the specified path pass else: # Load the example file pass apidoc = doc_maker.create_doc(APIDOC_OBJ, HYDRUS_SERVER_URL, API_NAME) 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)
def serve( adduser: tuple, api: str, auth: bool, dburl: str, pagination: bool, hydradoc: str, port: int, pagesize: int, serverurl: str, token: bool, use_db: bool, stale_records_removal_interval: int, ) -> None: """ Starts up the server. \f :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 pagination <bool>: Toggles the pagination. :param hydradoc <str> : Sets the link to the HydraDoc file (Supported formats - [.py, .jsonld, .yaml]) :param port <int> : Sets the API server port. :param pagesize <int> : Sets maximum size of page(view). :param serverurl <str> : Sets the API server url. :param token <bool> : Toggle token based user auth. :stable_records_removal_interval <int> : Interval period between removal of stale modification records. :return : None Raises: Error: If `hydradoc` is not of a supported format[.py, .jsonld, .yaml]. """ # 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 = f"{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, connect_args={"check_same_thread": False}) # 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 hydra_python_core/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", hydradoc).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(f"Using {hydradoc} as hydradoc") apidoc = doc_maker.create_doc(doc, HYDRUS_SERVER_URL, API_NAME) except BaseException: if FOUND_DOC: click.echo("Problem parsing specified hydradoc file" "Using hydradoc from environment variable") else: click.echo("Problem parsing specified hydradoc file, " "using sample hydradoc as default.") apidoc = doc_maker.create_doc(APIDOC_OBJ, HYDRUS_SERVER_URL, API_NAME) else: if FOUND_DOC: click.echo( "No hydradoc specified, using hydradoc from environment variable." ) else: click.echo( "No hydradoc specified, using sample hydradoc as default.\n" "For creating api documentation see this " "https://www.hydraecosystem.org/01-Usage.html#newdoc\n" "You can find the example used in hydrus/samples/hydra_doc_sample.py" ) apidoc = doc_maker.create_doc(APIDOC_OBJ, 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)) # Get all the classes from the doc # You can also pass dictionary defined in # hydra_python_core/doc_writer_sample_output.py classes = doc_parse.get_classes(apidoc) # Insert them into the database if use_db is False: Base.metadata.drop_all(engine) click.echo("Adding Classes and Properties") create_database_tables(classes) click.echo("Creating models") Base.metadata.create_all(engine) # Add authorized users and pass if they already exist click.echo("Adding authorized users") try: add_user(id_=adduser[0], paraphrase=adduser[1], session=session) except UserExists: pass # Insert them into the database click.echo("Creating the application") # Create a Hydrus app with the API name you want, default will be "api" app = app_factory(API_NAME, apidoc.doc_name) # Set the name of the API # Create a socket for the app socketio = create_socket(app, session) click.echo("Starting the application") # # Nested context managers # # Use authentication for all requests # Set the API Documentation # Set HYDRUS_SERVER_URL # Set the Database session # Enable/disable pagination # Set page size of a collection view with set_authentication(app, auth) as _, set_token( app, token) as _, set_api_name(app, api) as _, set_doc( app, apidoc) as _, set_hydrus_server_url( app, HYDRUS_SERVER_URL) as _, set_session( app, session) as _, set_pagination( app, pagination) as _, set_page_size(app, pagesize) as _: # Run a thread to remove stale modification records at some # interval of time. remove_stale_modification_records(session, stale_records_removal_interval) # Start the hydrus app socketio.run(app, port=port) click.echo("Server running at:") click.echo(f"{HYDRUS_SERVER_URL}{API_NAME}")
from flock_drone.api_docs.doc import doc from gevent.pywsgi import WSGIServer if __name__ == "__main__": engine = create_engine(DB_URL) print("Droping database if exist") Base.metadata.drop_all(engine) print("Creating models....") Base.metadata.create_all(engine) print("Done") apidoc = doc_maker.create_doc(doc, HYDRUS_SERVER_URL, API_NAME) session = sessionmaker(bind=engine)() classes = doc_parse.get_classes(apidoc.generate()) properties = doc_parse.get_all_properties(classes) doc_parse.insert_classes(classes, session) doc_parse.insert_properties(properties, session) app = app_factory(API_NAME) with set_doc(app, apidoc): with set_authentication(app, False): with set_token(app, False):
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. Raises: Error: If `hydradoc` is not of a supported format[.py, .jsonld, .yaml]. """ # 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 hydra_python_core/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.\n" "For creating api documentation see this " "https://www.hydraecosystem.org/01-Usage.html#newdoc\n" "You can find the example used in examples/drones/doc.py") 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 # hydra_python_core/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
def drone_doc(constants): API_NAME = constants['API_NAME'] HYDRUS_SERVER_URL = constants['HYDRUS_SERVER_URL'] doc = doc_maker.create_doc(hydra_doc_sample.doc, HYDRUS_SERVER_URL, API_NAME) return doc
from hydrus.data.user import add_user from hydra_python_core import doc_maker from hydrus.utils import (set_session, set_doc, set_hydrus_server_url, set_token, set_api_name, set_authentication) from hydrus.socketio_factory import create_socket logger = logging.getLogger(__file__) # TODO: loading the engine and creating the tables should be handled better engine = create_engine(DB_URL) session = sessionmaker(bind=engine)() # # Load ApiDoc with doc_maker # apidoc = doc_maker.create_doc(APIDOC_OBJ, HYDRUS_SERVER_URL, API_NAME) classes = doc_parse.get_classes(apidoc) try: Base.metadata.drop_all(engine) create_database_tables(classes) Base.metadata.create_all(engine) except Exception: pass AUTH = True TOKEN = True if AUTH: try: add_user(id_=1, paraphrase="test", session=session) except UserExists:
from flock_drone.api_docs.doc import doc from gevent.pywsgi import WSGIServer if __name__ == "__main__": engine = create_engine(DB_URL) print("Droping database if exist") Base.metadata.drop_all(engine) print("Creating models....") Base.metadata.create_all(engine) print("Done") apidoc = doc_maker.create_doc(doc, HYDRUS_SERVER_URL, API_NAME) session = sessionmaker(bind=engine)() classes = doc_parse.get_classes(apidoc.generate()) properties = doc_parse.get_all_properties(classes) doc_parse.insert_classes(classes, session) doc_parse.insert_properties(properties, session) app = app_factory(API_NAME) # Set the API Documentation with set_doc(app, apidoc): # Set HYDRUS_SERVER_URL
import json from hydra_python_core.doc_maker import create_doc from samples.doc_writer_sample_output import doc if __name__ == "__main__": api_doc = create_doc(doc) print(json.dumps(api_doc.generate(), indent=4, sort_keys=True))