Python SDK (v.0.0.1)
Space and Time Python SDK (Python version >= 3.6)
Installation Instructions
git clone https://github.com/spaceandtimelabs/SxT-Python-SDK.git
- Before running the code, rename the
.env.sample
to.env
and ensure that your credentials are set up in the.env
file properly. - Install SDK dependencies:
pip install -r requirements.txt
Important Notes
- By default, the SDK will create a new user for you. However, If you provide a
PUBLICKEY
andPRIVATEKEY
in.env
, then the SDK will assume you have an existing account, and authenticate with that. - If you change between user accounts, you must also change the keys in your .env file.
- The code in
main.py
demonstrates how to call the SDK. - An installable PIP package is coming soon.
Features
Sessions
The SDK implements persistent storage in file-based sessions.
Encryption
It supports ED25519 public key encryption for biscuit authorization and securing data in the platform.
SQL Support
๐ช Support for DDL: Configure Resources - Perform all CREATE, ALTER, DROP commands
๐งช Support for DML: Modify data - Perform all INSERT, UPDATE, MERGE, DELETE commands
๐ฅ Support for SQL: Execute queries - Perform all SELECT commands
๐ Support for SQL Views: Execute a view
Platform Discovery
For fetching metadata and information about the database resources.
- Namespaces
- Tables
- Table Columns
- Table Indexes
- Table Primary Keys
- Table Relationships
- Table Primary Key References
- Table Foreign Key References
Examples
Initializing the SDK
# Initializing the Space and Time SDK for use.
from spaceandtimesdk import SpaceAndTimeSDK
SpaceAndTimeInit = SpaceAndTimeSDK()
Authenticating with the Space and Time Platform
When the SDK creates registers a new account, it will save your keys to the .env file. Make sure to save your private key used in authentication and biscuit generation, or else you will not be able to have access to the user and the tables created using the key.
๐ก SxT-Python-SDK will save newly created keys to your .env for you!
The generated AccessToken
is valid for 25 minutes, and the RefreshToken
for 30 minutes.
# Authenticate yourself using the Space and Time SDK.
authenticate_token_data = SpaceAndTimeInit.authenticate_user()
authenticate_token_response = authenticate_token_data["response"]
authenticate_token_error = authenticate_token_data["error"]
print("Response: ", authenticate_token_response)
print("Error: ", authenticate_token_error)
Generating Biscuits
Space and Time relies on biscuits to support decentralized authorization. Queries against public_read
tables in Space and Time (like our blockchain data) don't require biscuits. However, many user-created tables will establish authorization with biscuits. You can read more about how Space and Time uses biscuits in the biscuit authorization documentation.
Our Python SDK does not currently support generating biscuits natively. However, they can be generated using the SxT CLI or via several different open-source libraries at biscuitsec.org.
DDL, DML, and DQL
Note: To create a new schema,
ddl_create
permission on your biscuit is needed.
# Create a Schema
create_schema_data = SpaceAndTimeInit.CreateSchema("CREATE SCHEMA ETH")
create_schema_response = create_schema_data["response"]
create_schema_error = create_schema_data["error"]
print("Response: ", create_schema_response)
print("Error: ", create_schema_error)
# Only for Create Table Queries
# for ALTER and DROP, use DDL()
ddl_create_table_data = SpaceAndTimeInit.DDLCreateTable("ETH.TESTTABLE", "CREATE TABLE ETH.TESTTABLE (id INT PRIMARY KEY, test VARCHAR)", "permissioned", publicKey, biscuit)
ddl_create_table_response = ddl_create_table_data["response"]
ddl_create_table_error = ddl_create_table_data["error"]
print("Response: ", ddl_create_table_response)
print("Error: ", ddl_create_table_error)
# For ALTER and DROP
ddl_data = SpaceAndTimeInit.DDL("ETH.TESTTABLE", "ALTER TABLE ETH.TESTTABLE ADD TEST2 VARCHAR", biscuit)
ddl_data_response = ddl_data["response"]
ddl_data_error = ddl_data["error"]
print("Response: ", ddl_data_response)
print("Error: ", ddl_data_error)
# DML
# Use DML() to insert, update, delete and merge queries
dml_data = SpaceAndTimeInit.DML("ETH.TESTTABLE", "INSERT INTO ETH.TESTTABLE VALUES(5,'x5')", biscuit)
dml_data_response = dml_data["response"]
dml_data_error = dml_data["error"]
print("Response: ", dml_data_response)
print("Error: ", dml_data_error)
# DQL for selecting content from the blockchain tables.
dql_data = SpaceAndTimeInit.DQL("ETH.TESTTABLE", "SELECT * FROM ETH.TESTTABLE", biscuit)
dql_data_response = dql_data["response"]
dql_data_error = dql_data["error"]
print("Response: ", dql_data_response)
print("Error: ", dql_data_error)
DISCOVERY
Discovery SDK calls need a user to be logged in.
# List Namespaces
namespace_data = SpaceAndTimeInit.get_namespaces()
namespace_response = namespace_data["response"]
namespace_error = namespace_data["error"]
print("Response: ", namespace_response)
print("Error: ", namespace_error)
# List Tables in a given namespace
# Possible scope values - ALL = all tables, PUBLIC = non-permissioned tables, PRIVATE = tables created by a requesting user
get_tables_data = SpaceAndTimeInit.get_tables("ALL","ETH")
get_tables_response = get_tables_data["response"]
get_tables_error = get_tables_data["error"]
print("Response: ", get_tables_response)
print("Error: ", get_tables_error)
# List columns for a given table in a namespace
get_table_column_data = SpaceAndTimeInit.get_table_columns("TESTTABLE", "ETH")
get_table_column_response = get_table_column_data["response"]
get_table_column_error = get_table_column_data["error"]
print("Response: ", get_table_column_response)
print("Error: ", get_table_column_error)
# List table index for a given table in a namespace
get_table_indexes_data = SpaceAndTimeInit.get_table_indexes("TESTTABLE", "ETH")
get_table_indexes_response = get_table_indexes_data["response"]
get_table_indexes_error = get_table_indexes_data["error"]
print("Response: ", get_table_indexes_response)
print("Error: ", get_table_indexes_error)
# List table primary key for a given table in a namespace
get_table_primary_keys_data = SpaceAndTimeInit.get_table_primary_keys("TESTTABLE", "ETH")
get_table_primary_keys_response = get_table_primary_keys_data["response"]
get_table_primary_keys_error = get_table_primary_keys_data["error"]
print("Response: ", get_table_primary_keys_response)
print("Error: ", get_table_primary_keys_error)
# List table relations for a namespace and scope
get_table_relationship_data = SpaceAndTimeInit.get_table_relationships("PRIVATE", "ETH")
get_table_relationship_response = get_table_relationship_data["response"]
get_table_relationship_error = get_table_relationship_data["error"]
print("Response: ", get_table_relationship_response)
print("Error: ", get_table_relationship_error)
# List table primary key references for a table, column and a namespace
primary_key_reference_data = SpaceAndTimeInit.get_primary_key_references("TESTTABLE", "TEST", "ETH")
primary_key_reference_response =
primary_key_reference_data["response"]
primary_key_reference_error = primary_key_reference_data["error"]
print("Response: ", primary_key_reference_response)
print("Error: ", primary_key_reference_error)
# List table foreign key references for a table, column and a namespace
foreign_key_reference_data = SpaceAndTimeInit.get_foreign_key_references("TESTTABLE", "TEST", "ETH")
foreign_key_reference_response = foreign_key_reference_data["response"]
foreign_key_reference_error = foreign_key_reference_data["error"]
print("Response: ", foreign_key_reference_response)
print("Error: ", foreign_key_reference_error)
Storage
For File Storage, the following methods are available
# File Storage Methods
SpaceAndTimeInit.write_to_file(AccessToken, RefreshToken, AccessTokenExpires, RefreshTokenExpires)
SpaceAndTimeInit.read_file_contents()
Existing User Authentication Example
# Make sure you set two new variables in .env: PUBLICKEY, PRIVATEKEY
from spaceandtimesdk import SpaceAndTimeSDK
from dotenv import load_dotenv
load_dotenv()
# Initializing the Space and Time SDK for use.
SpaceAndTimeInit = SpaceAndTimeSDK()
print(SpaceAndTimeInit.authenticate_user())
Updated 4 months ago