-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathplayground.py
391 lines (298 loc) · 8.57 KB
/
playground.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from time import sleep
from random import randrange
from sys import maxsize
from appwrite.client import Client
from appwrite.services.users import Users
from appwrite.services.databases import Databases
from appwrite.services.storage import Storage
from appwrite.services.account import Account
from appwrite.services.functions import Functions
from appwrite.input_file import InputFile
from appwrite.permission import Permission
from appwrite.role import Role
from appwrite.id import ID
# Helper method to print green colored output.
def p(info):
print("\033[32;1m" + str(info) + "\033[0m")
# Read the docs at https://appwrite.io/docs to get more information
# about API keys and Project IDs
client = Client()
client.set_endpoint("http://YOUR_HOST/v1")
client.set_project("YOUR_PROJECT_ID")
client.set_key("YOU_API_KEY")
client.set_self_signed()
# client.set_jwt('JWT') # Use this to authenticate with JWT instead of API_KEY
databases = Databases(client)
storage = Storage(client)
functions = Functions(client)
users = Users(client)
database_id = None
collection_id = None
document_id = None
user_id = None
bucket_id = None
file_id = None
document_id = None
function_id = None
deployment_id = None
execution_id = None
def create_database():
global database_id
p("Running Create Database API")
response = databases.create(
database_id=ID.unique(),
name="Movies",
)
database_id = response["$id"]
print(response)
def create_collection():
global collection_id
p("Running Create Collection API")
response = databases.create_collection(
database_id,
collection_id=ID.unique(),
name="Movies",
document_security=True,
permissions=[
Permission.read(Role.any()),
Permission.create(Role.users()),
Permission.update(Role.users()),
Permission.delete(Role.users()),
],
)
collection_id = response["$id"]
print(response)
response = databases.create_string_attribute(
database_id,
collection_id,
key="name",
size=255,
required=True,
)
print(response)
response = databases.create_integer_attribute(
database_id, collection_id, key="release_year", required=True, min=0, max=9999
)
print(response)
response = databases.create_float_attribute(
database_id, collection_id, key="rating", required=True, min=0.0, max=99.99
)
print(response)
response = databases.create_boolean_attribute(
database_id, collection_id, key="kids", required=True
)
print(response)
response = databases.create_email_attribute(
database_id, collection_id, key="email", required=False
)
print(response)
# Wait for attributes to be created
sleep(2)
response = databases.create_index(
database_id,
collection_id,
key="name_email_idx",
type="fulltext",
attributes=["name", "email"],
)
print(response)
def list_collections():
p("Running List Collection API")
response = databases.list_collections(database_id)
print(response)
def get_account():
account = Account(client)
p("Running Get Account API")
response = account.get()
print(response)
def add_doc():
global document_id
p("Running Add Document API")
response = databases.create_document(
database_id,
collection_id,
document_id=ID.unique(),
data={
"name": "Spider Man",
"release_year": 1920,
"rating": 98.5,
"kids": False,
},
permissions=[
Permission.read(Role.users()),
Permission.update(Role.users()),
Permission.delete(Role.users()),
],
)
document_id = response["$id"]
print(response)
def list_doc():
p("Running List Document API")
response = databases.list_documents(database_id, collection_id)
print(response)
def delete_doc():
p("Running Delete Database API")
response = databases.delete_document(database_id, collection_id, document_id)
print(response)
def delete_collection():
p("Running Delete Collection API")
response = databases.delete_collection(database_id, collection_id)
print(response)
def delete_database():
p("Running Delete Database API")
response = databases.delete(database_id)
print(response)
def create_bucket():
global bucket_id
p("Running Create Bucket API")
response = storage.create_bucket(
bucket_id=ID.unique(),
name="awesome bucket",
file_security=True,
permissions=[
Permission.read(Role.any()),
Permission.create(Role.users()),
Permission.update(Role.users()),
Permission.delete(Role.users()),
],
)
bucket_id = response["$id"]
print(response)
def list_buckets():
p("Running List Buckets API")
response = storage.list_buckets()
print(response)
def upload_file():
global file_id
p("Running Upload File API")
response = storage.create_file(
bucket_id,
file_id=ID.unique(),
file=InputFile.from_path("./resources/nature.jpg"),
)
file_id = response["$id"]
print(response)
def list_files():
p("Running List Files API")
response = storage.list_files(bucket_id)
print(response)
def delete_file():
p("Running Delete File API")
response = storage.delete_file(bucket_id, file_id)
print(response)
def delete_bucket():
p("Running Delete Bucket API")
response = storage.delete_bucket(bucket_id)
print(response)
def create_user():
global user_id
name = str(randrange(1, maxsize))
p("Running Create User API")
response = users.create(
user_id=ID.unique(), email=f"{name}@test.com", password=f"{name}@123", name=name
)
user_id = response["$id"]
print(response)
def list_user():
p("Running List User API")
response = users.list()
print(response)
def delete_user():
p("Running Delete User API")
response = users.delete(user_id)
print(response)
def create_function():
global function_id
p("Running Create Function API")
response = functions.create(
function_id=ID.unique(),
name="Test Function",
execute=[Role.any()],
runtime="python-3.9",
)
function_id = response["$id"]
print(response)
def list_function():
p("Running List Function API")
response = functions.list()
print(response)
def delete_function():
p("Running Delete Function API")
response = functions.delete(function_id)
print(response)
def create_deployment():
global function_id
global deployment_id
p("Running Create Deployment API")
response = functions.create_deployment(
function_id,
code=InputFile.from_path("./resources/code.tar.gz"),
activate=True,
entrypoint="main.py",
)
deployment_id = response["$id"]
print(response)
def list_deployments():
global function_id
p("Running List Deployments API")
response = functions.list_deployments(function_id)
print(response)
def delete_deployment():
global function_id
global deployment_id
p("Running Delete Deployment API")
response = functions.delete_deployment(function_id, deployment_id)
print(response)
def create_execution():
global function_id
global execution_id
p("Running Create Execution API")
response = functions.create_execution(function_id)
execution_id = response["$id"]
print(response)
def list_executions():
global function_id
p("Running List Executions API")
response = functions.list_executions(function_id)
print(response)
def delete_execution():
global function_id
global execution_id
p("Running Delete Execution API")
response = functions.delete_execution(function_id, execution_id)
print(response)
def run_all_tasks():
# Databases
create_database()
create_collection()
list_collections()
add_doc()
list_doc()
delete_doc()
delete_collection()
delete_database()
# Storage
create_bucket()
list_buckets()
upload_file()
list_files()
delete_file()
delete_bucket()
# Users
# get_account() # Use this only with JWT
create_user()
list_user()
delete_user()
# Functions
create_function()
list_function()
create_deployment()
list_deployments()
create_execution()
list_executions()
delete_execution()
delete_deployment()
delete_function()
if __name__ == "__main__":
run_all_tasks()
p("Successfully ran playground!")