-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (41 loc) · 1.42 KB
/
app.py
File metadata and controls
55 lines (41 loc) · 1.42 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
# coding: utf-8
"""Demo FastAPI app for illustrating GitHub Actions workflow.
"""
import random
from enum import Enum
from fastapi import FastAPI, Query
from pydantic import BaseModel
api = FastAPI("github actions demo") # pylint: disable=invalid-name
class GreetingStyle(str, Enum):
"""Greeting styles used by `get_greeting`.
"""
Affable = "affable"
Unpleasant = "unpleasant"
Aggressive = "aggressive"
Diffident = "diffident"
_STYLE_NAMES = tuple(entry.value for entry in GreetingStyle)
_GREETING_FOR_STYLE = {
GreetingStyle.Affable: "Well hey there, {name}!",
GreetingStyle.Unpleasant: "Oh. It's you again, {name}.",
GreetingStyle.Diffident: "Oh! Hi, uh... hi {name}...",
GreetingStyle.Aggressive: "Well? What do you want, {name}?",
}
class GreetingResponseModel(BaseModel): # pylint: disable=too-few-public-methods
"""Response model for `get_greeting`.
"""
greeting: str
name: str
style: GreetingStyle
@api.get("/api/v1/greet", response_model=GreetingResponseModel)
def get_greeting(name: str = Query(..., description="Name to greet")):
"""Greet a user by name, with a randomly-chosen greeting style.
"""
if name.lower() == "joe":
style = GreetingStyle.Aggressive
else:
style = random.choice(list(GreetingStyle))
return {
"greeting": _GREETING_FOR_STYLE[style].format(name=name),
"style": style,
"name": name,
}