Example #1
0
def main(args):
    """Read user input from stdin until either quit, exit or CTRL-D is entered.
    """
    # Initialize the url factory and read default values.
    app_dir = get_base_directory()
    config_file = os.path.join(app_dir, CONFIG_FILE)
    if len(args) in [1, 2] and args[0] == 'init':
        if len(args) == 2:
            url = args[1]
        elif not os.path.isfile(config_file):
            url = 'http://localhost:5000/vizier-db/api/v1'
        else:
            # Print the URL of the API in the configuration file
            config = read_object_from_file(config_file)
            print_header()
            print('\nConnected to API at ' + config['url'])
            return
        if not os.path.isdir(app_dir):
            os.makedirs(app_dir)
        with open(config_file, 'w') as f:
            json.dump({'url': url}, f)
    elif not os.path.isfile(config_file):
        raise ValueError('vizier client is not initialized')
    else:
        config = read_object_from_file(config_file)
        defaults_file = os.path.join(app_dir, 'defaults.json')
        defaults = PersistentAnnotationSet(object_path=defaults_file)
        CommandInterpreter(urls=UrlFactory(base_url=config['url']),
                           defaults=defaults).eval(args)
Example #2
0
def get_url_factory(
        config: AppConfig, 
        projects: ProjectCache
    ) -> UrlFactory:
    """Get the url factory for a given configuration. In most cases we use the
    default url factory. Only for the configuration where each project is
    running in a separate container we need a different factory.

    Parameter
    ---------
    config: vizier.config.app.AppConfig
        Application configuration object
    projects: vizier.engine.project.cache.base.ProjectCache
        Cache for projects (only used for container engine)

    Returns
    -------
    vizier.api.routes.base.UrlFactory
    """
    if config.engine.identifier == base.CONTAINER_ENGINE:
        return ContainerEngineUrlFactory(
            base_url=config.app_base_url,
            api_doc_url=config.webservice.doc_url,
            projects=cast(ContainerProjectCache, projects)
        )
    else:
        return UrlFactory(
            base_url=config.app_base_url,
            api_doc_url=config.webservice.doc_url
        )
Example #3
0
 def test_init_url_factory(self):
     """Test initializing the main url factory."""
     urls = UrlFactory(base_url='http://abc.com/////')
     self.assertEqual(urls.base_url, 'http://abc.com')
     self.assertIsNone(urls.api_doc_url)
     urls = UrlFactory(base_url='http://abc.com/////', api_doc_url='ABC')
     self.assertEqual(urls.base_url, 'http://abc.com')
     self.assertEqual(urls.api_doc_url, 'ABC')
     # Override API doc url via properties
     urls = UrlFactory(base_url='http://abc.com/////',
                       api_doc_url='ABC',
                       properties={PROPERTIES_APIDOCURL: 'XYZ'})
     self.assertEqual(urls.base_url, 'http://abc.com')
     self.assertEqual(urls.api_doc_url, 'XYZ')
     # Override base url via properties
     urls = UrlFactory(base_url='http://abc.com/////',
                       api_doc_url='ABC',
                       properties={PROPERTIES_BASEURL: 'XYZ'})
     self.assertEqual(urls.base_url, 'XYZ')
     self.assertEqual(urls.api_doc_url, 'ABC')
     # Initialize only via properties
     urls = UrlFactory(properties={
         PROPERTIES_BASEURL: 'XYZ',
         PROPERTIES_APIDOCURL: 'ABC'
     })
     self.assertEqual(urls.base_url, 'XYZ')
     self.assertEqual(urls.api_doc_url, 'ABC')
     # Value error if base url is not set
     with self.assertRaises(ValueError):
         urls = UrlFactory(api_doc_url='ABC',
                           properties={PROPERTIES_APIDOCURL: 'XYZ'})
Example #4
0
    def get_datastore(self, identifier):
        """Get the datastore client for the project with the given identifier.

        Paramaters
        ----------
        identifier: string
            Unique identifier for datastore

        Returns
        -------
        vizier.api.client.datastore.base.DatastoreClient
        """
        return DatastoreClient(
            urls=DatastoreClientUrlFactory(urls=UrlFactory(
                base_url=self.webservice_url),
                                           project_id=identifier))
"""Test the vizier client datastore. This requires the web service API to run
and a project with datastores to have been setup.
"""

from vizier.api.client.base import VizierApiClient
from vizier.api.client.datastore.base import DatastoreClient
from vizier.api.routes.base import UrlFactory
from vizier.api.routes.datastore import DatastoreClientUrlFactory
from vizier.datastore.dataset import DatasetColumn, DatasetRow

from atexit import register as at_exit

URLS = UrlFactory(base_url='http://localhost:5000/vizier-db/api/v1')

api = VizierApiClient(URLS)
PROJECT_ID = api.create_project({"name": "Test Client Datastore"}).identifier

at_exit(api.delete_project, PROJECT_ID)

# We're just doing some unit testing on the fields specific to DatastoreClient, so
# ignore complaints about instantiating an abstract class
store = DatastoreClient(  # type: ignore[abstract]
    urls=DatastoreClientUrlFactory(urls=URLS, project_id=PROJECT_ID))

ds = store.create_dataset(columns=[
    DatasetColumn(identifier=0, name='Name'),
    DatasetColumn(identifier=1, name='Age', data_type="int")
],
                          rows=[
                              DatasetRow(identifier=0, values=['Alice', 32]),
                              DatasetRow(identifier=1, values=['Bob', 23])