Skip to content

Commit f53e6a7

Browse files
committed
add docs for responses
1 parent 715f94f commit f53e6a7

4 files changed

Lines changed: 67 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added tests for headers
1111
- Updated repr for `Route`
1212
- Patched responses with three values
13+
- Documented responses and result protocol
1314

1415
## [1.0.0-alpha2] - 2023-09-9
1516

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
1-
# Components
1+
# Responses
22

3-
## Using built-in components
3+
## Basics
4+
5+
view.py allows you to return three things from a route: the body, the headers, and the status code.
6+
7+
You can simply return these via a tuple:
8+
9+
```py
10+
@app.get("/")
11+
async def index():
12+
return "you are not worthy", 400, {"x-worthiness": "0"}
13+
```
14+
15+
In fact, it can be in any order you want:
16+
17+
```py
18+
@app.get("/")
19+
async def index():
20+
return {"x-worthiness": "0"}, "you are not worthy", 400
21+
```
22+
23+
### Result Protocol
24+
25+
If you need to return a more complicated object, you can put a `__view_result__` function on it:
26+
27+
```py
28+
class MyObject:
29+
def __view_result__(self) -> str:
30+
return "123"
31+
32+
@app.get("/")
33+
async def index():
34+
return MyObject()
35+
```
36+
37+
## Components
38+
39+
### Using built-in components
440

541
You can import any standard HTML components from the `view.components` module:
642

@@ -36,7 +72,7 @@ The above would translate to the following HTML snippet:
3672
</html>
3773
```
3874

39-
### Children
75+
#### Children
4076

4177
You can pass an infinite number of children to a component, and it will be translated to the proper HTML:
4278

@@ -54,7 +90,7 @@ Would translate to:
5490
</div>
5591
```
5692

57-
## Attributes
93+
### Attributes
5894

5995
All built in components come with their respected attributes, per the HTML specification:
6096

@@ -64,15 +100,15 @@ async def index():
64100
return html(lang="en")
65101
```
66102

67-
### Classes
103+
#### Classes
68104

69105
Since the `class` keyword is reserved in Python, view.py uses the parameter name `cls` instead:
70106

71107
```py
72108
div(cls="hello")
73109
```
74110

75-
## Custom Components
111+
### Custom Components
76112

77113
There's no need for any fancy mechanics when making a custom component, so you can just use a normal function:
78114

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ nav:
77
- Home: index.md
88
- Creating an app: creating.md
99
- Running: running.md
10-
- Components: components.md
10+
- Responses: responses.md
1111
- Parameters: parameters.md
1212

1313
theme:

tests/test_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,26 @@ async def index():
5656
assert res.status == 201
5757
assert res.message == "123"
5858
assert res.headers["a"] == "b"
59+
60+
61+
@test("result protocol")
62+
async def _():
63+
app = new_app()
64+
65+
class MyObject:
66+
def __view_result__(self) -> str:
67+
return "hello"
68+
69+
@app.get("/")
70+
async def index():
71+
return MyObject()
72+
73+
@app.get("/multi")
74+
async def multi():
75+
return 201, MyObject()
76+
77+
async with app.test() as test:
78+
assert (await test.get("/")).message == "hello"
79+
res = await test.get("/multi")
80+
assert res.message == "hello"
81+
assert res.status == 201

0 commit comments

Comments
 (0)