@@ -7,6 +7,7 @@ Object-oriented rest framework based on starlette, marshmallow and apispec.
77* [ Starlette] 0.12.0+
88* [ Marshmallow] 3.0.0rc8+
99* [ APISpec] 2.0.2+
10+ * [ python-multipart] 0.0.5+
1011
1112## Installation
1213
@@ -17,32 +18,49 @@ $ pip install star_resty
1718## Example
1819
1920``` python
20- from marshmallow import Schema, fields, ValidationError
21+ from marshmallow import Schema, fields, ValidationError, post_load
2122from starlette.applications import Starlette
2223from starlette.datastructures import UploadFile
2324from starlette.responses import JSONResponse
2425
25- from star_resty import Method, Operation, endpoint, json_payload, form_payload, query, setup_spec
26+ from dataclasses import dataclass
2627
28+ from star_resty import Method, Operation, endpoint, json_schema, json_payload, form_payload, query, setup_spec
29+ from typing import Optional
2730
2831class EchoInput (Schema ):
2932 a = fields.Int()
3033
3134
32- # Json Payload
35+ # Json Payload (by schema)
3336class JsonPayloadSchema (Schema ):
3437 a = fields.Int(required = True )
3538 s = fields.String()
3639
3740
41+ # Json Payload (by dataclass)
42+ @dataclass
43+ class Payload :
44+ a: int
45+ s: Optional[str ] = None
46+
47+ class JsonPayloadDataclass (Schema ):
48+ a= fields.Int(required = True )
49+ s= fields.Str()
50+
51+ @post_load
52+ def create_payload (self , data , ** kwargs ):
53+ return Payload(** data)
54+
55+
3856# Form Payload
3957class FormFile (fields .Field ):
4058 def _validate (self , value ):
4159 if not isinstance (value, UploadFile):
4260 raise ValidationError(' Not a file' )
4361
4462
45- class FormSchema (Schema ):
63+ class FormPayload (Schema ):
4664 id = fields.Int(required = True )
4765 file = FormFile()
4866
@@ -65,20 +83,29 @@ class Echo(Method):
6583 return query_params
6684
6785
68- @app.route (' /post' , methods = [' POST' ])
86+ @app.route (' /post/schema ' , methods = [' POST' ])
6987@endpoint
70- class Post (Method ):
71- meta = Operation(tag = ' default' , description = ' post json' )
88+ class PostSchema (Method ):
89+ meta = Operation(tag = ' default' , description = ' post json (by schema) ' )
7290
7391 async def execute (self , item : json_payload(JsonPayloadSchema)):
7492 return {' a' : item.get(' a' ) * 2 , ' s' : item.get(' s' )}
7593
94+
95+ @app.route (' /post/dataclass' , methods = [' POST' ])
96+ @endpoint
97+ class PostDataclass (Method ):
98+ meta = Operation(tag = ' default' , description = ' post json (by dataclass)' )
99+
100+ async def execute (self , item : json_schema(JsonPayloadDataclass, Payload)):
101+ return {' a' : item.a * 3 , ' s' : item.s}
102+
76103@app.route (' /form' , methods = [' POST' ])
77104@endpoint
78105class PostForm (Method ):
79106 meta = Operation(tag = ' default' , description = ' post form' )
80107
81- async def execute (self , form_data : form_payload(FormSchema )):
108+ async def execute (self , form_data : form_payload(FormPayload )):
82109 file_name = form_data.get(' file' ).filename
83110 id = form_data.get(' id' )
84111 return {' message' : f " file { file_name} with id { id } received " }
0 commit comments