Skip to main content

Connector

Connector​

Connector(host='localhost', port=DEFAULT_PORT, user='', password='', token='', use_ssl=True, ca_cert=None, shared_data=None, authenticate=True, use_keepalive=True, verify_hostname=True, retry_interval_seconds=DEFAULT_RETRY_INTERVAL_SECONDS, retry_max_attempts=DEFAULT_RETRY_MAX_ATTEMPTS, config: Optional[Configuration] = None, key: Optional[str] = None, connect: bool = False)

Bases: object

Class to facilitate connections with an instance of ApertureDB

It lets the client execute any JSON query based on the ApertureDB query language specification It manages the TCP connection to the database.

Note

  • The connection is established only when a query is run.
  • A new connection is established for each instance that runs a query, and gets closed explicitly by calling close() or at destruction.

Parameters:

NameTypeDescriptionDefault
hoststr

Address of the host to connect to.

'localhost'
portint

Port to connect to.

DEFAULT_PORT
userstr

Username to specify while establishing a connection.

''
passwordstr

Password to specify while connecting to ApertureDB.

''
tokenstr

Token to use while connecting to the database.

''
use_sslbool

Use SSL to encrypt communication with the database.

True
use_keepalivebool

Set keepalive on the connection with the database. This has two benefits: It reduces the chance of disconnection for a long-running query, and it means that disconnections are detected sooner. Turn this off to reduce traffic on high-cost network connections.

True
configConfiguration

Configuration object to use for connection.

None
keystr

Aperture Key, configuration as a deflated compressed string

None

The Configuration class options: host,port,user,password,token,use_ssl,use_keepalive,retry_interval_seconds,retrun_max_attempts are all ignored in the initializer if a config or key is passed in.

If a key is passed in, it is chosen over a config.

shared_data and authenticate are not Configuration class based options, and have no overrides.

Constructor for the Connector class.

authenticate​

authenticate(shared_data, user, password, token)

Authenticate with the database. This will be called automatically from query. This is separate from session refresh mechanism, and is set to be called only once per session. If a Refresh token also fails, this will be called again.

query​

query(q, blobs=[])

Query the database with a query string or a json object. First it checks if the session is valid, if not, it refreshes the token. Then it sends the query to the server and returns the response.

Parameters:

NameTypeDescriptionDefault
qjson

native query to be sent

required
blobslist

Blobs if needed with the query. Defaults to [].

[]

Raises:

TypeDescription
ConnectionError

Fatal error, connection to server lost

Returns:

NameTypeDescription
_type_

description

clone​

clone() -> Connector

Create a new Connector object with the same parameters as the current one. This is important in multi-threaded applications, where each thread should have its own Connector object. Be cautious when using this method, as it will create a new connection to the database, which will consume resources. Ideally, this method should be called once for each thread.

Returns:

NameTypeDescription
ConnectorConnector

Clone of original Connector

check_status​

check_status(json_res: Any) -> int

Returns the status of the first negative command response from the server, or the status of the first command if all are non-negative. Can traverse a JSON recursively to find the statuses.

Parameters:

NameTypeDescriptionDefault
json_resAny

The actual response from the server.

required

Returns:

NameTypeDescription
intint

The value received from the server, or -2 if not found.