예제 #1
0
    def __init__(self,
                 protocol="http",
                 host="localhost",
                 port=8529,
                 username="******",
                 password="",
                 client=None):
        """Initialize the wrapper object.

        :param protocol: the internet transfer protocol (default: 'http')
        :type protocol: str
        :param host: ArangoDB host (default: 'localhost')
        :type host: str
        :param port: ArangoDB port (default: 8529)
        :type port: int or str
        :param username: ArangoDB username (default: 'root')
        :type username: str
        :param password: ArangoDB password (default: '')
        :type password: str
        :param client: HTTP client for this wrapper to use
        :type client: arango.clients.base.BaseClient or None
        :raises: ConnectionError
        """
        self.protocol = protocol
        self.host = host
        self.port = port
        self.username = username
        self.password = password

        # Initialize the ArangoDB HTTP Client if not given
        if client is not None:
            self.client = client
        else:
            client_init_data = {"auth": (self.username, self.password)}
            self.client = DefaultClient(client_init_data)

        # Initialize the ArangoDB API wrapper object
        self.api = API(
            protocol=self.protocol,
            host=self.host,
            port=self.port,
            username=self.username,
            password=self.password,
            client=self.client,
        )

        # Check the connection by requesting a header
        res = self.api.head("/_api/version")
        if res.status_code not in HTTP_OK:
            raise ConnectionError(res)

        # Default ArangoDB database wrapper object
        self._default_database = Database(DEFAULT_DATABASE, self.api)

        # Cache for Database objects
        self._database_cache = {DEFAULT_DATABASE: self._default_database}
예제 #2
0
 def _invalidate_database_cache(self):
     """Invalidate the Database objects cache."""
     real_dbs = set(self.databases["all"])
     cached_dbs = set(self._database_cache)
     for db_name in cached_dbs - real_dbs:
         del self._database_cache[db_name]
     for db_name in real_dbs - cached_dbs:
         self._database_cache[db_name] = Database(
             name=db_name,
             api=API(protocol=self.protocol,
                     host=self.host,
                     port=self.port,
                     username=self.username,
                     password=self.password,
                     database=db_name,
                     client=self.client))