22
33Django-based REST Framework
44
5- # Requires
5+ # Install requires
66
77``` PHP
88pip install rinzler
@@ -12,30 +12,45 @@ pip install rinzler
1212``` Python
1313
1414# urls.py
15+
16+ import os
17+ import rinzler
18+
19+ from rinzler.core.main_controller import MainController
1520from your_controller import Controller
16- from rinzler.core.url_assembler import UrlAssembler
1721
18- assembler = UrlAssembler()
22+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__ )))
23+
24+ app = rinzler.boot(BASE_DIR )
25+ assembler = app[' url_assembler' ]
1926
2027urlpatterns = [
21- assembler.mount(' ' , Controller)
28+ assembler.mount(' hello' , Controller),
29+ assembler.mount(' ' , MainController),
2230]
2331
32+ ```
2433
34+ ``` Python
2535# your_controller.py
36+ from collections import OrderedDict
2637
2738from django.http.request import HttpRequest
2839from django.views.generic import TemplateView
2940
3041from rinzler.core.response import Response
3142
43+
3244class Controller (TemplateView ):
3345
3446 def connect (self , app ):
3547
3648 router = app[' router' ]
3749
38- router.get(' /' , self .hello_world)
50+ # Definir end-points aqui
51+ router.get(' /world/' , self .hello_world)
52+ router.get(' /{name} /' , self .hello_user)
53+
3954 return app
4055
4156 # end-point callbacks here:
@@ -48,10 +63,82 @@ class Controller(TemplateView):
4863 :param params dict url params, if present
4964 :rtype: Response
5065 """
51- response = {
52- " status" : True ,
53- " data" : " Hello World!"
54- }
66+ try :
67+ response = OrderedDict()
68+ response[" status" ] = True
69+ response[" data" ] = " Hello World!"
70+
71+ return Response(response, content_type = " application/json" )
72+ except BaseException as e:
73+ response = OrderedDict()
74+ response[" status" ] = False
75+ response[" mensagem" ] = str (e)
76+
77+ return Response(response, content_type = " application/json" , status = 500 )\
78+
79+ @ staticmethod
80+ def hello_user (request : HttpRequest, app : dict (), ** params : dict ):
81+ """
82+ Default route callback
83+ :param request HttpRequest
84+ :param app Rinzler's object
85+ :param params dict url params, if present
86+ :rtype: Response
87+ """
88+ try :
89+ user = params[' name' ]
90+ response = OrderedDict()
91+ response[" status" ] = True
92+ response[" data" ] = " Hello {0} !" .format(user)
93+
94+ return Response(response, content_type = " application/json" )
95+ except BaseException as e:
96+ response = OrderedDict()
97+ response[" status" ] = False
98+ response[" mensagem" ] = str (e)
99+
100+ return Response(response, content_type = " application/json" , status = 500 )
55101
56- return Response(response, content_type = " application/json" )
57102```
103+ ### Run django
104+ ``` shell
105+ python manage.py runserver
106+ August 02, 2017 - 18:48:00
107+ Django version 1.10.4, using settings ' Demo.settings'
108+ Starting development server at http://127.0.0.1:8000/
109+ Quit the server with CONTROL-C.
110+ ```
111+
112+ ### Sample requests
113+
114+ ``` shell
115+ curl http://localhost:8000/
116+ < center><h1> HTTP/1.1 200 OK RINZLER FRAMEWORK< /h1></center>
117+
118+ curl http://localhost:8000/hello/world/
119+ {
120+ " status" : true,
121+ " data" : " Hello World!"
122+ }
123+
124+ curl http://localhost:8000/hello/bob/
125+ {
126+ " status" : true,
127+ " data" : " Hello bob!"
128+ }
129+
130+ curl http://localhost:8000/foo/bar/
131+ {
132+ " status" : false,
133+ " exceptions" : {
134+ " message" : " No route found for GET foo/bar/"
135+ },
136+ " request" : {
137+ " content" : " " ,
138+ " method" : " GET" ,
139+ " path_info" : " foo/bar/"
140+ },
141+ " message" : " We are sorry, but something went terribly wrong."
142+ }
143+
144+ ```
0 commit comments