forked from hugapi/hug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
64 lines (51 loc) · 1.43 KB
/
api.py
File metadata and controls
64 lines (51 loc) · 1.43 KB
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
import hug
from demo.authentication import basic_authentication
from demo.directives import SqlalchemySession
from demo.models import TestUser, TestModel
from demo.validation import CreateUserSchema, DumpSchema, unique_username
@hug.post('/create_user2', requires=basic_authentication)
def create_user2(
db: SqlalchemySession,
data: CreateUserSchema()
):
user = TestUser(
**data
)
db.add(user)
db.flush()
return dict()
@hug.post('/create_user', requires=basic_authentication)
def create_user(
db: SqlalchemySession,
username: unique_username,
password: hug.types.text
):
user = TestUser(
username=username,
password=password
)
db.add(user)
db.flush()
return dict()
@hug.get('/test')
def test():
return ''
@hug.get('/hello')
def make_simple_query(db: SqlalchemySession):
for word in ["hello", "world", ":)"]:
test_model = TestModel()
test_model.name = word
db.add(test_model)
db.flush()
return " ".join([obj.name for obj in db.query(TestModel).all()])
@hug.get('/hello2')
def transform_example(db: SqlalchemySession) -> DumpSchema():
for word in ["hello", "world", ":)"]:
test_model = TestModel()
test_model.name = word
db.add(test_model)
db.flush()
return dict(users=db.query(TestModel).all())
@hug.get('/protected', requires=basic_authentication)
def protected():
return 'smile :)'