def get(self): # validate the request parameters devStoreKey = validateRequest(self.request,utils.GETROUTES) if devStoreKey is None: logging.debug("unable to validate the request parameters") self.response.headers['Content-Type'] = 'application/javascript' self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Illegal request parameters'))) return logging.debug('getroutes request... ') if self.request.get('force') is not '': refresh = True else: refresh = False if utils.afterHours() is True: # don't run these jobs during "off" hours json_response = utils.buildErrorResponse('-1','The Metro service is not currently running') else: if refresh is True: json_response = getRoutes(refresh) # drop it into the memcache again memcache.set(utils.GETROUTES, json_response) logging.debug('---> storing in memcache'); else: logging.debug('---> memcache hit'); json_response = memcache.get(utils.GETROUTES) if json_response is None: json_response = getRoutes(refresh) # drop it into the memcache again memcache.set(utils.GETROUTES, json_response) logging.debug('---> storing in memcache'); # record the API call for this devkey utils.recordDeveloperRequest(devStoreKey,utils.GETROUTES,self.request.query_string,self.request.remote_addr); #logging.debug('API: json response %s' % json_response); callback = self.request.get('callback') if callback is not '': self.response.headers['Content-Type'] = 'application/javascript' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.headers['Access-Control-Allow-Methods'] = 'GET' response = callback + '(' + simplejson.dumps(json_response) + ');' else: self.response.headers['Content-Type'] = 'application/json' response = json_response self.response.out.write(response)
def get(self): if utils.afterHours() is True: # don't run these jobs during "off" hours json_response = utils.buildErrorResponse('-1','The Metro service is not currently running') # validate the request parameters devStoreKey = validateRequest(self.request) if devStoreKey is None: logging.error("failed to validate the request paramters") self.response.headers['Content-Type'] = 'application/javascript' self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Unable to validate the request. There may be an illegal developer key.'))) return # snare the inputs stopID = utils.conformStopID(self.request.get('stopID')) routeID = self.request.get('routeID') vehicleID = self.request.get('vehicleID') logging.debug('getarrivals request parameters... stopID %s routeID %s vehicleID %s' % (stopID,routeID,vehicleID)) if stopID is not '' and routeID is '': json_response = stopRequest(stopID, devStoreKey) utils.recordDeveloperRequest(devStoreKey,utils.GETARRIVALS,self.request.query_string,self.request.remote_addr); elif stopID is not '' and routeID is not '': json_response = stopRouteRequest(stopID, routeID, devStoreKey) utils.recordDeveloperRequest(devStoreKey,utils.GETARRIVALS,self.request.query_string,self.request.remote_addr); elif routeID is not '' and vehicleID is not '': json_response = routeVehicleRequest(routeID, vehicleID, devStoreKey) utils.recordDeveloperRequest(devStoreKey,utils.GETVEHICLE,self.request.query_string,self.request.remote_addr); else: logging.debug("API: invalid request") utils.recordDeveloperRequest(devStoreKey,utils.GETARRIVALS,self.request.query_string,self.request.remote_addr,'illegal query string combination'); json_response = utils.buildErrorResponse('-1','Invalid Request parameters') # encapsulate response in json or jsonp logging.debug('API: json response %s' % json_response); callback = self.request.get('callback') if callback is not '': self.response.headers['Content-Type'] = 'application/javascript' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.headers['Access-Control-Allow-Methods'] = 'GET' response = callback + '(' + simplejson.dumps(json_response) + ');' else: self.response.headers['Content-Type'] = 'application/json' response = simplejson.dumps(json_response) self.response.out.write(response)
def get(self): # validate the request parameters devStoreKey = validateRequest(self.request,utils.GETSTOPS) if devStoreKey is None: logging.debug("unable to validate the request parameters") self.response.headers['Content-Type'] = 'application/javascript' self.response.out.write(simplejson.dumps(utils.buildErrorResponse('-1','Illegal request parameters'))) return # snare the inputs stopID = utils.conformStopID(self.request.get('stopID')) routeID = self.request.get('routeID') destination = self.request.get('destination').upper() logging.debug('getstops request parameters... routeID %s destination %s' % (routeID,destination)) if utils.afterHours() is True: # don't run these jobs during "off" hours json_response = utils.buildErrorResponse('-1','The Metro service is not currently running') elif routeID is not '' and destination is '': json_response = routeRequest(routeID, None) utils.recordDeveloperRequest(devStoreKey,utils.GETSTOPS,self.request.query_string,self.request.remote_addr); elif routeID is not '' and destination is not '': json_response = routeRequest(routeID, destination) utils.recordDeveloperRequest(devStoreKey,utils.GETSTOPS,self.request.query_string,self.request.remote_addr); else: logging.error("API: invalid request") json_response = utils.buildErrorResponse('-1','Invalid Request parameters. Did you forget to include a routeID?') utils.recordDeveloperRequest(devStoreKey,utils.GETSTOPS,self.request.query_string,self.request.remote_addr,'illegal query string combination'); #logging.debug('API: json response %s' % json_response); # encapsulate response in json callback = self.request.get('callback') if callback is not '': self.response.headers['Content-Type'] = 'application/javascript' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.headers['Access-Control-Allow-Methods'] = 'GET' response = callback + '(' + simplejson.dumps(json_response) + ');' else: self.response.headers['Content-Type'] = 'application/json' response = simplejson.dumps(json_response) self.response.out.write(response)