Skip to content

Commit

Permalink
update openapi docs with new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Sep 25, 2024
1 parent fa7cbc0 commit ff71f81
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
46 changes: 23 additions & 23 deletions docs_src/src/pages/documentation/api_reference/openapi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ 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 `TypedDict`
- Query Params: The typing for query params can be added as `def get(r: Request, query_params=GetRequestParams)` where `GetRequestParams` is a subclass of `QueryParam`
- Path Params are defaulted to string type (ref: https://en.wikipedia.org/wiki/Query_string)

<CodeGroup title="Basic App">

```python {{ title: 'untyped' }}
from typing import TypedDict
from robyn.types import RequestBody, QueryParam

from robyn import Robyn, OpenAPI
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components
Expand Down Expand Up @@ -61,13 +61,13 @@ async def welcome():
return "hi"


class GetRequestParams(TypedDict):
class GetRequestParams(QueryParam):
appointment_id: str
year: int


@app.get("/api/v1/name", openapi_name="Name Route", openapi_tags=["Name"])
async def get(r, query_params=GetRequestParams):
async def get(r, query_params: GetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -83,7 +83,7 @@ if __name__ == "__main__":
```

```python {{ title: 'typed' }}
from typing import TypedDict
from robyn.types import RequestBody, QueryParam

from robyn import Robyn, OpenAPI, Request
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components
Expand Down Expand Up @@ -118,13 +118,13 @@ async def welcome():
return "hi"


class GetRequestParams(TypedDict):
class GetRequestParams(QueryParam):
appointment_id: str
year: int


@app.get("/api/v1/name", openapi_name="Name Route", openapi_tags=["Name"])
async def get(r: Request, query_params=GetRequestParams):
async def get(r: Request, query_params: GetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -146,7 +146,7 @@ if __name__ == "__main__":
<CodeGroup title="Subrouters">

```python {{ title: 'untyped' }}
from typing import TypedDict
from robyn.types import QueryParam

from robyn import SubRouter

Expand All @@ -159,13 +159,13 @@ async def subrouter_welcome():
return "hiiiiii subrouter"


class SubRouterGetRequestParams(TypedDict):
class SubRouterGetRequestParams(QueryParam):
_id: int
value: str


@subrouter.get("/name")
async def subrouter_get(r, query_params=SubRouterGetRequestParams):
async def subrouter_get(r, query_params: SubRouterGetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -180,7 +180,7 @@ app.include_router(subrouter)
```

```python {{ title: 'typed' }}
from typing import TypedDict
from robyn.types import QueryParam

from robyn import Request, SubRouter

Expand All @@ -193,13 +193,13 @@ async def subrouter_welcome():
return "hiiiiii subrouter"


class SubRouterGetRequestParams(TypedDict):
class SubRouterGetRequestParams(QueryParam):
_id: int
value: str


@subrouter.get("/name")
async def subrouter_get(r: Request, query_params=SubRouterGetRequestParams):
async def subrouter_get(r: Request, query_params: SubRouterGetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -222,20 +222,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
from robyn.types import JSONResponse, RequestBody

class Initial(TypedDict):
class Initial(RequestBody):
is_present: bool
letter: Optional[str]


class FullName(TypedDict):
class FullName(RequestBody):
first: str
second: str
initial: Initial


class CreateItemBody(TypedDict):
class CreateItemBody(RequestBody):
name: FullName
description: str
price: float
Expand All @@ -248,25 +248,25 @@ class CreateResponse(JSONResponse):


@app.post("/")
def create_item(request: Request, body=CreateItemBody) -> CreateResponse:
def create_item(request: Request, body: CreateItemBody) -> CreateResponse:
return CreateResponse(success=True, items_changed=2)
```

```python {{ title: 'typed' }}
from robyn.types import JSONResponse
from robyn.types import JSONResponse, RequestBody

class Initial(TypedDict):
class Initial(RequestBody):
is_present: bool
letter: Optional[str]


class FullName(TypedDict):
class FullName(RequestBody):
first: str
second: str
initial: Initial


class CreateItemBody(TypedDict):
class CreateItemBody(RequestBody):
name: FullName
description: str
price: float
Expand All @@ -278,7 +278,7 @@ class CreateResponse(JSONResponse):


@app.post("/")
def create_item(request: Request, body=CreateItemBody) -> CreateResponse:
def create_item(request: Request, body: CreateItemBody) -> CreateResponse:
return CreateResponse(success=True, items_changed=2)
```

Expand Down
48 changes: 24 additions & 24 deletions docs_src/src/pages/documentation/example_app/openapi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ 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 `TypedDict`
- Query Params: The typing for query params can be added as `def get(r: Request, query_params=GetRequestParams)` where `GetRequestParams` is a subclass of `QueryParam`
- Path Params are defaulted to string type (ref: https://en.wikipedia.org/wiki/Query_string)

<CodeGroup title="Basic App">

```python {{ title: 'untyped' }}
from typing import TypedDict
from robyn.types import RequestBody, QueryParam

from robyn import Robyn, OpenAPI
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components
Expand All @@ -40,13 +40,13 @@ async def welcome():
return "hi"


class GetRequestParams(TypedDict):
class GetRequestParams(QueryParam):
appointment_id: str
year: int


@app.get("/api/v1/name", openapi_name="Name Route", openapi_tags=["Name"])
async def get(r, query_params=GetRequestParams):
async def get(r, query_params: GetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -62,7 +62,7 @@ if __name__ == "__main__":
```

```python {{ title: 'typed' }}
from typing import TypedDict
from robyn.types import RequestBody, QueryParam

from robyn import Robyn, OpenAPI, Request
from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components
Expand All @@ -76,13 +76,13 @@ async def welcome():
return "hi"


class GetRequestParams(TypedDict):
class GetRequestParams(QueryParam):
appointment_id: str
year: int


@app.get("/api/v1/name", openapi_name="Name Route", openapi_tags=["Name"])
async def get(r: Request, query_params=GetRequestParams):
async def get(r: Request, query_params: GetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -104,7 +104,7 @@ if __name__ == "__main__":
<CodeGroup title="Subrouters">

```python {{ title: 'untyped' }}
from typing import TypedDict
from robyn.types import QueryParam

from robyn import SubRouter

Expand All @@ -117,13 +117,13 @@ async def subrouter_welcome():
return "hiiiiii subrouter"


class SubRouterGetRequestParams(TypedDict):
class SubRouterGetRequestParams(QueryParam):
_id: int
value: str


@subrouter.get("/name")
async def subrouter_get(r, query_params=SubRouterGetRequestParams):
async def subrouter_get(r, query_params: SubRouterGetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -138,7 +138,7 @@ app.include_router(subrouter)
```

```python {{ title: 'typed' }}
from typing import TypedDict
from robyn.types import QueryParam

from robyn import Request, SubRouter

Expand All @@ -151,13 +151,13 @@ async def subrouter_welcome():
return "hiiiiii subrouter"


class SubRouterGetRequestParams(TypedDict):
class SubRouterGetRequestParams(QueryParam):
_id: int
value: str


@subrouter.get("/name")
async def subrouter_get(r: Request, query_params=SubRouterGetRequestParams):
async def subrouter_get(r: Request, query_params: SubRouterGetRequestParams):
"""Get Name by ID"""
return r.query_params

Expand All @@ -180,20 +180,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
from robyn.types import JSONResponse, RequestBody

class Initial(TypedDict):
class Initial(RequestBody):
is_present: bool
letter: Optional[str]


class FullName(TypedDict):
class FullName(RequestBody):
first: str
second: str
initial: Initial


class CreateItemBody(TypedDict):
class CreateItemBody(RequestBody):
name: FullName
description: str
price: float
Expand All @@ -206,25 +206,25 @@ class CreateResponse(JSONResponse):


@app.post("/")
def create_item(request: Request, body=CreateItemBody) -> CreateResponse:
return {"success": True, "items_changed": 2}
def create_item(request: Request, body: CreateItemBody) -> CreateResponse:
return CreateResponse(success=True, items_changed=2)
```

```python {{ title: 'typed' }}
from robyn.types import JSONResponse
from robyn.types import JSONResponse, RequestBody

class Initial(TypedDict):
class Initial(RequestBody):
is_present: bool
letter: Optional[str]


class FullName(TypedDict):
class FullName(RequestBody):
first: str
second: str
initial: Initial


class CreateItemBody(TypedDict):
class CreateItemBody(RequestBody):
name: FullName
description: str
price: float
Expand All @@ -236,7 +236,7 @@ class CreateResponse(JSONResponse):


@app.post("/")
def create_item(request: Request, body=CreateItemBody) -> CreateResponse:
def create_item(request: Request, body: CreateItemBody) -> CreateResponse:
return CreateResponse(success=True, items_changed=2)
```

Expand Down

0 comments on commit ff71f81

Please sign in to comment.