Skip to content

Instantly share code, notes, and snippets.

@enachb
Created March 18, 2014 00:14
Show Gist options
  • Select an option

  • Save enachb/9611133 to your computer and use it in GitHub Desktop.

Select an option

Save enachb/9611133 to your computer and use it in GitHub Desktop.
usergrid sample code
//login with client ID & secret but does
val client = new Client(Config.OrgName, Config.AppName)
client.setApiUrl(Config.UserGridUrl)
client.setApplicationId(Config.AppId)
val resp = client.authorizeAppClient(Config.UserGridClientId, Config.UserGridClientSecret)
if(resp.getError != null){
error("Login status: " + resp)
throw new LoginException(resp.getError)
}
def save(implicit client: Client): ApiResponse = {
info("persisting: " + toJson)
val resp = client.apiRequest(HttpMethod.POST,
null,
toJson(),
client.getClientId,
client.getApplicationId,
getClass.getSimpleName)
debug("save response: " + resp.getEntities.toString)
resp
}
def loadById(uuid: String)(implicit client: Client): Option[Domain[T]] = {
info(getClass.getSimpleName + ": reading " + uuid)
val params = Map[String, AnyRef]("ql" -> s"select * where uuid=${uuid}")
val response = client.apiRequest(HttpMethod.GET, params.asJava, null, Config.OrgId, Config.AppId, getClass.getSimpleName)
debug("DAAAAAAATAAAAAA: " + response.getEntities.toString)
response.getEntityCount match {
case 1 =>
Some(convertToObj(response.getFirstEntity.toString))
case 0 =>
None
case _ =>
throw new Exception("Got more than one entity back for " + getClass + response.getEntities.toString)
}
}
def loadByQuery(query: String)(implicit client: Client): Seq[Domain[T]] = {
info(getClass.getSimpleName + ": querying " + query)
val params = Map[String, AnyRef]("ql" -> query)
val response = client.apiRequest(HttpMethod.GET, params.asJava, null, Config.OrgId, Config.AppId, getClass.getSimpleName)
debug(getClass.getSimpleName + s": submitting query: ${query}")
// Create object list and return. Will be empty if query didn't yield anything.
//TODO see what is does when the list gets loooooong
response.getEntities.asScala.map {
e =>
debug("Got entity: " + e.toString)
convertToObj(e.toString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment