| 1 |
import http |
| 2 |
|
| 3 |
from fastapi import FastAPI, Path, Query |
| 4 |
|
| 5 |
external_docs = { |
| 6 |
"description": "External API documentation.", |
| 7 |
"url": "https://docs.example.com/api-general", |
| 8 |
} |
| 9 |
|
| 10 |
app = FastAPI(openapi_external_docs=external_docs) |
| 11 |
|
| 12 |
|
| 13 |
@app.api_route("/api_route") |
| 14 |
def non_operation(): |
| 15 |
return {"message": "Hello World"} |
| 16 |
|
| 17 |
|
| 18 |
def non_decorated_route(): |
| 19 |
return {"message": "Hello World"} |
| 20 |
|
| 21 |
|
| 22 |
app.add_api_route("/non_decorated_route", non_decorated_route) |
| 23 |
|
| 24 |
|
| 25 |
@app.get("/text") |
| 26 |
def get_text(): |
| 27 |
return "Hello World" |
| 28 |
|
| 29 |
|
| 30 |
@app.get("/path/{item_id}") |
| 31 |
def get_id(item_id): |
| 32 |
return item_id |
| 33 |
|
| 34 |
|
| 35 |
@app.get("/path/str/{item_id}") |
| 36 |
def get_str_id(item_id: str): |
| 37 |
return item_id |
| 38 |
|
| 39 |
|
| 40 |
@app.get("/path/int/{item_id}") |
| 41 |
def get_int_id(item_id: int): |
| 42 |
return item_id |
| 43 |
|
| 44 |
|
| 45 |
@app.get("/path/float/{item_id}") |
| 46 |
def get_float_id(item_id: float): |
| 47 |
return item_id |
| 48 |
|
| 49 |
|
| 50 |
@app.get("/path/bool/{item_id}") |
| 51 |
def get_bool_id(item_id: bool): |
| 52 |
return item_id |
| 53 |
|
| 54 |
|
| 55 |
@app.get("/path/param/{item_id}") |
| 56 |
def get_path_param_id(item_id: str | None = Path()): |
| 57 |
return item_id |
| 58 |
|
| 59 |
|
| 60 |
@app.get("/path/param-minlength/{item_id}") |
| 61 |
def get_path_param_min_length(item_id: str = Path(min_length=3)): |
| 62 |
return item_id |
| 63 |
|
| 64 |
|
| 65 |
@app.get("/path/param-maxlength/{item_id}") |
| 66 |
def get_path_param_max_length(item_id: str = Path(max_length=3)): |
| 67 |
return item_id |
| 68 |
|
| 69 |
|
| 70 |
@app.get("/path/param-min_maxlength/{item_id}") |
| 71 |
def get_path_param_min_max_length(item_id: str = Path(max_length=3, min_length=2)): |
| 72 |
return item_id |
| 73 |
|
| 74 |
|
| 75 |
@app.get("/path/param-gt/{item_id}") |
| 76 |
def get_path_param_gt(item_id: float = Path(gt=3)): |
| 77 |
return item_id |
| 78 |
|
| 79 |
|
| 80 |
@app.get("/path/param-gt0/{item_id}") |
| 81 |
def get_path_param_gt0(item_id: float = Path(gt=0)): |
| 82 |
return item_id |
| 83 |
|
| 84 |
|
| 85 |
@app.get("/path/param-ge/{item_id}") |
| 86 |
def get_path_param_ge(item_id: float = Path(ge=3)): |
| 87 |
return item_id |
| 88 |
|
| 89 |
|
| 90 |
@app.get("/path/param-lt/{item_id}") |
| 91 |
def get_path_param_lt(item_id: float = Path(lt=3)): |
| 92 |
return item_id |
| 93 |
|
| 94 |
|
| 95 |
@app.get("/path/param-lt0/{item_id}") |
| 96 |
def get_path_param_lt0(item_id: float = Path(lt=0)): |
| 97 |
return item_id |
| 98 |
|
| 99 |
|
| 100 |
@app.get("/path/param-le/{item_id}") |
| 101 |
def get_path_param_le(item_id: float = Path(le=3)): |
| 102 |
return item_id |
| 103 |
|
| 104 |
|
| 105 |
@app.get("/path/param-lt-gt/{item_id}") |
| 106 |
def get_path_param_lt_gt(item_id: float = Path(lt=3, gt=1)): |
| 107 |
return item_id |
| 108 |
|
| 109 |
|
| 110 |
@app.get("/path/param-le-ge/{item_id}") |
| 111 |
def get_path_param_le_ge(item_id: float = Path(le=3, ge=1)): |
| 112 |
return item_id |
| 113 |
|
| 114 |
|
| 115 |
@app.get("/path/param-lt-int/{item_id}") |
| 116 |
def get_path_param_lt_int(item_id: int = Path(lt=3)): |
| 117 |
return item_id |
| 118 |
|
| 119 |
|
| 120 |
@app.get("/path/param-gt-int/{item_id}") |
| 121 |
def get_path_param_gt_int(item_id: int = Path(gt=3)): |
| 122 |
return item_id |
| 123 |
|
| 124 |
|
| 125 |
@app.get("/path/param-le-int/{item_id}") |
| 126 |
def get_path_param_le_int(item_id: int = Path(le=3)): |
| 127 |
return item_id |
| 128 |
|
| 129 |
|
| 130 |
@app.get("/path/param-ge-int/{item_id}") |
| 131 |
def get_path_param_ge_int(item_id: int = Path(ge=3)): |
| 132 |
return item_id |
| 133 |
|
| 134 |
|
| 135 |
@app.get("/path/param-lt-gt-int/{item_id}") |
| 136 |
def get_path_param_lt_gt_int(item_id: int = Path(lt=3, gt=1)): |
| 137 |
return item_id |
| 138 |
|
| 139 |
|
| 140 |
@app.get("/path/param-le-ge-int/{item_id}") |
| 141 |
def get_path_param_le_ge_int(item_id: int = Path(le=3, ge=1)): |
| 142 |
return item_id |
| 143 |
|
| 144 |
|
| 145 |
@app.get("/query") |
| 146 |
def get_query(query): |
| 147 |
return f"foo bar {query}" |
| 148 |
|
| 149 |
|
| 150 |
@app.get("/query/optional") |
| 151 |
def get_query_optional(query=None): |
| 152 |
if query is None: |
| 153 |
return "foo bar" |
| 154 |
return f"foo bar {query}" |
| 155 |
|
| 156 |
|
| 157 |
@app.get("/query/int") |
| 158 |
def get_query_type(query: int): |
| 159 |
return f"foo bar {query}" |
| 160 |
|
| 161 |
|
| 162 |
@app.get("/query/int/optional") |
| 163 |
def get_query_type_optional(query: int | None = None): |
| 164 |
if query is None: |
| 165 |
return "foo bar" |
| 166 |
return f"foo bar {query}" |
| 167 |
|
| 168 |
|
| 169 |
@app.get("/query/int/default") |
| 170 |
def get_query_type_int_default(query: int = 10): |
| 171 |
return f"foo bar {query}" |
| 172 |
|
| 173 |
|
| 174 |
@app.get("/query/param") |
| 175 |
def get_query_param(query=Query(default=None)): |
| 176 |
if query is None: |
| 177 |
return "foo bar" |
| 178 |
return f"foo bar {query}" |
| 179 |
|
| 180 |
|
| 181 |
@app.get("/query/param-required") |
| 182 |
def get_query_param_required(query=Query()): |
| 183 |
return f"foo bar {query}" |
| 184 |
|
| 185 |
|
| 186 |
@app.get("/query/param-required/int") |
| 187 |
def get_query_param_required_type(query: int = Query()): |
| 188 |
return f"foo bar {query}" |
| 189 |
|
| 190 |
|
| 191 |
@app.get("/enum-status-code", status_code=http.HTTPStatus.CREATED) |
| 192 |
def get_enum_status_code(): |
| 193 |
return "foo bar" |
| 194 |
|
| 195 |
|
| 196 |
@app.get("/query/frozenset") |
| 197 |
def get_query_type_frozenset(query: frozenset[int] = Query(...)): |
| 198 |
return ",".join(map(str, sorted(query))) |
| 199 |
|
| 200 |
|
| 201 |
@app.get("/query/list") |
| 202 |
def get_query_list(device_ids: list[int] = Query()) -> list[int]: |
| 203 |
return device_ids |
| 204 |
|
| 205 |
|
| 206 |
@app.get("/query/list-default") |
| 207 |
def get_query_list_default(device_ids: list[int] = Query(default=[])) -> list[int]: |
| 208 |
return device_ids |