Package: rinzler
Rinzler REST FrameworkDjango-based REST Micro-FrameworkInstall requirespipinstallrinzlerUsage# urls.pyfromrinzlerimportboot,Rinzlerfromrinzler.core.main_controllerimportMainControllerfromyour_controllerimportControllerapp:Rinzler=boot("MyApp")urlpatterns=[app.mount('hello',Controller),app.mount('',MainController),]# your_controller.pyfromdjango.http.requestimportHttpRequestfromrinzlerimportRinzlerfromrinzler.core.responseimportResponseclassController:defconnect(self,app):router=app.get_end_point_register()# map end-points to callbacks hererouter.get('/world/',self.hello_world)router.get('/{name}/',self.hello_user)returnrouter# end-point callbacks here:@staticmethoddefhello_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!",}returnResponse(response,content_type="application/json")exceptBaseExceptionase:response={"status":False,"mensagem":str(e),}returnResponse(response,content_type="application/json",status=500)\@staticmethoddefhello_user(request:HttpRequest,app:Rinzler,**params:dict)->Response:try:user=params['name']response={"status":True,"data":f"Hello{user}!",}returnResponse(response,content_type="application/json")exceptBaseExceptionase:response={"status":False,"mensagem":str(e),}returnResponse(response,content_type="application/json",status=500)Run djangopythonmanage.pyrunserver
August02,2017-18:48:00
Djangoversion1.10.4,usingsettings'Demo.settings'Startingdevelopmentserverathttp://127.0.0.1:8000/
QuittheserverwithCONTROL-C.Sample requestscurlhttp://localhost:8000/
<center><h1>HTTP/1.1200OKRINZLERFRAMEWORK</h1></center>

curlhttp://localhost:8000/hello/world/{"status":true,"data":"Hello World!"}curlhttp://localhost:8000/hello/bob/{"status":true,"data":"Hello bob!"}curlhttp://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 404AuthenticationAuthentication 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 fromBaseAuthServiceand implements theauthenticatemethod.fromdjango.http.requestimportHttpRequestfromrinzler.auth.base_auth_serviceimportBaseAuthServiceclassMyAuthenticationService(BaseAuthService):defauthenticate(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 routeifauth_route=='GET_v1/hello/world/':returnTrue# Authenticated routesifrequest.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 controllerself.auth_data={"user_id":1,}returnTrueThen, you need to register your authentication service on the application'surls.pyfile.# urls.pyfromrinzlerimportboot,Rinzlerfromrinzler.core.main_controllerimportMainControllerfromyour_controllerimportControllerfrommy_auth_serviceimportMyAuthenticationServiceapp: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 theauth_dataattribute on the Rinzler object.# your_controller.pyfromdjango.http.requestimportHttpRequestfromrinzlerimportRinzlerfromrinzler.core.responseimportResponseclassController:# ...@staticmethoddefhello_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}.",}returnResponse(response,content_type="application/json")exceptBaseExceptionase:response={"status":False,"mensagem":str(e),}returnResponse(response,content_type="application/json",status=500)Sample requests# Non-authenticated requestcurlhttp://localhost:8000/hello/world/{"status":true,"data":"Hello World!"}# Improperly authenticated requestcurlhttp://localhost:8000/hello/bob/# (empty response body with status-code 403)# Properly authenticated requestcurlhttp://localhost:8000/hello/bob/-H"Authorization: XYZ"{"status":true,"data":"Hello bob! Your user id is 1"}