# Path: apps/api_bridge.py
# def getarrivals(request, result_count=3):
# def getparking():


import logging
import webapp2
import twilio
import config
import paywall
from google.appengine.api import memcache
from google.appengine.api.labs.taskqueue import Task
from apps import api_bridge

## end PhoneRequestBusHandler

class PhoneRequestStopHandler(webapp2.RequestHandler):

    def get(self):

      # validate it is in fact coming from twilio
      if config.ACCOUNT_SID == self.request.get('AccountSid'):
        logging.debug("PHONE request was confirmed to have come from Twilio.")
      else:
        logging.error("was NOT VALID.  It might have been spoofed!")
        self.response.out.write(errorResponse("Illegal caller"))
        return

      # pull the route and stopID out of the request body and
      # pad it with a zero on the front if the message forgot
      # to include it (that's how they are stored in the DB)
      routeID = memcache.get(self.request.get('AccountSid'))
      memcache.delete(self.request.get('AccountSid'))

      stopID = self.request.get('Digits')
      if len(stopID) == 3:
        stopID = "0" + stopID

      # hack - creating a string out of the details to conform to other interfaces
      requestArgs = "%s %s" % (routeID, stopID)

      ## magic ##
      logging.info('starting the magic... %s' % requestArgs)

# Path: apps/api_bridge.py
# def getarrivals(request, result_count=3):
# def getparking():


# Path: apps/phone.py
import logging
import webapp2
import twilio
import config
import paywall
from google.appengine.api import memcache
from google.appengine.api.labs.taskqueue import Task
from apps import api_bridge


## end PhoneRequestBusHandler

class PhoneRequestStopHandler(webapp2.RequestHandler):

    def get(self):

      # validate it is in fact coming from twilio
      if config.ACCOUNT_SID == self.request.get('AccountSid'):
        logging.debug("PHONE request was confirmed to have come from Twilio.")
      else:
        logging.error("was NOT VALID.  It might have been spoofed!")
        self.response.out.write(errorResponse("Illegal caller"))
        return

      # pull the route and stopID out of the request body and
      # pad it with a zero on the front if the message forgot
      # to include it (that's how they are stored in the DB)
      routeID = memcache.get(self.request.get('AccountSid'))
      memcache.delete(self.request.get('AccountSid'))

      stopID = self.request.get('Digits')
      if len(stopID) == 3:
        stopID = "0" + stopID

      # hack - creating a string out of the details to conform to other interfaces
      requestArgs = "%s %s" % (routeID, stopID)

      ## magic ##
      logging.info('starting the magic... %s' % requestArgs)

      textBody = api_bridge.getarrivals(requestArgs, 1)