|
| 1 | +export const description = |
| 2 | + 'Welcome to the Robyn API documentation. You will find comprehensive guides and documentation to help you start working with Robyn as quickly as possible, as well as support if you get stuck.' |
| 3 | + |
| 4 | + |
| 5 | +## OpenAPI Docs |
| 6 | + |
| 7 | +After deploying the application, Batman got multiple queries from the users on how to use the endpoints. Robyn showed him how to generate OpenAPI specifications for his application. |
| 8 | + |
| 9 | +Out of the box, the following endpoints are setup for you: |
| 10 | + |
| 11 | +- `/docs` The Swagger UI |
| 12 | +- `/openapi.json` The JSON Specification |
| 13 | + |
| 14 | +## How to use? |
| 15 | + |
| 16 | +- Query Params: The typing for query params can be added as `def get(r: Request, query_params=GetRequestParams)` where `GetRequestParams` is a `TypedDict` |
| 17 | +- Path Params are defaulted to string type (ref: https://en.wikipedia.org/wiki/Query_string) |
| 18 | + |
| 19 | +<CodeGroup title="Basic App"> |
| 20 | + |
| 21 | +```python {{ title: 'untyped' }} |
| 22 | +from typing import TypedDict |
| 23 | + |
| 24 | +from robyn import Robyn, OpenAPI |
| 25 | +from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components |
| 26 | + |
| 27 | +app = Robyn( |
| 28 | + file_object=__file__, |
| 29 | + openapi=OpenAPI( |
| 30 | + info=OpenAPIInfo( |
| 31 | + title="Sample App", |
| 32 | + description="This is a sample server application.", |
| 33 | + termsOfService="https://example.com/terms/", |
| 34 | + version="1.0.0", |
| 35 | + contact=Contact( |
| 36 | + name="API Support", |
| 37 | + url="https://www.example.com/support", |
| 38 | + |
| 39 | + ), |
| 40 | + license=License( |
| 41 | + name="BSD2.0", |
| 42 | + url="https://opensource.org/license/bsd-2-clause", |
| 43 | + ), |
| 44 | + externalDocs=ExternalDocumentation(description="Find more info here", url="https://example.com/"), |
| 45 | + components=Components(), |
| 46 | + ), |
| 47 | + ), |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +@app.get("/") |
| 52 | +async def welcome(): |
| 53 | + """welcome endpoint""" |
| 54 | + return "hi" |
| 55 | + |
| 56 | + |
| 57 | +class GetRequestParams(TypedDict): |
| 58 | + appointment_id: str |
| 59 | + year: int |
| 60 | + |
| 61 | + |
| 62 | +@app.get("/api/v1/name", openapi_tags=["Name"]) |
| 63 | +async def get(r, query_params=GetRequestParams): |
| 64 | + """Get Name by ID""" |
| 65 | + return r.query_params |
| 66 | + |
| 67 | + |
| 68 | +@app.delete("/users/:name", openapi_tags=["Name"]) |
| 69 | +async def delete(r): |
| 70 | + """Delete Name by ID""" |
| 71 | + return r.path_params |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + app.start() |
| 76 | +``` |
| 77 | + |
| 78 | +```python {{ title: 'typed' }} |
| 79 | +from typing import TypedDict |
| 80 | + |
| 81 | +from robyn import Robyn, OpenAPI, Request |
| 82 | +from robyn.openapi import OpenAPIInfo, Contact, License, ExternalDocumentation, Components |
| 83 | + |
| 84 | +app: Robyn = Robyn( |
| 85 | + file_object=__file__, |
| 86 | + openapi=OpenAPI( |
| 87 | + info=OpenAPIInfo( |
| 88 | + title="Sample App", |
| 89 | + description="This is a sample server application.", |
| 90 | + termsOfService="https://example.com/terms/", |
| 91 | + version="1.0.0", |
| 92 | + contact=Contact( |
| 93 | + name="API Support", |
| 94 | + url="https://www.example.com/support", |
| 95 | + |
| 96 | + ), |
| 97 | + license=License( |
| 98 | + name="BSD2.0", |
| 99 | + url="https://opensource.org/license/bsd-2-clause", |
| 100 | + ), |
| 101 | + externalDocs=ExternalDocumentation(description="Find more info here", url="https://example.com/"), |
| 102 | + components=Components(), |
| 103 | + ), |
| 104 | + ), |
| 105 | +) |
| 106 | + |
| 107 | + |
| 108 | +@app.get("/") |
| 109 | +async def welcome(): |
| 110 | + """welcome endpoint""" |
| 111 | + return "hi" |
| 112 | + |
| 113 | + |
| 114 | +class GetRequestParams(TypedDict): |
| 115 | + appointment_id: str |
| 116 | + year: int |
| 117 | + |
| 118 | + |
| 119 | +@app.get("/api/v1/name", openapi_tags=["Name"]) |
| 120 | +async def get(r: Request, query_params=GetRequestParams): |
| 121 | + """Get Name by ID""" |
| 122 | + return r.query_params |
| 123 | + |
| 124 | + |
| 125 | +@app.delete("/users/:name", openapi_tags=["Name"]) |
| 126 | +async def delete(r: Request): |
| 127 | + """Delete Name by ID""" |
| 128 | + return r.path_params |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + app.start() |
| 133 | +``` |
| 134 | + |
| 135 | +</CodeGroup> |
| 136 | + |
| 137 | +## How does it work with subrouters? |
| 138 | + |
| 139 | +<CodeGroup title="Subrouters"> |
| 140 | + |
| 141 | +```python {{ title: 'untyped' }} |
| 142 | +from typing import TypedDict |
| 143 | + |
| 144 | +from robyn import SubRouter |
| 145 | + |
| 146 | +subrouter = SubRouter(__name__, prefix="/sub") |
| 147 | + |
| 148 | + |
| 149 | +@subrouter.get("/") |
| 150 | +async def subrouter_welcome(): |
| 151 | + """welcome subrouter""" |
| 152 | + return "hiiiiii subrouter" |
| 153 | + |
| 154 | + |
| 155 | +class SubRouterGetRequestParams(TypedDict): |
| 156 | + _id: int |
| 157 | + value: str |
| 158 | + |
| 159 | + |
| 160 | +@subrouter.get("/name") |
| 161 | +async def subrouter_get(r, query_params=SubRouterGetRequestParams): |
| 162 | + """Get Name by ID""" |
| 163 | + return r.query_params |
| 164 | + |
| 165 | + |
| 166 | +@subrouter.delete("/:name") |
| 167 | +async def subrouter_delete(r): |
| 168 | + """Delete Name by ID""" |
| 169 | + return r.path_params |
| 170 | + |
| 171 | + |
| 172 | +app.include_router(subrouter) |
| 173 | +``` |
| 174 | + |
| 175 | +```python {{ title: 'typed' }} |
| 176 | +from typing import TypedDict |
| 177 | + |
| 178 | +from robyn import Request, SubRouter |
| 179 | + |
| 180 | +subrouter: SubRouter = SubRouter(__name__, prefix="/sub") |
| 181 | + |
| 182 | + |
| 183 | +@subrouter.get("/") |
| 184 | +async def subrouter_welcome(): |
| 185 | + """welcome subrouter""" |
| 186 | + return "hiiiiii subrouter" |
| 187 | + |
| 188 | + |
| 189 | +class SubRouterGetRequestParams(TypedDict): |
| 190 | + _id: int |
| 191 | + value: str |
| 192 | + |
| 193 | + |
| 194 | +@subrouter.get("/name") |
| 195 | +async def subrouter_get(r: Request, query_params=SubRouterGetRequestParams): |
| 196 | + """Get Name by ID""" |
| 197 | + return r.query_params |
| 198 | + |
| 199 | + |
| 200 | +@subrouter.delete("/:name") |
| 201 | +async def subrouter_delete(r: Request): |
| 202 | + """Delete Name by ID""" |
| 203 | + return r.path_params |
| 204 | + |
| 205 | + |
| 206 | +app.include_router(subrouter) |
| 207 | +``` |
| 208 | + |
| 209 | +</CodeGroup> |
| 210 | + |
| 211 | +With the reference documentation deployed and running smoothly, Batman had a powerful new tool at his disposal. The Robyn framework had provided him with the flexibility, scalability, and performance needed to create an effective crime-fighting application, giving him a technological edge in his ongoing battle to protect Gotham City. |
| 212 | + |
| 213 | + |
| 214 | +## What's next? |
| 215 | + |
| 216 | + |
| 217 | +Batman wondered about whether Robyn handlers can be dispatched to multiple processes. |
| 218 | + |
| 219 | +Robyn showed him the way! |
| 220 | + |
| 221 | +[Multitiprocess Execution](/documentation/api_reference/multiprocess_execution) |
| 222 | + |
| 223 | + |
| 224 | + |
0 commit comments