|
33 | 33 | pip install ellar |
34 | 34 | ``` |
35 | 35 |
|
| 36 | +## Usage |
| 37 | + |
| 38 | +Create a file `controller.py`: |
| 39 | + |
| 40 | +```Python |
| 41 | +from ellar.common import ModuleRouter, Controller, get |
| 42 | + |
| 43 | +router = ModuleRouter('', tag='Math') |
| 44 | + |
| 45 | + |
| 46 | +@router.get("/add") |
| 47 | +def add(request, a: int, b: int): |
| 48 | + return {"result": a + b} |
| 49 | + |
| 50 | + |
| 51 | +@Controller("", tag='Math') |
| 52 | +class MathAPI: |
| 53 | + |
| 54 | + @get('/subtract', ) |
| 55 | + def subtract(self, a: int, b: int): |
| 56 | + """Subtracts a from b""" |
| 57 | + return {"result": a - b} |
| 58 | + |
| 59 | + @get('/divide', ) |
| 60 | + def divide(self, a: int, b: int): |
| 61 | + """Divides a by b""" |
| 62 | + return {"result": a / b} |
| 63 | + |
| 64 | + @get('/multiple', ) |
| 65 | + def multiple(self, a: int, b: int): |
| 66 | + """Multiples a with b""" |
| 67 | + return {"result": a * b} |
| 68 | + |
| 69 | +``` |
| 70 | + |
| 71 | +Create another file `server.py`: |
| 72 | + |
| 73 | +```Python |
| 74 | +from ellar.core import AppFactory |
| 75 | +from ellar.openapi.builder import OpenAPIDocumentBuilder |
| 76 | +from ellar.openapi.module import OpenAPIDocumentModule |
| 77 | +from .controller import router, MathAPI |
| 78 | + |
| 79 | + |
| 80 | +app = AppFactory.create_app(routers=(router, ), controllers=(MathAPI, )) |
| 81 | + |
| 82 | +document_builder = OpenAPIDocumentBuilder() |
| 83 | +document_builder.set_title('Your Title')\ |
| 84 | + .set_version('1.0.2')\ |
| 85 | + .set_contact( name='Eadwin', url='https://www.yahoo.com', email='[email protected]')\ |
| 86 | + .set_license('MIT Licence', url='https://www.google.com') |
| 87 | + |
| 88 | +document = document_builder.build_document(app) |
| 89 | +module = app.install_module(OpenAPIDocumentModule, document=document) |
| 90 | +module.setup_swagger_doc() |
| 91 | +``` |
| 92 | + |
| 93 | +### Interactive API docs |
| 94 | + |
| 95 | +Now go to <a href="http://localhost:8000/docs/" target="_blank">http://localhost:8000/docs/</a> |
| 96 | + |
| 97 | +You will see the automatic interactive API documentation (provided by <a href="https://github.com/swagger-api/swagger-ui" target="_blank">Swagger UI</a>): |
| 98 | + |
| 99 | + |
| 100 | + |
36 | 101 | ## Status |
37 | 102 | Project is still in development |
| 103 | +- Remaining testing modules: |
| 104 | + - common |
| 105 | + - configuration |
| 106 | + - guard |
| 107 | + - openapi |
| 108 | +- Project CLI scaffolding |
| 109 | +- Database Plugin with [Encode/ORM](https://github.com/encode/orm) |
| 110 | +- Caching |
| 111 | +- API Throttling |
0 commit comments