Skip to content

Commit

Permalink
fix: demo to use try/catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
John2360 committed Aug 21, 2024
1 parent d2532be commit 5d30578
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions examples/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,46 @@
)

# Create a database
response = fortress.create_database(alias="Client 1")
if response.success:
print("Database created")
else:
print(f"Database creation failed: {response.message}")
database_id = None
try:
database_id = fortress.create_database(alias="Client 1")
except Exception as e:
print(f"Database creation failed: {e}")

# Create a tenant (if you do not provide a database_id, a new database will be created)
response = fortress.create_tenant(
tenant_name="client1",
alias="Client 1",
database_id=response.id,
)
if response.success:
print("Tenant created")
else:
print(f"Tenant creation failed: {response.message}")
try:
fortress.create_tenant(
tenant_name="client1",
alias="Client 1",
database_id=database_id,
)
except Exception as e:
print(f"Tenant creation failed: {e}")

# List all databases
response = fortress.list_databases()
if response.success:
for database in response.databases:
try:
databases = fortress.list_databases()
for database in databases:
print(database.id, database.alias, database.date_created)
except Exception as e:
print(f"Database listing failed: {e}")

# List all tenants
response = fortress.list_tenants()
if response.success:
for tenant in response.tenants:
try:
tenants = fortress.list_tenants()
for tenant in tenants:
print(tenant.name, tenant.alias, tenant.database_id, tenant.date_created)
except Exception as e:
print(f"Tenant listing failed: {e}")

# Connect to the tenant's database
conn = fortress.connect_tenant("client1")
conn = None
try:
conn = fortress.connect_tenant("client1")
except Exception as e:
print(f"Database connection failed: {e}")


cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, name TEXT)")
cursor.execute("INSERT INTO test (name) VALUES ('Alice')")
Expand Down

0 comments on commit 5d30578

Please sign in to comment.