Exemplo n.º 1
0
    def get(self):

        # validate the request parameters
        devStoreKey = validateRequest(self.request, api_utils.GETSTOPLOCATION)
        if devStoreKey is None:
            logging.debug("unable to validate the request parameters")
            self.response.headers['Content-Type'] = 'application/javascript'
            self.response.out.write(
                json.dumps(
                    api_utils.buildErrorResponse(
                        '-1', 'Illegal request parameters')))
            return

        # snare the inputs
        stopID = api_utils.conformStopID(self.request.get('stopID'))
        logging.debug('getstoplocation request parameters...  stopID %s' %
                      stopID)

        if api_utils.afterHours() is True:
            # don't run these jobs during "off" hours
            json_response = api_utils.buildErrorResponse(
                '-1', 'The Metro service is not currently running')
        elif stopID is not '':
            json_response = stopLocationRequest(stopID)
            api_utils.recordDeveloperRequest(devStoreKey, api_utils.GETSTOPS,
                                             self.request.query_string,
                                             self.request.remote_addr)
        else:
            logging.error("API: invalid request")
            json_response = api_utils.buildErrorResponse(
                '-1',
                'Invalid Request parameters. Did you forget to include a stpID?'
            )
            api_utils.recordDeveloperRequest(
                devStoreKey, api_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 + '(' + json.dumps(json_response) + ');'
        else:
            self.response.headers['Content-Type'] = 'application/json'
            response = json.dumps(json_response)

        self.response.out.write(response)
        stathat.apiStatCount()
        # push event out to anyone watching the live board
        task = Task(url='/map/task', params={'stopID': stopID})
        task.add('eventlogger')
Exemplo n.º 2
0
def validateRequest(request, type):

    # validate the key
    devStoreKey = api_utils.validateDevKey(request.get('key'))
    if devStoreKey is None:
        logging.debug('... illegal developer key %s' % request.get('key'))
        api_utils.recordDeveloperRequest(None, api_utils.GETSTOPS,
                                         request.query_string,
                                         request.remote_addr,
                                         'illegal developer key specified')
        return None

    if type == api_utils.GETSTOPS:
        routeID = request.get('routeID')
        destination = request.get('destination').upper()

        # a routeID is required
        if routeID is None or routeID is '':
            api_utils.recordDeveloperRequest(devStoreKey, type,
                                             request.query_string,
                                             request.remote_addr,
                                             'a routeID must be included')
            return None
        elif destination is not None and routeID is '':
            api_utils.recordDeveloperRequest(
                devStoreKey, type, request.query_string, request.remote_addr,
                'if a destination is specified, a routeID must be included')
            return None
    elif type == api_utils.GETSTOPLOCATION:
        stopID = api_utils.conformStopID(request.get('stopID'))
        if stopID == '' or stopID == '0' or stopID == '00':
            return None
    elif type == api_utils.GETNEARBYSTOPS:
        lat = request.get('lat')
        lon = request.get('lon')
        radius = request.get('radius')
        # lat/long is required
        if lat is None or lon is None:
            api_utils.recordDeveloperRequest(
                devStoreKey, type, request.query_string, request.remote_addr,
                'both latitude and longitude values must be specified')
            return None
        elif radius is not None and radius is not '' and radius > '5000':
            logging.error(
                'unable to validate getnearbystops call. illegal radius value of %s'
                % radius)
            api_utils.recordDeveloperRequest(devStoreKey, type,
                                             request.query_string,
                                             request.remote_addr,
                                             'radius must be less than 5,000')
            return None

    return devStoreKey
Exemplo n.º 3
0
    def get(self):

      # validate the request parameters
      devStoreKey = validateRequest(self.request,api_utils.GETSTOPS)
      if devStoreKey is None:
          if( not (self.request.get('key') == 'kiosk' and self.request.get('stopID') == '') ):
              logging.error("failed to validate the request parameters")
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.out.write(json.dumps(api_utils.buildErrorResponse('-1','Illegal request parameters')))
          return
      
      # snare the inputs
      stopID = api_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 api_utils.afterHours() is True:
          # don't run these jobs during "off" hours
	      json_response = api_utils.buildErrorResponse('-1','The Metro service is not currently running')
      elif routeID is not '' and destination is '':
          json_response = routeRequest(routeID, None)
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      elif routeID is not '' and destination is not '':
          json_response = routeRequest(routeID, destination)
          api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETSTOPS,self.request.query_string,self.request.remote_addr);
      else:
          logging.error("API: invalid request")
          json_response = api_utils.buildErrorResponse('-1','Invalid Request parameters. Did you forget to include a routeID?')
          api_utils.recordDeveloperRequest(devStoreKey,api_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 + '(' + json.dumps(json_response) + ');'
      else:
          self.response.headers['Content-Type'] = 'application/json'
          response = json.dumps(json_response)
      
      self.response.out.write(response)
      stathat.apiStatCount()
      # push event out to anyone watching the live board
      task = Task(url='/map/task', params={'stopID':stopID})
      task.add('eventlogger')
Exemplo n.º 4
0
def validateRequest(request,type):
    
    # validate the key
    devStoreKey = api_utils.validateDevKey(request.get('key'))
    if devStoreKey is None:
        logging.debug('... illegal developer key %s' % request.get('key'))
        api_utils.recordDeveloperRequest(None,api_utils.GETSTOPS,request.query_string,request.remote_addr,'illegal developer key specified');
        return None
    
    if type == api_utils.GETSTOPS:
        routeID = request.get('routeID')
        destination = request.get('destination').upper()
        
        # a routeID is required
        if routeID is None or routeID is '':
            api_utils.recordDeveloperRequest(devStoreKey,type,request.query_string,request.remote_addr,'a routeID must be included');
            return None
        elif destination is not None and routeID is '':
            api_utils.recordDeveloperRequest(devStoreKey,type,request.query_string,request.remote_addr,'if a destination is specified, a routeID must be included');
            return None
    elif type == api_utils.GETSTOPLOCATION:
        stopID = api_utils.conformStopID(request.get('stopID'))
        if stopID == '' or stopID == '0' or stopID == '00':
            return None            
    elif type == api_utils.GETNEARBYSTOPS:
        lat = request.get('lat')
        lon = request.get('lon')
        radius = request.get('radius')
        # lat/long is required
        if lat is None or lon is None:
            api_utils.recordDeveloperRequest(devStoreKey,type,request.query_string,request.remote_addr,'both latitude and longitude values must be specified');
            return None
        elif radius is not None and radius is not '' and radius > '5000':
            logging.error('unable to validate getnearbystops call. illegal radius value of %s' % radius)
            api_utils.recordDeveloperRequest(devStoreKey,type,request.query_string,request.remote_addr,'radius must be less than 5,000');
            return None

    return devStoreKey
Exemplo n.º 5
0
    def get(self):
        start = time.time()
        # snare the inputs
        dev_key = self.request.get('key')
        stopID = api_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))

        self.request.registry['aggregated_results'] = []

        try:
            if api_utils.afterHours() is False:  # and dev_key != 'uwkiosk9':

                # validate the request parameters
                devStoreKey = validateRequest(self.request)
                if devStoreKey is None:
                    # filter out the kiosk errors from the log
                    if (not (dev_key == 'kiosk'
                             and self.request.get('stopID') == '')):
                        logging.error(
                            "failed to validate the request parameters")
                    self.response.headers[
                        'Content-Type'] = 'application/javascript'
                    self.response.out.write(
                        json.dumps(
                            api_utils.buildErrorResponse(
                                '-1',
                                'Unable to validate the request. There may be an illegal developer key.'
                            )))
                    return

                if stopID is not '' and routeID is '':
                    json_response = stopRequest(stopID, dev_key)
                    api_utils.recordDeveloperRequest(devStoreKey,
                                                     api_utils.GETARRIVALS,
                                                     self.request.query_string,
                                                     self.request.remote_addr)
                elif stopID is not '' and routeID is not '':
                    json_response = stopRouteRequest(stopID, routeID,
                                                     devStoreKey)
                    api_utils.recordDeveloperRequest(devStoreKey,
                                                     api_utils.GETARRIVALS,
                                                     self.request.query_string,
                                                     self.request.remote_addr)
                elif routeID is not '' and vehicleID is not '':
                    json_response = routeVehicleRequest(
                        routeID, vehicleID, devStoreKey)
                    api_utils.recordDeveloperRequest(devStoreKey,
                                                     api_utils.GETVEHICLE,
                                                     self.request.query_string,
                                                     self.request.remote_addr)
                else:
                    logging.debug("API: invalid request")
                    api_utils.recordDeveloperRequest(
                        devStoreKey, api_utils.GETARRIVALS,
                        self.request.query_string, self.request.remote_addr,
                        'illegal query string combination')
                    json_response = api_utils.buildErrorResponse(
                        '-1', 'Invalid Request parameters')

                # push event out to anyone watching the live board
                channels = memcache.get('channels')
                if channels is not None:
                    task = Task(url='/map/task', params={'stopID': stopID})
                    task.add('eventlogger')

                # stop statistics - DISABLED
                # if( "kiosk" not in dev_key ):
                #     task = Task(url='/stats/stop', params={'apikey':dev_key,'stop':stopID})
                #     task.add('stats')

            else:
                # don't run these jobs during "off" hours
                #logging.debug('shunted... off hour request')
                #if dev_key.find('kiosk') >= 0:
                #  json_response = api_utils.buildErrorResponse('-1','Kiosk requests no longer supported')
                #else:
                json_response = api_utils.buildErrorResponse(
                    '-1', 'The Metro service is not currently running')

            # encapsulate response in json or jsonp
            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 + '(' + json.dumps(json_response) + ');'
            else:
                self.response.headers['Content-Type'] = 'application/json'
                response = json.dumps(json_response)

            self.response.out.write(response)
        except DeadlineExceededError:
            self.response.clear()
            self.response.set_status(500)
            self.response.out.write(
                "This operation could not be completed in time...")

        # persist some statistics
        # stathat:
        if api_utils.afterHours() is False and dev_key != 'uwkiosk9':
            stathat.apiTimeStat(config.STATHAT_API_GETARRIVALS_TIME_KEY,
                                ((time.time() - start) * 1000))
            stathat.apiStatCount()
