lightapi 0.1.5__tar.gz → 0.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. lightapi-0.1.7/PKG-INFO +214 -0
  2. lightapi-0.1.7/README.md +162 -0
  3. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/authentication.md +4 -0
  4. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/rest.md +16 -1
  5. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/auth.md +20 -31
  6. {lightapi-0.1.5 → lightapi-0.1.7}/docs/index.md +27 -0
  7. {lightapi-0.1.5 → lightapi-0.1.7}/docs/tutorial/endpoints.md +15 -0
  8. {lightapi-0.1.5 → lightapi-0.1.7}/examples/README.md +36 -50
  9. lightapi-0.1.5/examples/auth_example.py → lightapi-0.1.7/examples/authentication_jwt.py +5 -11
  10. lightapi-0.1.7/examples/blog_post.py +57 -0
  11. lightapi-0.1.5/examples/caching_example.py → lightapi-0.1.7/examples/caching_redis_custom.py +2 -3
  12. lightapi-0.1.5/examples/user_goal_example.py → lightapi-0.1.7/examples/comprehensive_ideal_usage.py +4 -3
  13. lightapi-0.1.5/examples/filtering_pagination_example.py → lightapi-0.1.7/examples/filtering_pagination.py +3 -11
  14. lightapi-0.1.5/examples/example.py → lightapi-0.1.7/examples/general_usage.py +7 -10
  15. lightapi-0.1.7/examples/mega_example.py +660 -0
  16. lightapi-0.1.5/examples/custom_snippet.py → lightapi-0.1.7/examples/middleware_cors_auth.py +14 -27
  17. lightapi-0.1.5/examples/middleware_example.py → lightapi-0.1.7/examples/middleware_custom.py +4 -14
  18. lightapi-0.1.5/examples/relationships_example.py → lightapi-0.1.7/examples/relationships_sqlalchemy.py +31 -73
  19. lightapi-0.1.5/examples/basic_rest_api.py → lightapi-0.1.7/examples/rest_crud_basic.py +1 -1
  20. lightapi-0.1.5/examples/swagger_example.py → lightapi-0.1.7/examples/swagger_openapi_docs.py +5 -10
  21. lightapi-0.1.5/examples/validation_example.py → lightapi-0.1.7/examples/validation_custom_fields.py +1 -1
  22. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/auth.py +1 -3
  23. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/base_endpoint.py +1 -3
  24. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/config.py +6 -18
  25. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/core.py +56 -109
  26. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/database.py +12 -2
  27. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/exceptions.py +1 -2
  28. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/handlers.py +125 -64
  29. lightapi-0.1.7/lightapi/lightapi.py +719 -0
  30. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/models.py +57 -11
  31. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/rest.py +140 -27
  32. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/swagger.py +9 -35
  33. {lightapi-0.1.5 → lightapi-0.1.7}/pyproject.toml +1 -1
  34. lightapi-0.1.7/run_server.py +6 -0
  35. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_additional_features.py +2 -62
  36. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_auth.py +5 -2
  37. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_cache.py +7 -32
  38. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_caching_example.py +19 -160
  39. lightapi-0.1.7/tests/test_core.py +41 -0
  40. lightapi-0.1.7/tests/test_custom_snippet.py +40 -0
  41. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_filtering_pagination_example.py +7 -7
  42. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_filters.py +4 -24
  43. lightapi-0.1.7/tests/test_from_config.py +1149 -0
  44. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_helpers.py +3 -19
  45. lightapi-0.1.7/tests/test_integration.py +54 -0
  46. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_middleware.py +4 -0
  47. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_rest.py +1 -17
  48. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_swagger.py +2 -6
  49. lightapi-0.1.7/tests/test_validators.py +45 -0
  50. lightapi-0.1.7/uv.lock +3107 -0
  51. lightapi-0.1.5/PKG-INFO +0 -273
  52. lightapi-0.1.5/README.md +0 -221
  53. lightapi-0.1.5/lightapi/lightapi.py +0 -341
  54. lightapi-0.1.5/run_server.py +0 -6
  55. lightapi-0.1.5/tests/test_auth_example.py +0 -301
  56. lightapi-0.1.5/tests/test_basic_rest_api.py +0 -209
  57. lightapi-0.1.5/tests/test_core.py +0 -81
  58. lightapi-0.1.5/tests/test_custom_snippet.py +0 -328
  59. lightapi-0.1.5/tests/test_example.py +0 -56
  60. lightapi-0.1.5/tests/test_from_config.py +0 -932
  61. lightapi-0.1.5/tests/test_integration.py +0 -112
  62. lightapi-0.1.5/tests/test_middleware_example.py +0 -337
  63. lightapi-0.1.5/tests/test_validation_example.py +0 -210
  64. lightapi-0.1.5/uv.lock +0 -734
  65. {lightapi-0.1.5 → lightapi-0.1.7}/.github/workflows/pages-publish.yml +0 -0
  66. {lightapi-0.1.5 → lightapi-0.1.7}/.github/workflows/python-publish.yml +0 -0
  67. {lightapi-0.1.5 → lightapi-0.1.7}/.github/workflows/test-dev.yml +0 -0
  68. {lightapi-0.1.5 → lightapi-0.1.7}/.gitignore +0 -0
  69. {lightapi-0.1.5 → lightapi-0.1.7}/LICENSE +0 -0
  70. {lightapi-0.1.5 → lightapi-0.1.7}/docs/.pages +0 -0
  71. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/.pages +0 -0
  72. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/caching.md +0 -0
  73. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/filtering.md +0 -0
  74. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/middleware.md +0 -0
  75. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/pagination.md +0 -0
  76. {lightapi-0.1.5 → lightapi-0.1.7}/docs/advanced/validation.md +0 -0
  77. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/.pages +0 -0
  78. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/auth.md +0 -0
  79. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/cache.md +0 -0
  80. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/core.md +0 -0
  81. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/database.md +0 -0
  82. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/exceptions.md +0 -0
  83. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/filters.md +0 -0
  84. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/index.md +0 -0
  85. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/models.md +0 -0
  86. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/pagination.md +0 -0
  87. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/swagger.md +0 -0
  88. {lightapi-0.1.5 → lightapi-0.1.7}/docs/api-reference/validation.md +0 -0
  89. {lightapi-0.1.5 → lightapi-0.1.7}/docs/deployment/.pages +0 -0
  90. {lightapi-0.1.5 → lightapi-0.1.7}/docs/deployment/docker.md +0 -0
  91. {lightapi-0.1.5 → lightapi-0.1.7}/docs/deployment/production.md +0 -0
  92. {lightapi-0.1.5 → lightapi-0.1.7}/docs/deployment/security.md +0 -0
  93. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/.pages +0 -0
  94. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/basic-crud.md +0 -0
  95. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/basic-rest.md +0 -0
  96. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/caching.md +0 -0
  97. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/custom-application.md +0 -0
  98. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/filtering-pagination.md +0 -0
  99. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/middleware.md +0 -0
  100. {lightapi-0.1.5 → lightapi-0.1.7}/docs/examples/validation.md +0 -0
  101. {lightapi-0.1.5 → lightapi-0.1.7}/docs/getting-started/.pages +0 -0
  102. {lightapi-0.1.5 → lightapi-0.1.7}/docs/getting-started/configuration.md +0 -0
  103. {lightapi-0.1.5 → lightapi-0.1.7}/docs/getting-started/first-steps.md +0 -0
  104. {lightapi-0.1.5 → lightapi-0.1.7}/docs/getting-started/installation.md +0 -0
  105. {lightapi-0.1.5 → lightapi-0.1.7}/docs/getting-started/introduction.md +0 -0
  106. {lightapi-0.1.5 → lightapi-0.1.7}/docs/getting-started/quickstart.md +0 -0
  107. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/.pages +0 -0
  108. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/cache.md +0 -0
  109. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/core-api.md +0 -0
  110. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/endpoints.md +0 -0
  111. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/handlers.md +0 -0
  112. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/middleware.md +0 -0
  113. {lightapi-0.1.5 → lightapi-0.1.7}/docs/technical-reference/models.md +0 -0
  114. {lightapi-0.1.5 → lightapi-0.1.7}/docs/troubleshooting.md +0 -0
  115. {lightapi-0.1.5 → lightapi-0.1.7}/docs/tutorial/.pages +0 -0
  116. {lightapi-0.1.5 → lightapi-0.1.7}/docs/tutorial/basic-api.md +0 -0
  117. {lightapi-0.1.5 → lightapi-0.1.7}/docs/tutorial/database.md +0 -0
  118. {lightapi-0.1.5 → lightapi-0.1.7}/docs/tutorial/requests.md +0 -0
  119. {lightapi-0.1.5 → lightapi-0.1.7}/docs/tutorial/responses.md +0 -0
  120. {lightapi-0.1.5 → lightapi-0.1.7}/examples/__init__.py +0 -0
  121. lightapi-0.1.5/tests/__init__.py → lightapi-0.1.7/examples/example.py +0 -0
  122. lightapi-0.1.5/tests/test_clients.py → lightapi-0.1.7/examples/user_goal_example.py +0 -0
  123. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/__init__.py +1 -1
  124. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/cache.py +0 -0
  125. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/filters.py +0 -0
  126. {lightapi-0.1.5 → lightapi-0.1.7}/lightapi/pagination.py +0 -0
  127. {lightapi-0.1.5 → lightapi-0.1.7}/mkdocs.yml +0 -0
  128. {lightapi-0.1.5 → lightapi-0.1.7}/pytest.ini +0 -0
  129. {lightapi-0.1.5 → lightapi-0.1.7}/requirements.txt +0 -0
  130. {lightapi-0.1.5 → lightapi-0.1.7}/tests/conftest.py +0 -0
  131. {lightapi-0.1.5 → lightapi-0.1.7}/tests/test_pagination.py +0 -0
  132. {lightapi-0.1.5 → lightapi-0.1.7}/update_version.py +0 -0
