52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
import requests
|
|
import time
|
|
|
|
ENDPOINT = 'https://nyc.cloud.appwrite.io/v1'
|
|
PROJECT_ID = '68be03580005c05fb11f'
|
|
API_KEY = 'standard_08a3843fed4838afe384fce76c6ab5889478347e04c1f55f253f4adb195831f9f57e8b2784448a95b72139e2b97fad27304935bd420d1ea5c8d9cdcd2afb9c23f1989269ec2a90c8f87c961ebddd73cb19df261bd5268d01382c62fd76135e6b34daf6ebab8910de85fd43a6534b86ecec14bb8c6da8bcfa62233e17b79ad35e'
|
|
|
|
headers = {
|
|
'X-Appwrite-Project': PROJECT_ID,
|
|
'X-Appwrite-Key': API_KEY,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
def setup_appwrite():
|
|
# 1. Criar Database
|
|
print('Criando Database chat_db...')
|
|
db_resp = requests.post(f'{ENDPOINT}/databases', headers=headers, json={
|
|
'databaseId': 'chat_db',
|
|
'name': 'Chat Database'
|
|
})
|
|
print(f'Database Result: {db_resp.status_code}')
|
|
|
|
# 2. Criar Coleção
|
|
print('Criando Coleção messages...')
|
|
coll_resp = requests.post(f'{ENDPOINT}/databases/chat_db/collections', headers=headers, json={
|
|
'collectionId': 'messages',
|
|
'name': 'Messages',
|
|
'permissions': ['read("users")', 'create("users")', 'update("users")']
|
|
})
|
|
print(f'Coleção Result: {coll_resp.status_code}')
|
|
|
|
# 3. Criar Atributos
|
|
attrs = [
|
|
{'key': 'conversation_id', 'size': 255, 'required': True},
|
|
{'key': 'sender_id', 'size': 255, 'required': True},
|
|
{'key': 'content', 'size': 65535, 'required': True},
|
|
{'key': 'timestamp', 'size': 255, 'required': True}
|
|
]
|
|
|
|
for attr in attrs:
|
|
print(f"Criando atributo {attr['key']}...")
|
|
requests.post(
|
|
f'{ENDPOINT}/databases/chat_db/collections/messages/attributes/string',
|
|
headers=headers,
|
|
json=attr
|
|
)
|
|
time.sleep(2)
|
|
|
|
print('Configuração do Appwrite finalizada!')
|
|
|
|
if __name__ == "__main__":
|
|
setup_appwrite()
|