File: api.py

package info (click to toggle)
python-hug 2.6.0-2.4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,072 kB
  • sloc: python: 8,938; sh: 99; makefile: 17
file content (52 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download
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
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 :)"