Django-based REST API Framework
Project description
# Rinzler REST Framework
Django-based REST Micro-Framework
# Install requires
```PHP
pip install rinzler
```
# Usage
```Python
# urls.py
import os
import rinzler
from rinzler.core.main_controller import MainController
from your_controller import Controller
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
app = rinzler.boot(BASE_DIR)
assembler = app['url_assembler']
urlpatterns = [
assembler.mount('hello', Controller),
assembler.mount('', MainController),
]
```
```Python
# your_controller.py
from collections import OrderedDict
from django.http.request import HttpRequest
from django.views.generic import TemplateView
from rinzler.core.response import Response
class Controller(TemplateView):
def connect(self, app):
router = app['router']
# map end-points to callbacks here
router.get('/world/', self.hello_world)
router.get('/{name}/', self.hello_user)
return app
# end-point callbacks here:
@staticmethod
def hello_world(request: HttpRequest, app: dict(), **params: dict):
"""
Default route callback
:param request HttpRequest
:param app Rinzler's object
:param params dict url params, if present
:rtype: Response
"""
try:
response = OrderedDict()
response["status"] = True
response["data"] = "Hello World!"
return Response(response, content_type="application/json")
except BaseException as e:
response = OrderedDict()
response["status"] = False
response["mensagem"] = str(e)
return Response(response, content_type="application/json", status=500)\
@staticmethod
def hello_user(request: HttpRequest, app: dict(), **params: dict):
"""
Default route callback
:param request HttpRequest
:param app Rinzler's object
:param params dict url params, if present
:rtype: Response
"""
try:
user = params['name']
response = OrderedDict()
response["status"] = True
response["data"] = "Hello {0}!".format(user)
return Response(response, content_type="application/json")
except BaseException as e:
response = OrderedDict()
response["status"] = False
response["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."
}
```
Django-based REST Micro-Framework
# Install requires
```PHP
pip install rinzler
```
# Usage
```Python
# urls.py
import os
import rinzler
from rinzler.core.main_controller import MainController
from your_controller import Controller
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
app = rinzler.boot(BASE_DIR)
assembler = app['url_assembler']
urlpatterns = [
assembler.mount('hello', Controller),
assembler.mount('', MainController),
]
```
```Python
# your_controller.py
from collections import OrderedDict
from django.http.request import HttpRequest
from django.views.generic import TemplateView
from rinzler.core.response import Response
class Controller(TemplateView):
def connect(self, app):
router = app['router']
# map end-points to callbacks here
router.get('/world/', self.hello_world)
router.get('/{name}/', self.hello_user)
return app
# end-point callbacks here:
@staticmethod
def hello_world(request: HttpRequest, app: dict(), **params: dict):
"""
Default route callback
:param request HttpRequest
:param app Rinzler's object
:param params dict url params, if present
:rtype: Response
"""
try:
response = OrderedDict()
response["status"] = True
response["data"] = "Hello World!"
return Response(response, content_type="application/json")
except BaseException as e:
response = OrderedDict()
response["status"] = False
response["mensagem"] = str(e)
return Response(response, content_type="application/json", status=500)\
@staticmethod
def hello_user(request: HttpRequest, app: dict(), **params: dict):
"""
Default route callback
:param request HttpRequest
:param app Rinzler's object
:param params dict url params, if present
:rtype: Response
"""
try:
user = params['name']
response = OrderedDict()
response["status"] = True
response["data"] = "Hello {0}!".format(user)
return Response(response, content_type="application/json")
except BaseException as e:
response = OrderedDict()
response["status"] = False
response["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."
}
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
rinzler-2.0.0-py3.6.egg
(59.9 kB
view hashes)
Close
Hashes for rinzler-2.0.0.macosx-10.6-intel.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bc33ca7b7ee7f5e0ee7888769545c1bdcf3f64b31ef486edcc2543a9d08c239 |
|
MD5 | 1c85060121f50af17d354faccbcf6b1b |
|
BLAKE2b-256 | 11401a7287ce3534288588968fee095cb3ee2387f9bb633a558242f9c7e2b840 |