@@ -0,0 +1,214 @@
1
+ Metadata-Version: 2.4
2
+ Name: lightapi
3
+ Version: 0.1.7
4
+ Summary: A lightweight framework for building API endpoints using Python's native libraries.
5
+ Project-URL: Repository, https://github.com/henriqueblobato/LightApi
6
+ Project-URL: Issues, https://github.com/henriqueblobato/LightApi/issues
7
+ Project-URL: Homepage, https://github.com/henriqueblobato/LightApi
8
+ Author-email: iklobato <iklobato1@gmail.com>
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: api,endpoint,framework,lightweight,rest,restful
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Topic :: Internet :: WWW/HTTP
20
+ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
21
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
22
+ Requires-Python: >=3.8.1
23
+ Requires-Dist: aiohttp<4.0.0,>=3.9.5
24
+ Requires-Dist: pyjwt<3.0.0,>=2.8.0
25
+ Requires-Dist: pyyaml>=5.1
26
+ Requires-Dist: redis<6.0.0,>=5.0.0
27
+ Requires-Dist: sqlalchemy<3.0.0,>=2.0.30
28
+ Requires-Dist: starlette<1.0.0,>=0.37.0
29
+ Requires-Dist: uvicorn<1.0.0,>=0.30.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: black<24.0.0,>=23.3.0; extra == 'dev'
32
+ Requires-Dist: flake8<7.0.0,>=6.0.0; extra == 'dev'
33
+ Requires-Dist: isort<6.0.0,>=5.12.0; extra == 'dev'
34
+ Requires-Dist: mypy<2.0.0,>=1.3.0; extra == 'dev'
35
+ Requires-Dist: pytest<8.0.0,>=7.3.1; extra == 'dev'
36
+ Provides-Extra: docs
37
+ Requires-Dist: mkdocs-awesome-pages-plugin; extra == 'docs'
38
+ Requires-Dist: mkdocs-git-authors-plugin; extra == 'docs'
39
+ Requires-Dist: mkdocs-git-committers-plugin-2; extra == 'docs'
40
+ Requires-Dist: mkdocs-git-revision-date-localized-plugin; extra == 'docs'
41
+ Requires-Dist: mkdocs-glightbox; extra == 'docs'
42
+ Requires-Dist: mkdocs-material; extra == 'docs'
43
+ Requires-Dist: mkdocstrings[python]; extra == 'docs'
44
+ Provides-Extra: test
45
+ Requires-Dist: httpx<1.0.0,>=0.27.0; extra == 'test'
46
+ Requires-Dist: pyjwt<3.0.0,>=2.8.0; extra == 'test'
47
+ Requires-Dist: pytest<8.0.0,>=7.3.1; extra == 'test'
48
+ Requires-Dist: redis<6.0.0,>=5.0.0; extra == 'test'
49
+ Requires-Dist: starlette<1.0.0,>=0.37.0; extra == 'test'
50
+ Requires-Dist: uvicorn<1.0.0,>=0.30.0; extra == 'test'
51
+ Description-Content-Type: text/markdown
52
+
53
+ # LightAPI: Instant Python REST APIs from SQL Databases
54
+
55
+ [![PyPI version](https://badge.fury.io/py/lightapi.svg)](https://pypi.org/project/lightapi/)
56
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
57
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
58
+
59
+ **LightAPI** is a modern Python framework for building high-performance REST APIs directly from your SQL database—no boilerplate, no manual endpoint wiring. Instantly expose CRUD endpoints from SQLAlchemy models or a YAML config, with full support for authentication, caching, validation, filtering, and OpenAPI documentation.
60
+
61
+ ---
62
+
63
+ ## Why use LightAPI?
64
+
65
+ - **Zero-boilerplate Python REST API**: Instantly generate CRUD endpoints from your database schema.
66
+ - **YAML-driven API generator**: Reflect your database and expose only the tables and operations you want.
67
+ - **Async and Fast**: Built on aiohttp for high concurrency and low latency.
68
+ - **Production-ready**: JWT authentication, Redis caching, request validation, and robust error handling.
69
+ - **Automatic OpenAPI docs**: Swagger UI and OpenAPI JSON out of the box.
70
+ - **Flexible**: Use with SQLite, PostgreSQL, MySQL, or any SQLAlchemy-supported database.
71
+ - **Modern Python**: Type hints, async/await, and best practices throughout.
72
+
73
+ ---
74
+
75
+ ## Who is this for?
76
+
77
+ - **Backend developers** who want to ship APIs fast, with minimal code.
78
+ - **Data engineers** needing to expose existing databases as RESTful services.
79
+ - **Prototypers** and **startups** who want to iterate quickly and scale later.
80
+ - **Anyone** who wants a clean, maintainable, and extensible Python API stack.
81
+
82
+ ---
83
+
84
+ ## Features
85
+
86
+ - 🚀 **Automatic CRUD endpoints** from SQLAlchemy models or YAML config
87
+ - 🔄 **Database reflection**: Expose existing tables instantly
88
+ - 🔐 **JWT authentication** with CORS support
89
+ - ⚡ **Async performance** (aiohttp)
90
+ - 💾 **Redis caching** with auto-invalidation
91
+ - 🧪 **Request validation** and error handling
92
+ - 🔍 **Filtering, pagination, and sorting**
93
+ - 📖 **OpenAPI/Swagger documentation** auto-generated
94
+ - 🔧 **Custom middleware** support
95
+ - 🗄️ **Works with SQLite, PostgreSQL, MySQL, and more**
96
+ - 📝 **Environment-based configuration**
97
+
98
+ ---
99
+
100
+ ## Quick Start
101
+
102
+ ### 1. Install LightAPI
103
+
104
+ ```bash
105
+ pip install lightapi
106
+ ```
107
+
108
+ ### 2. Define your model (SQLAlchemy)
109
+
110
+ ```python
111
+ from lightapi import LightApi
112
+ from lightapi.database import Base
113
+ from sqlalchemy import Column, Integer, String
114
+
115
+ class User(Base):
116
+ __tablename__ = "users"
117
+ id = Column(Integer, primary_key=True)
118
+ name = Column(String(50))
119
+ email = Column(String(100))
120
+
121
+ app = LightApi()
122
+ app.register(User)
123
+
124
+ if __name__ == "__main__":
125
+ app.run()
126
+ ```
127
+
128
+ ### 3. Or use YAML for instant API from your database
129
+
130
+ ```yaml
131
+ # config.yaml
132
+ database_url: sqlite:///mydata.db
133
+ tables:
134
+ - name: users
135
+ crud: [get, post, put, patch, delete]
136
+ - name: orders
137
+ crud: [get, post]
138
+ ```
139
+
140
+ ```python
141
+ from lightapi import LightApi
142
+ api = LightApi.from_config('config.yaml')
143
+ api.run(host="0.0.0.0", port=8081)
144
+ ```
145
+
146
+ ---
147
+
148
+ ## Example Endpoints (from YAML above)
149
+
150
+ - `GET /users/` - List users
151
+ - `POST /users/` - Create user
152
+ - `GET /users/{id}` - Get user by ID
153
+ - `PUT /users/{id}` - Replace user
154
+ - `PATCH /users/{id}` - Update user
155
+ - `DELETE /users/{id}` - Delete user
156
+ - `GET /orders/` - List orders
157
+ - `POST /orders/` - Create order
158
+ - `GET /orders/{id}` - Get order by ID
159
+
160
+ ---
161
+
162
+ ## Documentation
163
+
164
+ - [Full Documentation](https://iklobato.github.io/lightapi/)
165
+ - [Getting Started](https://iklobato.github.io/lightapi/getting-started/installation/)
166
+ - [API Reference](https://iklobato.github.io/lightapi/api-reference/core/)
167
+ - [Examples](https://iklobato.github.io/lightapi/examples/basic-rest/)
168
+
169
+ ---
170
+
171
+ ## FAQ
172
+
173
+ **Q: Can I use LightAPI with my existing database?**
174
+ A: Yes! Use the YAML config to reflect your schema and instantly expose REST endpoints.
175
+
176
+ **Q: What databases are supported?**
177
+ A: Any database supported by SQLAlchemy (PostgreSQL, MySQL, SQLite, etc.).
178
+
179
+ **Q: How do I secure my API?**
180
+ A: Enable JWT authentication and CORS with a single line.
181
+
182
+ **Q: Can I customize endpoints or add business logic?**
183
+ A: Yes, you can extend or override any handler, add middleware, and use validators.
184
+
185
+ **Q: Is this production-ready?**
186
+ A: Yes. LightAPI is designed for both rapid prototyping and production deployment.
187
+
188
+ ---
189
+
190
+ ## Comparison
191
+
192
+ | Feature | LightAPI | FastAPI | Flask | Django REST |
193
+ |------------------------|----------|--------|-------|-------------|
194
+ | Zero-boilerplate CRUD | ✅ | ❌ | ❌ | ❌ |
195
+ | YAML-driven API | ✅ | ❌ | ❌ | ❌ |
196
+ | Async support | ✅ | ✅ | ❌ | ❌ |
197
+ | OpenAPI docs | ✅ | ✅ | ❌ | ✅ |
198
+ | Built-in Auth/Caching | ✅ | ❌ | ❌ | ✅ |
199
+ | DB Reflection | ✅ | ❌ | ❌ | ❌ |
200
+
201
+ ---
202
+
203
+ ## License
204
+
205
+ MIT License. See [LICENSE](LICENSE).
206
+
207
+ ---
208
+
209
+ > **Note:** Only GET, POST, PUT, PATCH, DELETE HTTP verbs are supported. Required fields must be NOT NULL in the schema. Constraint violations (NOT NULL, UNIQUE, FK) return 409.
210
+ > To start your API, always use `api.run(host, port)`. Do not use external libraries or `app = api.app` to start the server directly.
211
+
212
+ ---
213
+
214
+ **LightAPI** - *The fastest way to build Python REST APIs from your database.*
@@ -0,0 +1,162 @@
1
+ # LightAPI: Instant Python REST APIs from SQL Databases
2
+
3
+ [![PyPI version](https://badge.fury.io/py/lightapi.svg)](https://pypi.org/project/lightapi/)
4
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ **LightAPI** is a modern Python framework for building high-performance REST APIs directly from your SQL database—no boilerplate, no manual endpoint wiring. Instantly expose CRUD endpoints from SQLAlchemy models or a YAML config, with full support for authentication, caching, validation, filtering, and OpenAPI documentation.
8
+
9
+ ---
10
+
11
+ ## Why use LightAPI?
12
+
13
+ - **Zero-boilerplate Python REST API**: Instantly generate CRUD endpoints from your database schema.
14
+ - **YAML-driven API generator**: Reflect your database and expose only the tables and operations you want.
15
+ - **Async and Fast**: Built on aiohttp for high concurrency and low latency.
16
+ - **Production-ready**: JWT authentication, Redis caching, request validation, and robust error handling.
17
+ - **Automatic OpenAPI docs**: Swagger UI and OpenAPI JSON out of the box.
18
+ - **Flexible**: Use with SQLite, PostgreSQL, MySQL, or any SQLAlchemy-supported database.
19
+ - **Modern Python**: Type hints, async/await, and best practices throughout.
20
+
21
+ ---
22
+
23
+ ## Who is this for?
24
+
25
+ - **Backend developers** who want to ship APIs fast, with minimal code.
26
+ - **Data engineers** needing to expose existing databases as RESTful services.
27
+ - **Prototypers** and **startups** who want to iterate quickly and scale later.
28
+ - **Anyone** who wants a clean, maintainable, and extensible Python API stack.
29
+
30
+ ---
31
+
32
+ ## Features
33
+
34
+ - 🚀 **Automatic CRUD endpoints** from SQLAlchemy models or YAML config
35
+ - 🔄 **Database reflection**: Expose existing tables instantly
36
+ - 🔐 **JWT authentication** with CORS support
37
+ - ⚡ **Async performance** (aiohttp)
38
+ - 💾 **Redis caching** with auto-invalidation
39
+ - 🧪 **Request validation** and error handling
40
+ - 🔍 **Filtering, pagination, and sorting**
41
+ - 📖 **OpenAPI/Swagger documentation** auto-generated
42
+ - 🔧 **Custom middleware** support
43
+ - 🗄️ **Works with SQLite, PostgreSQL, MySQL, and more**
44
+ - 📝 **Environment-based configuration**
45
+
46
+ ---
47
+
48
+ ## Quick Start
49
+
50
+ ### 1. Install LightAPI
51
+
52
+ ```bash
53
+ pip install lightapi
54
+ ```
55
+
56
+ ### 2. Define your model (SQLAlchemy)
57
+
58
+ ```python
59
+ from lightapi import LightApi
60
+ from lightapi.database import Base
61
+ from sqlalchemy import Column, Integer, String
62
+
63
+ class User(Base):
64
+ __tablename__ = "users"
65
+ id = Column(Integer, primary_key=True)
66
+ name = Column(String(50))
67
+ email = Column(String(100))
68
+
69
+ app = LightApi()
70
+ app.register(User)
71
+
72
+ if __name__ == "__main__":
73
+ app.run()
74
+ ```
75
+
76
+ ### 3. Or use YAML for instant API from your database
77
+
78
+ ```yaml
79
+ # config.yaml
80
+ database_url: sqlite:///mydata.db
81
+ tables:
82
+ - name: users
83
+ crud: [get, post, put, patch, delete]
84
+ - name: orders
85
+ crud: [get, post]
86
+ ```
87
+
88
+ ```python
89
+ from lightapi import LightApi
90
+ api = LightApi.from_config('config.yaml')
91
+ api.run(host="0.0.0.0", port=8081)
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Example Endpoints (from YAML above)
97
+
98
+ - `GET /users/` - List users
99
+ - `POST /users/` - Create user
100
+ - `GET /users/{id}` - Get user by ID
101
+ - `PUT /users/{id}` - Replace user
102
+ - `PATCH /users/{id}` - Update user
103
+ - `DELETE /users/{id}` - Delete user
104
+ - `GET /orders/` - List orders
105
+ - `POST /orders/` - Create order
106
+ - `GET /orders/{id}` - Get order by ID
107
+
108
+ ---
109
+
110
+ ## Documentation
111
+
112
+ - [Full Documentation](https://iklobato.github.io/lightapi/)
113
+ - [Getting Started](https://iklobato.github.io/lightapi/getting-started/installation/)
114
+ - [API Reference](https://iklobato.github.io/lightapi/api-reference/core/)
115
+ - [Examples](https://iklobato.github.io/lightapi/examples/basic-rest/)
116
+
117
+ ---
118
+
119
+ ## FAQ
120
+
121
+ **Q: Can I use LightAPI with my existing database?**
122
+ A: Yes! Use the YAML config to reflect your schema and instantly expose REST endpoints.
123
+
124
+ **Q: What databases are supported?**
125
+ A: Any database supported by SQLAlchemy (PostgreSQL, MySQL, SQLite, etc.).
126
+
127
+ **Q: How do I secure my API?**
128
+ A: Enable JWT authentication and CORS with a single line.
129
+
130
+ **Q: Can I customize endpoints or add business logic?**
131
+ A: Yes, you can extend or override any handler, add middleware, and use validators.
132
+
133
+ **Q: Is this production-ready?**
134
+ A: Yes. LightAPI is designed for both rapid prototyping and production deployment.
135
+
136
+ ---
137
+
138
+ ## Comparison
139
+
140
+ | Feature | LightAPI | FastAPI | Flask | Django REST |
141
+ |------------------------|----------|--------|-------|-------------|
142
+ | Zero-boilerplate CRUD | ✅ | ❌ | ❌ | ❌ |
143
+ | YAML-driven API | ✅ | ❌ | ❌ | ❌ |
144
+ | Async support | ✅ | ✅ | ❌ | ❌ |
145
+ | OpenAPI docs | ✅ | ✅ | ❌ | ✅ |
146
+ | Built-in Auth/Caching | ✅ | ❌ | ❌ | ✅ |
147
+ | DB Reflection | ✅ | ❌ | ❌ | ❌ |
148
+
149
+ ---
150
+
151
+ ## License
152
+
153
+ MIT License. See [LICENSE](LICENSE).
154
+
155
+ ---
156
+
157
+ > **Note:** Only GET, POST, PUT, PATCH, DELETE HTTP verbs are supported. Required fields must be NOT NULL in the schema. Constraint violations (NOT NULL, UNIQUE, FK) return 409.
158
+ > To start your API, always use `api.run(host, port)`. Do not use external libraries or `app = api.app` to start the server directly.
159
+
160
+ ---
161
+
162
+ **LightAPI** - *The fastest way to build Python REST APIs from your database.*
@@ -732,3 +732,7 @@ app.add_middleware([SecurityHeadersMiddleware])
732
732
  ```
733
733
 
734
734
  This comprehensive authentication system provides enterprise-grade security while maintaining the simplicity and performance that LightAPI is known for. The modular design allows you to implement exactly the authentication strategy your application needs, from simple API keys to complex multi-factor authentication systems.
735
+
736
+ > **Note:** All JWT-protected endpoints require the `LIGHTAPI_JWT_SECRET` environment variable to be set before running the server.
737
+
738
+ > **Custom endpoints must specify their intended paths using `route_patterns`. See the mega example for a full-stack authentication and registration demo.**
@@ -753,4 +753,19 @@ class User(RestEndpoint):
753
753
  - All required fields must be defined as NOT NULL in your database schema for correct enforcement.
754
754
  - The API will return 409 Conflict if you attempt to create or update a record missing a NOT NULL field, or violating a UNIQUE or FOREIGN KEY constraint.
755
755
 
756
- To start your API, always use `api.run(host, port)`. Do not use external libraries or 'app = api.app' to start the server directly.
756
+ To start your API, always use `api.run(host, port)`. Do not use external libraries or 'app = api.app' to start the server directly.
757
+
758
+ ## Custom Endpoint Registration with route_patterns
759
+
760
+ When registering custom (non-model) endpoints, you must specify the intended REST path(s) using the `route_patterns` attribute. Fallback to class names is not supported for custom endpoints.
761
+
762
+ ```python
763
+ class HelloWorldEndpoint(RestEndpoint):
764
+ route_patterns = ["/hello"]
765
+ def get(self, request):
766
+ return {"message": "Hello, World!"}
767
+
768
+ app.register(HelloWorldEndpoint)
769
+ ```
770
+
771
+ > See the mega example for a comprehensive demonstration of registering multiple endpoints with custom paths.
@@ -158,52 +158,41 @@ curl -X POST http://localhost:8000/auth/login \
158
158
  }
159
159
  ```
160
160
 
161
- ### 2. Access Public Endpoint
161
+ ### 2. Access User Profile (JWT-protected)
162
162
 
163
- ```bash
164
- curl http://localhost:8000/public
165
- ```
166
-
167
- **Response:**
168
- ```json
169
- {
170
- "message": "This is public information"
171
- }
172
- ```
173
-
174
- ### 3. Access Protected Endpoint
163
+ > **Note:** The `user_id` field in the profile must match the JWT `sub` claim (e.g., `user_1`).
175
164
 
176
165
  ```bash
177
- # Without token (will fail)
178
- curl http://localhost:8000/secret
179
- ```
180
-
181
- **Response:**
182
- ```json
183
- {
184
- "error": "Authentication failed"
185
- }
186
- ```
166
+ curl -X POST http://localhost:8000/user_profiles \
167
+ -H "Content-Type: application/json" \
168
+ -H "Authorization: Bearer YOUR_TOKEN" \
169
+ -d '{
170
+ "user_id": "user_1",
171
+ "full_name": "Admin User",
172
+ "email": "admin@example.com"
173
+ }'
187
174
 
188
- ```bash
189
- # With valid token
190
175
  curl -H "Authorization: Bearer YOUR_TOKEN" \
191
- http://localhost:8000/secret
176
+ http://localhost:8000/user_profiles
192
177
  ```
193
178
 
194
179
  **Response:**
195
180
  ```json
196
181
  {
197
- "message": "Hello, admin! You have admin access.",
198
- "secret_data": "This is protected information"
182
+ "id": 2,
183
+ "user_id": "user_1",
184
+ "full_name": "Admin User",
185
+ "email": "admin@example.com"
199
186
  }
200
187
  ```
201
188
 
202
- ### 4. Access User Profile
189
+ ### 3. Troubleshooting
190
+
191
+ - If you get `{ "error": "Profile not found" }`, ensure you created the profile with `user_id` matching the JWT `sub` claim.
192
+ - Always set the environment variable before running the server:
203
193
 
204
194
  ```bash
205
- curl -H "Authorization: Bearer YOUR_TOKEN" \
206
- http://localhost:8000/profile
195
+ export LIGHTAPI_JWT_SECRET="your-super-secret-key"
207
196
  ```
208
197
 
209
198
  ## JWT Token Structure
@@ -160,3 +160,30 @@ LightAPI is released under the [MIT License](https://github.com/henriqueblobato/
160
160
 
161
161
  > To start your API, always use `api.run(host, port)`. Do not use external libraries or 'app = api.app' to start the server directly.
162
162
 
163
+ ## Mega Example: All Features in One App
164
+
165
+ The `examples/mega_example.py` script demonstrates the full capabilities of LightAPI:
166
+ - RESTful models (products, categories, orders, users, etc.)
167
+ - Custom endpoints (auth, weather, hello, secret, public, etc.)
168
+ - JWT authentication and protected resources
169
+ - Middleware (logging, CORS, rate limiting, authentication)
170
+ - Caching, filtering, pagination, and more
171
+
172
+ **Available Endpoints:**
173
+
174
+ | Path | Methods | Description |
175
+ |-----------------------|--------------------------------------------|------------------------------|
176
+ | /products | GET, POST, PUT, PATCH, DELETE, OPTIONS | Product CRUD |
177
+ | /categories | GET, POST, PUT, PATCH, DELETE, OPTIONS | Category CRUD |
178
+ | /orders | GET, POST, PUT, PATCH, DELETE, OPTIONS | Order CRUD |
179
+ | /order_items | GET, POST, PUT, PATCH, DELETE, OPTIONS | Order item CRUD |
180
+ | /users | GET, POST, PUT, PATCH, DELETE, OPTIONS | User CRUD |
181
+ | /user_profiles | GET, POST, PUT, PATCH, DELETE, OPTIONS | User profile (JWT protected) |
182
+ | /auth/login | POST, OPTIONS | JWT login |
183
+ | /secret | GET, OPTIONS (JWT required) | Protected resource |
184
+ | /public | GET, OPTIONS | Public resource |
185
+ | /weather/{city} | GET, OPTIONS | Weather info (custom path) |
186
+ | /hello | GET, OPTIONS | Hello world (custom path) |
187
+
188
+ > All endpoints are registered with explicit RESTful or custom paths using `route_patterns` or `__tablename__`.
189
+
@@ -33,6 +33,21 @@ app = LightApi()
33
33
  app.register({'/custom-users': CustomUserEndpoint})
34
34
  ```
35
35
 
36
+ ## Registering Custom Endpoints with route_patterns
37
+
38
+ When defining a custom endpoint (not a SQLAlchemy model), always specify the intended path(s) using the `route_patterns` attribute:
39
+
40
+ ```python
41
+ class HelloWorldEndpoint(RestEndpoint):
42
+ route_patterns = ["/hello"]
43
+ def get(self, request):
44
+ return {"message": "Hello, World!"}
45
+
46
+ app.register(HelloWorldEndpoint)
47
+ ```
48
+
49
+ > See the mega example for a comprehensive demonstration of this pattern.
50
+
36
51
  ## HTTP Method Configuration
37
52
 
38
53
  - `http_method_names`: List of allowed HTTP methods.
@@ -2,9 +2,9 @@
2
2
 
3
3
  This directory contains example applications demonstrating various features of the LightAPI framework.
4
4
 
5
- ## Basic Examples
5
+ ## Example Features
6
6
 
7
- - **user_goal_example.py**: Comprehensive demonstration of intended LightAPI usage patterns
7
+ - **comprehensive_ideal_usage.py**: Comprehensive demonstration of intended LightAPI usage patterns
8
8
  - Shows the exact API design philosophy and usage as envisioned
9
9
  - Demonstrates custom validators with field-specific validation methods
10
10
  - Implements JWT authentication with proper configuration
@@ -13,17 +13,17 @@ This directory contains example applications demonstrating various features of t
13
13
  - Includes CORS support and proper environment variable usage
14
14
  - **Note**: Some advanced features (caching + pagination) are commented out due to current limitations
15
15
 
16
- - **basic_rest_api.py**: A simple REST API with default CRUD operations
16
+ - **rest_crud_basic.py**: A simple REST API with default CRUD operations
17
17
  - Demonstrates minimal setup for a REST endpoint
18
18
  - Shows automatic handling of GET, POST, PUT, DELETE operations
19
19
  - Illustrates SQLAlchemy model integration with LightAPI
20
20
 
21
- - **validation_example.py**: Data validation with custom validators
21
+ - **validation_custom_fields.py**: Data validation with custom validators
22
22
  - Shows field-specific validation rules using Validator class
23
23
  - Demonstrates error handling for validation failures
24
24
  - Illustrates data transformation (price conversion between dollars and cents)
25
25
 
26
- - **auth_example.py**: JWT authentication with protected resources
26
+ - **authentication_jwt.py**: JWT authentication with protected resources
27
27
  - Implements JWT token generation and verification
28
28
  - Shows protected endpoints requiring authentication
29
29
  - Demonstrates user information extraction from token
@@ -31,45 +31,31 @@ This directory contains example applications demonstrating various features of t
31
31
 
32
32
  ## Advanced Features
33
33
 
34
- - **custom_snippet.py**: Built-in middleware demonstration
34
+ - **middleware_cors_auth.py**: Built-in middleware demonstration
35
35
  - Shows new `CORSMiddleware` and `AuthenticationMiddleware` classes
36
36
  - Demonstrates automatic CORS preflight handling with JWT authentication
37
37
  - Illustrates seamless integration of authentication with CORS support
38
38
  - Uses built-in middleware for cleaner, more maintainable code
39
39
 
40
- - **middleware_example.py**: Custom middleware implementation for request/response processing
41
- - Demonstrates request/response lifecycle hooks
42
- - Includes logging middleware with request timing
43
- - Shows custom CORS headers management for cross-origin requests
44
- - Implements rate limiting with custom window controls
45
-
46
- - **filtering_pagination_example.py**: Query filtering and result pagination
47
- - Shows parameter-based filtering for REST endpoints
48
- - Implements custom filter logic for search and range queries
49
- - Demonstrates paginated results with metadata
50
- - Illustrates dynamic sorting by different fields
51
-
52
- - **caching_example.py**: Response caching for improved performance
53
- - Shows Redis cache implementation with automatic JSON serialization
54
- - Demonstrates cache key generation strategies
55
- - Includes time-to-live (TTL) management
56
- - Shows manual cache invalidation (DELETE operations)
57
- - Implements cache hit/miss HTTP headers
58
- - Fixed JSON serialization issues for proper caching
59
-
60
- - **swagger_example.py**: Enhanced OpenAPI/Swagger documentation
61
- - Demonstrates docstring-based API documentation
62
- - Shows custom SwaggerGenerator implementation
63
- - Illustrates request/response schema documentation
64
- - Implements API grouping with tags
65
- - Demonstrates security scheme definitions
66
-
67
- - **relationships_example.py**: Complex SQLAlchemy relationships (one-to-many, many-to-many)
68
- - Shows many-to-many relationships with association tables
69
- - Demonstrates one-to-many relationships with back references
70
- - Illustrates cascade behaviors on related objects
71
- - Shows nested data serialization across relationships
72
- - Implements relationship handling in POST/PUT operations
40
+ - **filtering_pagination.py**: Filtering and pagination
41
+ - Demonstrates filtering, pagination, and sorting of results
42
+ - Shows how to use query parameters for advanced queries
43
+
44
+ - **middleware_custom.py**: Custom middleware and order
45
+ - Demonstrates how to add custom middleware
46
+ - Shows the order of middleware execution
47
+ - Includes logging, CORS, and rate limiting examples
48
+
49
+ - **relationships_sqlalchemy.py**: SQLAlchemy relationships
50
+ - Demonstrates one-to-many and many-to-many relationships
51
+ - Shows how to query related resources
52
+
53
+ - **swagger_openapi_docs.py**: Swagger/OpenAPI documentation
54
+ - Shows how to generate and customize API documentation
55
+ - Demonstrates validator docstrings for better docs
56
+
57
+ - **general_usage.py**: General usage
58
+ - Shows custom validator, custom headers, and CRUD
73
59
 
74
60
  ## New Built-in Features (Latest Updates)
75
61
 
@@ -95,17 +81,17 @@ All examples now demonstrate improved CORS handling:
95
81
  Each example is self-contained and can be run directly:
96
82
 
97
83
  ```bash
98
- # Basic example
99
- python examples/basic_rest_api.py
84
+ # Basic REST/CRUD example
85
+ python examples/rest_crud_basic.py
100
86
 
101
- # Built-in middleware example (demonstrates new features)
102
- LIGHTAPI_JWT_SECRET="your-secret-key" python examples/custom_snippet.py
87
+ # Built-in middleware, CORS, Auth example
88
+ LIGHTAPI_JWT_SECRET="your-secret-key" python examples/middleware_cors_auth.py
103
89
 
104
- # Authentication with CORS
105
- LIGHTAPI_JWT_SECRET="your-secret-key" python examples/auth_example.py
90
+ # JWT Authentication example
91
+ LIGHTAPI_JWT_SECRET="your-secret-key" python examples/authentication_jwt.py
106
92
 
107
93
  # Caching with Redis (requires Redis running)
108
- python examples/caching_example.py
94
+ python examples/caching_redis_custom.py
109
95
  ```
110
96
 
111
97
  Most examples will:
@@ -119,11 +105,11 @@ Most examples will:
119
105
  The examples now include improved CORS and authentication. Test with:
120
106
 
121
107
  ```bash
122
- # Start the custom_snippet example
123
- LIGHTAPI_JWT_SECRET="test-secret-key-123" python examples/custom_snippet.py
108
+ # Start the middleware_cors_auth example
109
+ LIGHTAPI_JWT_SECRET="test-secret-key-123" python examples/middleware_cors_auth.py
124
110
 
125
- # Start the user goal example (comprehensive demonstration)
126
- LIGHTAPI_JWT_SECRET="test-secret-key-123" python examples/user_goal_example.py
111
+ # Start the comprehensive_ideal_usage example (comprehensive demonstration)
112
+ LIGHTAPI_JWT_SECRET="test-secret-key-123" python examples/comprehensive_ideal_usage.py
127
113
 
128
114
  # Test CORS preflight (should work without authentication)
129
115
  curl -X OPTIONS http://localhost:8000/custom -v