Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

New Resource Client #11

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add enum to handle httpverb and action
  • Loading branch information
Verachad Wongsawangtham committed Dec 17, 2014
commit 0215e2d1f16f848d56b9f9e55c219b7c457e7d26
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun String.toStartingLetterUppercase(): String {
return builder.toString()
}

fun String.SnakeToCamel(): String {
fun String.fromSnakeToCamel(): String {

if (this.isEmpty()) return this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.taskworld.android.restfulandroidkotlin.model.PlayList
/**
* Created by VerachadW on 11/18/14.
*/
class PostCreatePlayListSpiceRequest(val path: String, val entity: PlayList):
class CreatePlayListSpiceRequest(val path: String, val entity: PlayList):
RetrofitSpiceRequest<Map<String, String>, TheMovieDBAPI.ListAPI>(javaClass<Map<String, String>>(), javaClass<TheMovieDBAPI.ListAPI>()) {

override fun loadDataFromNetwork(): Map<String, String>? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.taskworld.android.restfulandroidkotlin.resource.router.ResourceRouter
import com.taskworld.android.restfulandroidkotlin.model.PlayList
import java.util.HashMap
import com.squareup.okhttp.RequestBody
import com.taskworld.android.restfulandroidkotlin.extensions.fromSnakeToCamel

class ResourceClient(builder: ResourceClient.Builder) {

Expand All @@ -22,29 +23,50 @@ class ResourceClient(builder: ResourceClient.Builder) {
private var mRealm: Realm?
private var mBus: EventBus

private var mResourceUrlMap: HashMap<String, String>

//initialize
{
mSpiceManager = builder.manager
mResourceRouter = builder.router ?: ResourceRouterImpl.newInstance()
mRealm = builder.realm
mBus = builder.bus ?: EventBus.getDefault()
mResourceUrlMap = hashMapOf("playlist" to "list")
}

enum class Action {
NONE
CREATE
UPDATE
DELETE
GET
CREATE {
override fun getVerb(): String {
return "post"
}
}
UPDATE {
override fun getVerb(): String {
return "put"
}
}
DELETE {
override fun getVerb(): String {
return "delete"
}
}
GET {
override fun getVerb(): String {
return "get"
}
}
GET_LIST {
override fun getVerb(): String {
return "get_list"
}

override fun toString(): String {
return "list"
}
}

open fun getVerb(): String {
return ""
}

override fun toString(): String {
return this.name().toLowerCase()
}
Expand Down Expand Up @@ -95,11 +117,10 @@ class ResourceClient(builder: ResourceClient.Builder) {
mBus.post(entity)

// TODO: Call Create API
val http = "post"
val action = Action.CREATE
val path: String? = mResourceRouter.getPathForAction(action, clazz)

executeWithEventBusListener(http, action, clazz.getSimpleName(), path!!, entity, clazz)
executeWithEventBusListener(action, clazz.getSimpleName(), path!!, entity, clazz)
}

fun <T : RealmObject> update(clazz: Class<T>, conditionMap: Map<String, String>, f: (it: RealmResults<T>) -> Unit) {
Expand Down Expand Up @@ -150,20 +171,18 @@ class ResourceClient(builder: ResourceClient.Builder) {
val results = query(clazz, args)
mBus.post(results)

val httpVerb = "get"
val action = Action.GET_LIST
val path = mResourceRouter.getPathForAction(action, clazz, args)

//network
executeWithEventBusListener<T>(httpVerb, action, clazz.getSimpleName(), path!!)
executeWithEventBusListener<T>(action, clazz.getSimpleName(), path!!)
}

fun <T : RealmObject> find(clazz: Class<T>, id: String) {
find(clazz, id, null)
}

fun <T : RealmObject> find(clazz: Class<T>, id: String, args: Map<String, String>?) {
val httpVerb = "get"
val action = Action.GET

var newArgs = hashMapOf("id" to id)
Expand All @@ -180,23 +199,22 @@ class ResourceClient(builder: ResourceClient.Builder) {
}

//network
executeWithEventBusListener<T>(httpVerb, action, clazz.getSimpleName(), path!!)
executeWithEventBusListener<T>(action, clazz.getSimpleName(), path!!)
}

fun <T : RealmObject> executeWithEventBusListener(httpVerb: String, action: ResourceClient.Action, resourceName: String, requestPath: String) {
executeWithEventBusListener<T>(httpVerb, action, resourceName, requestPath, null, null)
fun <T : RealmObject> executeWithEventBusListener(action: ResourceClient.Action, resourceName: String, requestPath: String) {
executeWithEventBusListener<T>(action, resourceName, requestPath, null, null)
}

fun <T : RealmObject> executeWithEventBusListener(httpVerb: String, action: ResourceClient.Action, resourceName: String, requestPath: String, entity: T?, clazz: Class<T>?) {
fun <T : RealmObject> executeWithEventBusListener(action: ResourceClient.Action, resourceName: String, requestPath: String, entity: T?, clazz: Class<T>?) {
var extraPath = ""
when (action) {
Action.GET -> extraPath = mResourceRouter.extraPathForSingle ?: ""
Action.GET_LIST -> extraPath = mResourceRouter.extraPathForList ?: ""
}

if (mSpiceManager != null) {
val className = listOf(httpVerb.toStartingLetterUppercase(),
action.toString().toStartingLetterUppercase(),
val className = listOf(action.getVerb().fromSnakeToCamel(),
resourceName.toStartingLetterUppercase(),
extraPath.replace("_", "").toStartingLetterUppercase(),
REQUEST_CLASS_SUFFIX).join("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ trait ResourceRouter {
}

fun <T : RealmObject> getPathForAction(action: Action, clazz: Class<T>, args: Map<String, Any>?): String? {
when(action){
Action.GET_LIST -> return getPathForListOnResource(clazz, args)
Action.CREATE -> return getPathForCreateResource(clazz, args)
return when(action){
Action.GET_LIST -> getPathForListOnResource(clazz, args)
Action.CREATE -> getPathForCreateResource(clazz, args)
Action.GET -> getPathForSingleOnResource(clazz, args)
else -> null
}
return null
}

fun <T : RealmObject> getPathForListOnResource(clazz: Class<T>, args: Map<String, Any>?): String
Expand Down