Skip to content

Instantly share code, notes, and snippets.

@tohenryliu
Created October 11, 2012 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tohenryliu/3875364 to your computer and use it in GitHub Desktop.
Save tohenryliu/3875364 to your computer and use it in GitHub Desktop.
automate api action invoke
"
name, com.patch.kickass.actions.EmailPreferenceActions$SetOptOut
parameters: UserRef
param values: 1
"
"
output:
action name, param types, return type, case class, implementation(needed to invoke the action)
"
"inspect:"
{
"actions":[
{
"action_name": "com.patch.kickass.actions.EmailPreferenceActions$SetOptOut",
"parameters" : [
{ "type": "com.patch.kickass.models.UserRef" }
],
"return_type" : "Unit"
},
{
"action_name": "com.patch.kickass.actions.EmailPreferenceActions$SetSubscribedVars",
"parameters": [
{ "type": "com.patch.kickass.models.UserRef" },
{ "type": "scala.collection.immutable.Map[String, AnyRef]" }
],
"return_type": "?"
},
{
"action_name": "com.patch.kickass.actions.EmailPreferenceActions$SetSubscribedTemplates",
"parameters": [
{ "type": "com.patch.kickass.models.UserRef" },
{ "type": "scala.collection.immutable.Set[Template]" }
],
"return_type": "?"
}
]
}
"invoke:"
{
"input": {
"action_name": "com.patch.kickass.actions.EmailPreferenceActions$SetOptOut",
"parameters": [
{
"position": 1,
"type": "com.patch.kickass.models.UserRefs.ById",
"value": 123
}
]
},
"output":
"json of the return data structure"
}
to expose store actions
/*
object ApiEmailPreferenceActions {
val SetOptOut = EmailPreferenceActions.SetOptOut
val GetSailthruEmail = EmailPreferenceActions.GetSailthruEmail
...
// leave out the other members
}
*/
type Json = Map[String, Any]
// get the object with the given class name
// getSingletonInstance("com.patch.kickass.actions.EmailPreferenceActions")
def getSingletonInstance(cls:String):Object
// getMethod("com.patch.kickass.actions.EmailPreferenceActions", "GetSailthruEmail")
def getMethod(cls:String, methodName:String):Method
// get a serializer that is capable for the return type of this method
def getSerializer(m:Method): (?=>Json) // controls serialization
def getDeserializer(m:Method, paramPosition:Int): (Json=>Object) // controls serialization
OR
def getSerializer(cls:Class): (?=>Json) // controls serialization
def getDeserializer(cls:Class): (Json=>Object) // controls serialization
// invoke the method with params, get the data back
// val inst = getSingletonInstance("com.patch.kickass.actions.EmailPreferenceActions")
// val m = getMethod("com.patch.kickass.actions.EmailPreferenceActions", "GetSailthruEmail")
// val ser = getSerializer(m)
// val json = call(inst, m, ser)
def call(driver:Driver, instance:Object, method:Method, params:Json, serializer: Any=>Json):Json
format/layout of the returned data is controlled manually.
POST:http://api-server-host/genetic-end-point?input={json payload here}
code snippet
val singleton = Class.forName("com.patch.kickass.actions.ApiEmailPreferenceActions$").getDeclaredFields()(0).get(null)
Class.forName("com.patch.kickass.actions.ApiEmailPreferenceActions$").getDeclaredMethods()(0).invoke(singleton)
Class.forName("com.patch.kickass.actions.EmailPreferenceActions").getClasses()(0).getMethods.filter{m=>m.getName=="implementation"}(0).getParameterTypes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment