@@ -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,35 +18,53 @@ $ pip install star_resty
1718## Example
1819
1920``` python
20- from dataclasses import dataclass
21- from typing import Optional
22-
23- from marshmallow import Schema, fields, post_load, ValidationError
21+ from marshmallow import Schema, fields, ValidationError, post_load
2422from starlette.applications import Starlette
23+ from starlette.datastructures import UploadFile
2524from starlette.responses import JSONResponse
2625
27- from star_resty import Method, Operation, endpoint, json_schema, query, setup_spec
26+ from dataclasses import dataclass
2827
28+ from star_resty import Method, Operation, endpoint, json_schema, json_payload, form_payload, query, setup_spec
29+ from typing import Optional
2930
3031class EchoInput (Schema ):
3132 a = fields.Int()
3233
3334
35+ # Json Payload (by schema)
36+ class JsonPayloadSchema (Schema ):
37+ a = fields.Int(required = True )
38+ s = fields.String()
39+
40+
41+ # Json Payload (by dataclass)
3442@dataclass
3543class Payload :
3644 a: int
3745 s: Optional[str ] = None
3846
39-
40- class PayloadSchema (Schema ):
41- a = fields.Int(required = True )
42- s = fields.String()
47+ class JsonPayloadDataclass (Schema ):
48+ a= fields.Int(required = True )
49+ s= fields.Str()
4350
4451 @post_load
4552 def create_payload (self , data , ** kwargs ):
4653 return Payload(** data)
4754
4855
56+ # Form Payload
57+ class FormFile (fields .Field ):
58+ def _validate (self , value ):
59+ if not isinstance (value, UploadFile):
60+ raise ValidationError(' Not a file' )
61+
62+
63+ class FormPayload (Schema ):
64+ id = fields.Int(required = True )
65+ file = FormFile()
66+
67+
4968app = Starlette(debug = True )
5069
5170@app.exception_handler (ValidationError)
@@ -64,13 +83,32 @@ class Echo(Method):
6483 return query_params
6584
6685
67- @app.route (' /post' , methods = [' POST' ])
86+ @app.route (' /post/schema' , methods = [' POST' ])
87+ @endpoint
88+ class PostSchema (Method ):
89+ meta = Operation(tag = ' default' , description = ' post json (by schema)' )
90+
91+ async def execute (self , item : json_payload(JsonPayloadSchema)):
92+ return {' a' : item.get(' a' ) * 2 , ' s' : item.get(' s' )}
93+
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+
103+ @app.route (' /form' , methods = [' POST' ])
68104@endpoint
69- class Post (Method ):
70- meta = Operation(tag = ' default' , description = ' post' )
105+ class PostForm (Method ):
106+ meta = Operation(tag = ' default' , description = ' post form ' )
71107
72- async def execute (self , item : json_schema(PayloadSchema, Payload)):
73- return {' a' : item.a * 2 , ' s' : item.s}
108+ async def execute (self , form_data : form_payload(FormPayload)):
109+ file_name = form_data.get(' file' ).filename
110+ id = form_data.get(' id' )
111+ return {' message' : f " file { file_name} with id { id } received " }
74112
75113
76114if __name__ == ' __main__' :
0 commit comments