aboutsummaryrefslogtreecommitdiff
path: root/test_api/test_api.py
blob: 02eb491a739483904a24092ae15e918f5c406e50 (plain)
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
from bottle import get, post, run, HTTPError, request

@get("/")
def get_root():
    return {"status": "OK"}

@get("/long")
def get_long():
    return {"data": [
        {"name": "apple", "calories": 5},
        {"name": "orange", "calories": 120},
        {"name": "pear", "calories": 45},
    ]}

@post("/")
def post_root():
    return {"status": "SUCCESS"}

@get("/errors/404")
def error_404():
    raise HTTPError(404, body={"error": "no exist"})

@get("/errors/500")
def error_500():
    raise HTTPError(500, body={"error": "server error"})

@post("/auth")
def post_auth():
    return {"token": "blah1234"}

@get("/profile")
def get_profile():
    if request.headers.get("TOKEN") != "blah1234":
        raise HTTPError(401, body={"error": "Not Authorized"})
    return {
        "user": "John Doe"
    }

run(host="localhost", port=8080)