def prepare(self): """Override the prepare RequestHandler method. Check if the Client send correct token and if the token need refresh. Support the Authorization Header check or the GET access_token. if the Client don't send a Token or the token is incorrect this method flush a 403 HTTP Error.""" token = None #frst we check if the authentication token is send by the Autorization header if 'Authorization' in self.request.headers: auth_header = self.request.headers['Authorization'] #the authentication token is send like Bearer <token> #we need to split the string and obtain the token header_parts = auth_header.split(' ') token = header_parts[1] elif self.get_argument('access_token'): token = self.get_argument('access_token') else: raise tornado.web.HTTPError(403,"not authorized, no token send") #check if the token is correct try: grant = Grant() results = grant.get(token=token) except ObjectDoesNotExist, e: raise tornado.web.HTTPError(403,"invalid authorization token incorrect")
self.set_status(204) self.write_result({},"call") def post(self): """Method POST to add a new call on ongoing status.""" try: call_app = Call() call_app.check_allowed_calls(self._grant_token,MAXIMUM_ONGOING_CALL) except RestMaxCallError, e: raise tornado.web.HTTPError(409,"maximum ongoin calls reached") number = self.get_argument("number") grant_app = Grant() grant_id = grant_app.get(token=self._grant_token) call_start = Call() token_call = call_start.start_call(grant_id['id'],number) self.set_status(201) self.set_header("Location",DOMAIN_REST+"/call?token_call="+token_call) self.write("") def put(self): """PUT method to udpate the status of the call, in this case is stop the call and actualize the end time and the duration""" token_call = self.get_argument("token_call") call_stop = Call() returned_call = call_stop.get(token=token_call) if returned_call['duration'] is None: call_stop.update(token_call) self.set_status(200) self.set_header("Location",DOMAIN_REST+"/call?token_call="+token_call)