The code for the Nova API is being looked at, and a test program is written in the rest implementation logic.
The Nova API certainly supports more features such as Xml/json serialization, authentication, and so on. This test program only implements the rest invocation logic in the
To be easy to read, I removed the class inheritance logic.
Myrouter: Responsible for the HTTP request, according to the matching rules, distributed to different application
1) [Python Deploy] receives HTTP request, first call Myrouter's __call__ method for processing
2) [Myrouter] According to the internal matching definition, distributed through the Routemiddleware to the corresponding MyApplication
MyApplication: After receiving the HTTP request, parse the parameter information in it, call the corresponding method of the Mycontroller business class in the background
Mycontroller: The final business class that accepts parameters and performs operations on the business logic.
From __future__ import print_functionfrom routes import Mapperimport webob.decimport Webob.excimport Routes.middlewareimport testtoolsclass Mycontroller (object): Def getlist (self, MyKey): Print ("Step 4:mycontroll Er ' s getlist (self, MyKey) is invoked ") return" GetList (), mykey= "+ Mykeyclass MyApplication (object):" "" Test AP Plication to call from router. "" " def __init__ (self, controller): Self._controller = Controller def __call__ (self, environ, start_response ): Print ("Step 3:myapplication is invoked") Action_args = environ[' Wsgiorg.routing_args '][1].copy () Try:del action_args[' controller '] except Keyerror:pass Try:del a ction_args[' format '] except Keyerror:pass action = action_args.pop (' action ', None) Controller_method = GetAttr (Self._controller, action) result = Controller_method (**action_args) St Art_respOnse (' OK ', [(' Content-type ', ' Text/plain ')] return [Result]class myrouter (object): "" "Test router." "" def __init__ (self): Route_name = "Dummy_route" Route_path = "/dummies" my_application = MyApp Lication (Mycontroller ()) Self.mapper = Mapper () self.mapper.connect (Route_name, Route_path, Controller=my_application, action= "GetList", mykey= "MyValue" , conditions={"method": [' GET ']}) Self._router = Routes.middleware.RoutesMid Dleware (Self._dispatch, Self.mapper) @webob. Dec.wsgify (Request Class=webob. Request) def __call__ (self, req): "" "Route the incoming request to a controller based on Self.map. If no match, return a 404. "" "Print (" Step 1:myrouter is invoked ") return Self._router @staticmethod @webob. dec.wsGify (Requestclass=webob. Request) def _dispatch (req): "" "dispatch the request to the appropriate controller. Called by self._router through matching the incoming request to a route and putting the information into Req.environ. Either returns 404 or the routed WSGI app ' s response. "" "Print (" step 2:routesmiddleware is invoked, calling we _dispatch back ") match_dict = Req.enviro n[' Wsgiorg.routing_args '][1] if not Match_dict:return webob.exc.HTTPNotFound () app = match_dict[ ' Controller '] return app class Routingtestcase (TestTools. TestCase): def test_router (self): router = myrouter () result = Webob. Request.blank ('/dummies '). Get_response (Router) self.assertequal (Result.body, "getlist (), Mykey=myvalue")
From the output, you can see that the order of the call is: Myrouter---routes.middleware.RoutesMiddleware---MyApplication--Mycontroller:
[Test case] Test_routerstep 1:myrouter is Invokedstep 2:routesmiddleware are invoked, calling our _dispatch backstep 3:m Yapplication is Invokedstep 4:mycontroller ' s getlist (self, mykey) is invoked
Simple class Nova REST API Implementation Program