fastapi

Форк
0
/
main.py 
194 строки · 4.1 Кб
1
import http
2
from typing import FrozenSet, Optional
3

4
from fastapi import FastAPI, Path, Query
5

6
app = FastAPI()
7

8

9
@app.api_route("/api_route")
10
def non_operation():
11
    return {"message": "Hello World"}
12

13

14
def non_decorated_route():
15
    return {"message": "Hello World"}
16

17

18
app.add_api_route("/non_decorated_route", non_decorated_route)
19

20

21
@app.get("/text")
22
def get_text():
23
    return "Hello World"
24

25

26
@app.get("/path/{item_id}")
27
def get_id(item_id):
28
    return item_id
29

30

31
@app.get("/path/str/{item_id}")
32
def get_str_id(item_id: str):
33
    return item_id
34

35

36
@app.get("/path/int/{item_id}")
37
def get_int_id(item_id: int):
38
    return item_id
39

40

41
@app.get("/path/float/{item_id}")
42
def get_float_id(item_id: float):
43
    return item_id
44

45

46
@app.get("/path/bool/{item_id}")
47
def get_bool_id(item_id: bool):
48
    return item_id
49

50

51
@app.get("/path/param/{item_id}")
52
def get_path_param_id(item_id: Optional[str] = Path()):
53
    return item_id
54

55

56
@app.get("/path/param-minlength/{item_id}")
57
def get_path_param_min_length(item_id: str = Path(min_length=3)):
58
    return item_id
59

60

61
@app.get("/path/param-maxlength/{item_id}")
62
def get_path_param_max_length(item_id: str = Path(max_length=3)):
63
    return item_id
64

65

66
@app.get("/path/param-min_maxlength/{item_id}")
67
def get_path_param_min_max_length(item_id: str = Path(max_length=3, min_length=2)):
68
    return item_id
69

70

71
@app.get("/path/param-gt/{item_id}")
72
def get_path_param_gt(item_id: float = Path(gt=3)):
73
    return item_id
74

75

76
@app.get("/path/param-gt0/{item_id}")
77
def get_path_param_gt0(item_id: float = Path(gt=0)):
78
    return item_id
79

80

81
@app.get("/path/param-ge/{item_id}")
82
def get_path_param_ge(item_id: float = Path(ge=3)):
83
    return item_id
84

85

86
@app.get("/path/param-lt/{item_id}")
87
def get_path_param_lt(item_id: float = Path(lt=3)):
88
    return item_id
89

90

91
@app.get("/path/param-lt0/{item_id}")
92
def get_path_param_lt0(item_id: float = Path(lt=0)):
93
    return item_id
94

95

96
@app.get("/path/param-le/{item_id}")
97
def get_path_param_le(item_id: float = Path(le=3)):
98
    return item_id
99

100

101
@app.get("/path/param-lt-gt/{item_id}")
102
def get_path_param_lt_gt(item_id: float = Path(lt=3, gt=1)):
103
    return item_id
104

105

106
@app.get("/path/param-le-ge/{item_id}")
107
def get_path_param_le_ge(item_id: float = Path(le=3, ge=1)):
108
    return item_id
109

110

111
@app.get("/path/param-lt-int/{item_id}")
112
def get_path_param_lt_int(item_id: int = Path(lt=3)):
113
    return item_id
114

115

116
@app.get("/path/param-gt-int/{item_id}")
117
def get_path_param_gt_int(item_id: int = Path(gt=3)):
118
    return item_id
119

120

121
@app.get("/path/param-le-int/{item_id}")
122
def get_path_param_le_int(item_id: int = Path(le=3)):
123
    return item_id
124

125

126
@app.get("/path/param-ge-int/{item_id}")
127
def get_path_param_ge_int(item_id: int = Path(ge=3)):
128
    return item_id
129

130

131
@app.get("/path/param-lt-gt-int/{item_id}")
132
def get_path_param_lt_gt_int(item_id: int = Path(lt=3, gt=1)):
133
    return item_id
134

135

136
@app.get("/path/param-le-ge-int/{item_id}")
137
def get_path_param_le_ge_int(item_id: int = Path(le=3, ge=1)):
138
    return item_id
139

140

141
@app.get("/query")
142
def get_query(query):
143
    return f"foo bar {query}"
144

145

146
@app.get("/query/optional")
147
def get_query_optional(query=None):
148
    if query is None:
149
        return "foo bar"
150
    return f"foo bar {query}"
151

152

153
@app.get("/query/int")
154
def get_query_type(query: int):
155
    return f"foo bar {query}"
156

157

158
@app.get("/query/int/optional")
159
def get_query_type_optional(query: Optional[int] = None):
160
    if query is None:
161
        return "foo bar"
162
    return f"foo bar {query}"
163

164

165
@app.get("/query/int/default")
166
def get_query_type_int_default(query: int = 10):
167
    return f"foo bar {query}"
168

169

170
@app.get("/query/param")
171
def get_query_param(query=Query(default=None)):
172
    if query is None:
173
        return "foo bar"
174
    return f"foo bar {query}"
175

176

177
@app.get("/query/param-required")
178
def get_query_param_required(query=Query()):
179
    return f"foo bar {query}"
180

181

182
@app.get("/query/param-required/int")
183
def get_query_param_required_type(query: int = Query()):
184
    return f"foo bar {query}"
185

186

187
@app.get("/enum-status-code", status_code=http.HTTPStatus.CREATED)
188
def get_enum_status_code():
189
    return "foo bar"
190

191

192
@app.get("/query/frozenset")
193
def get_query_type_frozenset(query: FrozenSet[int] = Query(...)):
194
    return ",".join(map(str, sorted(query)))
195

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.