Exemplo n.º 6
0
    def get(self):
      start = time.time()
      # snare the inputs
      dev_key = self.request.get('key')
      stopID = api_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))

      self.request.registry['aggregated_results'] = []

      try:
          if api_utils.afterHours() is False:  # and dev_key != 'uwkiosk9':

              # validate the request parameters
              devStoreKey = validateRequest(self.request)
              if devStoreKey is None:
                  # filter out the kiosk errors from the log
                  if( not (dev_key == 'kiosk' and self.request.get('stopID') == '') ):
                      logging.error("failed to validate the request parameters")
                  self.response.headers['Content-Type'] = 'application/javascript'
                  self.response.out.write(json.dumps(api_utils.buildErrorResponse('-1','Unable to validate the request. There may be an illegal developer key.')))
                  return

              if stopID is not '' and routeID is '':
                  json_response = stopRequest(stopID, dev_key)
                  api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETARRIVALS,self.request.query_string,self.request.remote_addr);
              elif stopID is not '' and routeID is not '':
                  json_response = stopRouteRequest(stopID, routeID, devStoreKey)
                  api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETARRIVALS,self.request.query_string,self.request.remote_addr);
              elif routeID is not '' and vehicleID is not '':
                  json_response = routeVehicleRequest(routeID, vehicleID, devStoreKey)
                  api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETVEHICLE,self.request.query_string,self.request.remote_addr);
              else:
                  logging.debug("API: invalid request")
                  api_utils.recordDeveloperRequest(devStoreKey,api_utils.GETARRIVALS,self.request.query_string,self.request.remote_addr,'illegal query string combination');
                  json_response = api_utils.buildErrorResponse('-1','Invalid Request parameters')

              # push event out to anyone watching the live board
              channels = memcache.get('channels')
              if channels is not None:
                  task = Task(url='/map/task', params={'stopID':stopID})
                  task.add('eventlogger')

              # stop statistics - DISABLED
              # if( "kiosk" not in dev_key ):
              #     task = Task(url='/stats/stop', params={'apikey':dev_key,'stop':stopID})
              #     task.add('stats')

          else:
              # don't run these jobs during "off" hours
              #logging.debug('shunted... off hour request')
              #if dev_key.find('kiosk') >= 0:
              #  json_response = api_utils.buildErrorResponse('-1','Kiosk requests no longer supported')
              #else:
              json_response = api_utils.buildErrorResponse('-1','The Metro service is not currently running')

          # encapsulate response in json or jsonp
          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 + '(' + json.dumps(json_response) + ');'
          else:
              self.response.headers['Content-Type'] = 'application/json'
              response = json.dumps(json_response)

          self.response.out.write(response)
      except DeadlineExceededError:
          self.response.clear()
          self.response.set_status(500)
          self.response.out.write("This operation could not be completed in time...")

      # persist some statistics
      # stathat:
      if api_utils.afterHours() is False and dev_key != 'uwkiosk9':
        stathat.apiTimeStat(config.STATHAT_API_GETARRIVALS_TIME_KEY,((time.time()-start)*1000))
        stathat.apiStatCount()