# Rinzler REST Framework
Django-based REST Micro-Framework
# Install requires
```bash
pip install rinzler
```
# Usage
```Python
# urls.py
from rinzler import boot, Rinzler
from rinzler.core.main_controller import MainController
from your_controller import Controller
app: Rinzler = boot("MyApp")
urlpatterns = [
app.mount('hello', Controller),
app.mount('', MainController),
]
```
```Python
# your_controller.py
from django.http.request import HttpRequest
from rinzler import Rinzler
from rinzler.core.response import Response
class Controller:
def connect(self, app):
router = app.get_end_point_register()
# map end-points to callbacks here
router.get('/world/', self.hello_world)
router.get('/{name}/', self.hello_user)
return router
# end-point callbacks here:
@staticmethod
def hello_world(request: HttpRequest, app: Rinzler, **params: dict):
"""
Default route callback
:param request HttpRequest
:param app Rinzler's object
:param params dict url params, if present
:rtype: Response
"""
try:
response = {
"status": True,
"data": "Hello World!",
}
return Response(response, content_type="application/json")
except BaseException as e:
response = {
"status": False,
"mensagem": str(e),
}
return Response(response, content_type="application/json", status=500)\
@staticmethod
def hello_user(request: HttpRequest, app: Rinzler, **params: dict) -> Response:
try:
user = params['name']
response = {
"status": True,
"data": f"Hello {user}!",
}
return Response(response, content_type="application/json")
except BaseException as e:
response = {
"status": False,
"mensagem": str(e),
}
return Response(response, content_type="application/json", status=500)
```
### Run django
```shell
python manage.py runserver
August 02, 2017 - 18:48:00
Django version 1.10.4, using settings 'Demo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
```
### Sample requests
```shell
curl http://localhost:8000/
<center><h1>HTTP/1.1 200 OK RINZLER FRAMEWORK</h1></center>
curl http://localhost:8000/hello/world/
{
"status": true,
"data": "Hello World!"
}
curl http://localhost:8000/hello/bob/
{
"status": true,
"data": "Hello bob!"
}
curl http://localhost:8000/foo/bar/
{
"status": false,
"exceptions": {
"message": "No route found for GET foo/bar/"
},
"request": {
"content": "",
"method": "GET",
"path_info": "foo/bar/"
},
"message": "We are sorry, but something went terribly wrong."
}
# If project's settings has DEBUG=True, otherwise: empty response body with status-code 404
```
### Authentication
Authentication can be done on a per-route basis thus allowing to have authenticated and non-authenticated routes on the application.
To do so, first you need to create a class that inherits from `BaseAuthService` and implements the `authenticate` method.
```Python
from django.http.request import HttpRequest
from rinzler.auth.base_auth_service import BaseAuthService
class MyAuthenticationService(BaseAuthService):
def authenticate(self, request: HttpRequest, auth_route: str, params: dict) -> bool:
"""
Implement your authentication logic here
:param request: HttpRequest Django's request object
:param auth_route: str route being requested
:param params: dict url params, if present
:return: bool if not True, the request will be promptly returned with status-code 403
"""
# Non-authenticated route
if auth_route == 'GET_v1/hello/world/':
return True
# Authenticated routes
if request.META.get("HTTP_AUTHORIZATION"):
# after performing your authentication logic, you can append user data to the Rinzler object so it'll be available on the controller
self.auth_data = {
"user_id": 1,
}
return True
```
Then, you need to register your authentication service on the application's `urls.py` file.
```Python
# urls.py
from rinzler import boot, Rinzler
from rinzler.core.main_controller import MainController
from your_controller import Controller
from my_auth_service import MyAuthenticationService
app: Rinzler = boot("MyApp")
app.set_auth_service(MyAuthenticationService())
urlpatterns = [
app.mount('hello', Controller),
app.mount('', MainController),
]
```
Finally, you can access the user data on the controller by accessing the `auth_data` attribute on the Rinzler object.
```Python
# your_controller.py
from django.http.request import HttpRequest
from rinzler import Rinzler
from rinzler.core.response import Response
class Controller:
# ...
@staticmethod
def hello_user(request: HttpRequest, app: Rinzler, **params: dict) -> Response:
try:
user = params['name']
user_id = app.auth_data['user_id']
response = {
"status": True,
"data": f"Hello {user}! Your user id is {user_id}.",
}
return Response(response, content_type="application/json")
except BaseException as e:
response = {
"status": False,
"mensagem": str(e),
}
return Response(response, content_type="application/json", status=500)
```
### Sample requests
```shell
# Non-authenticated request
curl http://localhost:8000/hello/world/
{
"status": true,
"data": "Hello World!"
}
# Improperly authenticated request
curl http://localhost:8000/hello/bob/
# (empty response body with status-code 403)
# Properly authenticated request
curl http://localhost:8000/hello/bob/ -H "Authorization: XYZ"
{
"status": true,
"data": "Hello bob! Your user id is 1"
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/feliphebueno/Rinzler",
"name": "rinzler",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "rest, api, framework, django",
"author": "Rinzler",
"author_email": "feliphezion@gmail.com",
"download_url": null,
"platform": null,
"description": "# Rinzler REST Framework\n\nDjango-based REST Micro-Framework\n\n# Install requires\n\n```bash\npip install rinzler\n```\n\n# Usage\n```Python\n# urls.py\nfrom rinzler import boot, Rinzler\n\nfrom rinzler.core.main_controller import MainController\nfrom your_controller import Controller\n\n\napp: Rinzler = boot(\"MyApp\")\n\nurlpatterns = [\n app.mount('hello', Controller),\n app.mount('', MainController),\n]\n```\n\n```Python\n# your_controller.py\nfrom django.http.request import HttpRequest\n\nfrom rinzler import Rinzler\nfrom rinzler.core.response import Response\n\n\nclass Controller:\n\n def connect(self, app):\n\n router = app.get_end_point_register()\n\n # map end-points to callbacks here\n router.get('/world/', self.hello_world)\n router.get('/{name}/', self.hello_user)\n\n return router\n\n # end-point callbacks here:\n @staticmethod\n def hello_world(request: HttpRequest, app: Rinzler, **params: dict):\n \"\"\"\n Default route callback\n :param request HttpRequest\n :param app Rinzler's object\n :param params dict url params, if present\n :rtype: Response\n \"\"\"\n try:\n response = {\n \"status\": True,\n \"data\": \"Hello World!\",\n }\n return Response(response, content_type=\"application/json\")\n except BaseException as e:\n response = {\n \"status\": False,\n \"mensagem\": str(e),\n }\n return Response(response, content_type=\"application/json\", status=500)\\\n\n @staticmethod\n def hello_user(request: HttpRequest, app: Rinzler, **params: dict) -> Response:\n try:\n user = params['name']\n response = {\n \"status\": True,\n \"data\": f\"Hello {user}!\",\n }\n\n return Response(response, content_type=\"application/json\")\n except BaseException as e:\n response = {\n \"status\": False,\n \"mensagem\": str(e),\n }\n return Response(response, content_type=\"application/json\", status=500)\n```\n### Run django\n```shell\npython manage.py runserver\nAugust 02, 2017 - 18:48:00\nDjango version 1.10.4, using settings 'Demo.settings'\nStarting development server at http://127.0.0.1:8000/\nQuit the server with CONTROL-C.\n```\n\n### Sample requests\n\n```shell\ncurl http://localhost:8000/\n<center><h1>HTTP/1.1 200 OK RINZLER FRAMEWORK</h1></center>\n\ncurl http://localhost:8000/hello/world/\n{\n \"status\": true,\n \"data\": \"Hello World!\"\n}\n\ncurl http://localhost:8000/hello/bob/\n{\n \"status\": true,\n \"data\": \"Hello bob!\"\n}\n\ncurl http://localhost:8000/foo/bar/\n{\n \"status\": false,\n \"exceptions\": {\n \"message\": \"No route found for GET foo/bar/\"\n },\n \"request\": {\n \"content\": \"\",\n \"method\": \"GET\",\n \"path_info\": \"foo/bar/\"\n },\n \"message\": \"We are sorry, but something went terribly wrong.\"\n}\n# If project's settings has DEBUG=True, otherwise: empty response body with status-code 404\n```\n\n\n### Authentication\nAuthentication can be done on a per-route basis thus allowing to have authenticated and non-authenticated routes on the application.\n\nTo do so, first you need to create a class that inherits from `BaseAuthService` and implements the `authenticate` method.\n\n```Python\nfrom django.http.request import HttpRequest\n\nfrom rinzler.auth.base_auth_service import BaseAuthService\n\nclass MyAuthenticationService(BaseAuthService):\n def authenticate(self, request: HttpRequest, auth_route: str, params: dict) -> bool:\n \"\"\"\n Implement your authentication logic here\n :param request: HttpRequest Django's request object\n :param auth_route: str route being requested\n :param params: dict url params, if present\n :return: bool if not True, the request will be promptly returned with status-code 403\n \"\"\"\n # Non-authenticated route\n if auth_route == 'GET_v1/hello/world/':\n return True\n\n # Authenticated routes\n if request.META.get(\"HTTP_AUTHORIZATION\"):\n # after performing your authentication logic, you can append user data to the Rinzler object so it'll be available on the controller\n self.auth_data = {\n \"user_id\": 1,\n }\n return True\n```\n\nThen, you need to register your authentication service on the application's `urls.py` file.\n\n```Python\n# urls.py\nfrom rinzler import boot, Rinzler\n\nfrom rinzler.core.main_controller import MainController\nfrom your_controller import Controller\nfrom my_auth_service import MyAuthenticationService\n\n\napp: Rinzler = boot(\"MyApp\")\napp.set_auth_service(MyAuthenticationService())\n\nurlpatterns = [\n app.mount('hello', Controller),\n app.mount('', MainController),\n]\n```\n \nFinally, you can access the user data on the controller by accessing the `auth_data` attribute on the Rinzler object.\n```Python\n# your_controller.py\nfrom django.http.request import HttpRequest\n\nfrom rinzler import Rinzler\nfrom rinzler.core.response import Response\n\n\nclass Controller:\n # ...\n @staticmethod\n def hello_user(request: HttpRequest, app: Rinzler, **params: dict) -> Response:\n try:\n user = params['name']\n user_id = app.auth_data['user_id']\n response = {\n \"status\": True,\n \"data\": f\"Hello {user}! Your user id is {user_id}.\",\n }\n\n return Response(response, content_type=\"application/json\")\n except BaseException as e:\n response = {\n \"status\": False,\n \"mensagem\": str(e),\n }\n return Response(response, content_type=\"application/json\", status=500)\n```\n### Sample requests\n\n```shell\n# Non-authenticated request\ncurl http://localhost:8000/hello/world/\n{\n \"status\": true,\n \"data\": \"Hello World!\"\n}\n\n# Improperly authenticated request\ncurl http://localhost:8000/hello/bob/\n# (empty response body with status-code 403)\n\n# Properly authenticated request\ncurl http://localhost:8000/hello/bob/ -H \"Authorization: XYZ\"\n{\n \"status\": true,\n \"data\": \"Hello bob! Your user id is 1\"\n}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django-based REST API Framework",
"version": "3.0.5",
"project_urls": {
"Homepage": "https://github.com/feliphebueno/Rinzler"
},
"split_keywords": [
"rest",
" api",
" framework",
" django"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9bf85de72dd222f332f0314cbf72e41f157ff0af4adc4e41fd5622128988976f",
"md5": "d4b26332f53fef57992ebb5c0dd28309",
"sha256": "b12578d2fce31c2bd22e8844f4847b6a35efababffcc67fa718f16163ebc014d"
},
"downloads": -1,
"filename": "rinzler-3.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d4b26332f53fef57992ebb5c0dd28309",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 20068,
"upload_time": "2024-09-24T01:07:33",
"upload_time_iso_8601": "2024-09-24T01:07:33.794184Z",
"url": "https://files.pythonhosted.org/packages/9b/f8/5de72dd222f332f0314cbf72e41f157ff0af4adc4e41fd5622128988976f/rinzler-3.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-24 01:07:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "feliphebueno",
"github_project": "Rinzler",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [],
"tox": true,
"lcname": "rinzler"
}