Skip to content

Commit

Permalink
- json string method separated
Browse files Browse the repository at this point in the history
  • Loading branch information
semihozkoroglu committed Apr 11, 2021
1 parent fb85159 commit 3e4ddda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/rbs/android/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MainActivity : AppCompatActivity() {
btnSearch.setOnClickListener {
rbs.sendAction(
action = "rbs.address.get.COUNTRIES",
data = mapOf(Pair("cartId", "1de255c877")),
jsonString = null,
headers = mapOf(Pair("header1", "parameter 1"), Pair("header2", "parameter 2"), Pair("header3", "parameter 3")),
success = { jsonData ->
Log.e("RBSService", jsonData) // Convert to data model with Gson()
Expand Down
36 changes: 30 additions & 6 deletions rbs/src/main/java/com/rettermobile/rbs/RBS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,46 @@ class RBS(
fun sendAction(
action: String,
data: Map<String, Any> = mapOf(),
jsonString: String? = null,
headers: Map<String, String> = mapOf(),
success: ((String?) -> Unit)? = null,
error: ((Throwable?) -> Unit)? = null
) {
GlobalScope.launch {
async(Dispatchers.IO) {
if (!TextUtils.isEmpty(action)) {
val requestJsonString = if (TextUtils.isEmpty(jsonString)) {
Gson().toJson(data)
val res =
kotlin.runCatching { executeRunBlock(action = action, requestJsonString = Gson().toJson(data), headers = headers) }

if (res.isSuccess) {
withContext(Dispatchers.Main) {
success?.invoke(res.getOrNull())
}
} else {
jsonString
withContext(Dispatchers.Main) {
error?.invoke(res.exceptionOrNull())
}
}
} else {
withContext(Dispatchers.Main) {
error?.invoke(IllegalArgumentException("action must not be null or empty"))
}
}
}
}
}

fun sendAction(
action: String,
jsonString: String? = null,
headers: Map<String, String> = mapOf(),
success: ((String?) -> Unit)? = null,
error: ((Throwable?) -> Unit)? = null
) {
GlobalScope.launch {
async(Dispatchers.IO) {
if (!TextUtils.isEmpty(action)) {
val res =
kotlin.runCatching { executeRunBlock(action = action, requestJsonString = requestJsonString, headers = headers) }
kotlin.runCatching { executeRunBlock(action = action, requestJsonString = jsonString, headers = headers) }

if (res.isSuccess) {
withContext(Dispatchers.Main) {
Expand Down Expand Up @@ -206,7 +230,7 @@ class RBS(
}
}

val res = service.executeAction(tokenInfo!!.accessToken, action!!, requestJsonString ?: "", headers ?: mapOf(), isGenerate)
val res = service.executeAction(tokenInfo!!.accessToken, action!!, requestJsonString ?: Gson().toJson(null), headers ?: mapOf(), isGenerate)

return if (res.isSuccess) {
Log.e("RBSService", "executeAction success")
Expand Down

0 comments on commit 3e4ddda

Please sign in to comment.