Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.47 KB | None | 0 0
  1. package controllers
  2. import scala.concurrent.Future
  3. import javax.inject.Inject
  4. import play.api.mvc._
  5. import play.api.libs.concurrent.Execution.Implicits.defaultContext
  6. import models.sms.smsApi
  7.  
  8.  
  9. class Sms @Inject()() extends Controller {
  10.   private lazy val apiCaller = new smsApi
  11.   def handleApi(api: String) = Action.async{request =>
  12.     val f = Future[String]{  apiCaller.parseMethod(api) }
  13.     val q = f.flatMap {
  14.       case s => Future(s)
  15.       case _ => Future("Error")
  16.     }
  17.     q.map(i => Ok(i))
  18.   }
  19. }
  20.  
  21. -----------------------------------------------------------------------------------------------------------------------
  22. package models.sms
  23. import play.api.libs.json._
  24.  
  25.  
  26. class smsApi {
  27.   private lazy val actionHandler = Map[ String, Function[String,String] ](
  28.     "get_state"       -> { getState ( _ )   },
  29.     "invalid_request" -> { invalidReq( _ )  }
  30.   )
  31.  
  32.   def parseMethod(arg: String): String  = {
  33.     val rfc = try {
  34.       (Json.parse(arg) \ "p_action").as[String]
  35.     } catch {
  36.       case e: JsResultException => "invalid_request"
  37.     }
  38.  
  39.    if( actionHandler.contains(rfc) )
  40.      actionHandler( rfc )(arg)
  41.    else
  42.      noApiMethodFound
  43.   }
  44.  
  45.   def getState (agr:String ): String = {
  46.   ""
  47.   }
  48.  
  49.   def noApiMethodFound: String = {
  50.     Json.toJson( Map("meta"->"Error","data"->"No method found") ).toString
  51.   }
  52.  
  53.   def invalidReq(arg: String): String = {
  54.     Json.toJson( Map("meta"->"Error","data"->"Invalid request type") ).toString
  55.   }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement