forked from sparckles/Robyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bd4921
commit abe068d
Showing
2 changed files
with
88 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,19 +19,37 @@ python app.py --disable-openapi | |
|
||
## How to use? | ||
|
||
- Query Params: The typing for query params can be added as `def get(r: Request, query_params=GetRequestParams)` where `GetRequestParams` is a subclass of `RequestQuery` | ||
- Query Params: The typing for query params can be added as `def get(r: Request, query_params: GetRequestParams)` where `GetRequestParams` is a subclass of `QueryParams` | ||
- Path Params are defaulted to string type (ref: https://en.wikipedia.org/wiki/Query_string) | ||
|
||
<CodeGroup title="Basic App"> | ||
|
||
```python {{ title: 'untyped' }} | ||
from robyn.types import RequestBody, RequestQuery | ||
|
||
from robyn import Robyn, OpenAPI | ||
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components | ||
|
||
|
||
app = Robyn(file_object=__file__) | ||
from robyn import Robyn | ||
from robyn.robyn import QueryParams | ||
|
||
app = Robyn( | ||
file_object=__file__, | ||
openapi=OpenAPI( | ||
info=OpenAPIInfo( | ||
title="Sample App", | ||
description="This is a sample server application.", | ||
termsOfService="https://example.com/terms/", | ||
version="1.0.0", | ||
contact=Contact( | ||
name="API Support", | ||
url="https://www.example.com/support", | ||
email="[email protected]", | ||
), | ||
license=License( | ||
name="BSD2.0", | ||
url="https://opensource.org/license/bsd-2-clause", | ||
), | ||
externalDocs=ExternalDocumentation(description="Find more info here", url="https://example.com/"), | ||
components=Components(), | ||
), | ||
), | ||
) | ||
|
||
|
||
@app.get("/") | ||
|
@@ -40,7 +58,7 @@ async def welcome(): | |
return "hi" | ||
|
||
|
||
class GetRequestParams(RequestQuery): | ||
class GetRequestParams(QueryParams): | ||
appointment_id: str | ||
year: int | ||
|
||
|
@@ -62,12 +80,32 @@ if __name__ == "__main__": | |
``` | ||
|
||
```python {{ title: 'typed' }} | ||
from robyn.types import RequestBody, RequestQuery | ||
|
||
from robyn import Robyn, OpenAPI, Request | ||
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components | ||
|
||
app: Robyn = Robyn(file_object=__file__) | ||
from robyn.robyn import QueryParams | ||
|
||
from robyn import Robyn, Request | ||
|
||
app = Robyn( | ||
file_object=__file__, | ||
openapi=OpenAPI( | ||
info=OpenAPIInfo( | ||
title="Sample App", | ||
description="This is a sample server application.", | ||
termsOfService="https://example.com/terms/", | ||
version="1.0.0", | ||
contact=Contact( | ||
name="API Support", | ||
url="https://www.example.com/support", | ||
email="[email protected]", | ||
), | ||
license=License( | ||
name="BSD2.0", | ||
url="https://opensource.org/license/bsd-2-clause", | ||
), | ||
externalDocs=ExternalDocumentation(description="Find more info here", url="https://example.com/"), | ||
components=Components(), | ||
), | ||
), | ||
) | ||
|
||
|
||
@app.get("/") | ||
|
@@ -76,7 +114,7 @@ async def welcome(): | |
return "hi" | ||
|
||
|
||
class GetRequestParams(RequestQuery): | ||
class GetRequestParams(QueryParams): | ||
appointment_id: str | ||
year: int | ||
|
||
|
@@ -104,9 +142,8 @@ if __name__ == "__main__": | |
<CodeGroup title="Subrouters"> | ||
|
||
```python {{ title: 'untyped' }} | ||
from robyn.types import RequestQuery | ||
|
||
from robyn import SubRouter | ||
from robyn.robyn import QueryParams | ||
|
||
subrouter = SubRouter(__name__, prefix="/sub") | ||
|
||
|
@@ -117,7 +154,7 @@ async def subrouter_welcome(): | |
return "hiiiiii subrouter" | ||
|
||
|
||
class SubRouterGetRequestParams(RequestQuery): | ||
class SubRouterGetRequestParams(QueryParams): | ||
_id: int | ||
value: str | ||
|
||
|
@@ -138,7 +175,7 @@ app.include_router(subrouter) | |
``` | ||
|
||
```python {{ title: 'typed' }} | ||
from robyn.types import RequestQuery | ||
from robyn.robyn import QueryParams | ||
|
||
from robyn import Request, SubRouter | ||
|
||
|
@@ -151,7 +188,7 @@ async def subrouter_welcome(): | |
return "hiiiiii subrouter" | ||
|
||
|
||
class SubRouterGetRequestParams(RequestQuery): | ||
class SubRouterGetRequestParams(QueryParams): | ||
_id: int | ||
value: str | ||
|
||
|
@@ -180,20 +217,20 @@ We support all the params mentioned in the latest OpenAPI specifications (https: | |
<CodeGroup title="Request & Response Body"> | ||
|
||
```python {{ title: 'untyped' }} | ||
from robyn.types import JSONResponse, RequestBody | ||
from robyn.types import JSONResponse, Body | ||
|
||
class Initial(RequestBody): | ||
class Initial(Body): | ||
is_present: bool | ||
letter: Optional[str] | ||
|
||
|
||
class FullName(RequestBody): | ||
class FullName(Body): | ||
first: str | ||
second: str | ||
initial: Initial | ||
|
||
|
||
class CreateItemBody(RequestBody): | ||
class CreateItemBody(Body): | ||
name: FullName | ||
description: str | ||
price: float | ||
|
@@ -211,25 +248,26 @@ def create_item(request: Request, body: CreateItemBody) -> CreateResponse: | |
``` | ||
|
||
```python {{ title: 'typed' }} | ||
from robyn.types import JSONResponse, RequestBody | ||
from robyn.types import JSONResponse, Body | ||
|
||
class Initial(RequestBody): | ||
class Initial(Body): | ||
is_present: bool | ||
letter: Optional[str] | ||
|
||
|
||
class FullName(RequestBody): | ||
class FullName(Body): | ||
first: str | ||
second: str | ||
initial: Initial | ||
|
||
|
||
class CreateItemBody(RequestBody): | ||
class CreateItemBody(Body): | ||
name: FullName | ||
description: str | ||
price: float | ||
tax: float | ||
|
||
|
||
class CreateResponse(JSONResponse): | ||
success: bool | ||
items_changed: int | ||
|