Skip to content

Commit

Permalink
Fix BT response schema bug from API-VERSION 2024-01-08
Browse files Browse the repository at this point in the history
I'm not sure why BT responses all of a sudden stopped returning as
strings and started returning as Gson representations. These changes
seem to mitigate the issue though.

Signed-off-by: Devin Morgan <[email protected]>
  • Loading branch information
devinmorgan committed Jul 12, 2024
1 parent 8704552 commit e0bc47c
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.joinforage.forage.android.core.services.forageapi.network.ForageApiRe
import com.joinforage.forage.android.core.services.forageapi.network.ForageError
import com.joinforage.forage.android.core.services.forageapi.network.UnknownErrorApiResponse
import com.joinforage.forage.android.core.services.vault.VaultResponseParser
import org.json.JSONObject

internal class BTResponseParser(btRes: Result<Any?>) : VaultResponseParser {
override val isNullResponse: Boolean = false
Expand Down Expand Up @@ -63,9 +64,21 @@ internal class BTResponseParser(btRes: Result<Any?>) : VaultResponseParser {
return if (!isSuccessful) {
null
} else {
// the BT response get's parsed as a Map instead of String
val jsonResponse = Gson().toJson(vaultResponse)
ForageApiResponse.Success(jsonResponse.toString())
// BT uses the Gson package under the hood to work with JSON
// Unfortunately, they are returning Gson parsed JSON instead
// of standard JSON which forces us to use Gson to get back
// the original JSON (we return a JSON string in our
// ForageApiResponse.Success)
val gsonParsedJson = Gson().toJson(vaultResponse)

// for some really really strange reason, Gson wraps the
// actual JSON we care about into top-level "value" JSON
// field like so: { "value": <json_result> }
// So, annoyingly, we need to unwrap it. Since deferred
// payments have an empty response, we need to consider
// the case where there is no top-level "value" field
val unwrapped = JSONObject(gsonParsedJson).optJSONObject("value") ?: ""
ForageApiResponse.Success(unwrapped.toString())
}
}
}

0 comments on commit e0bc47c

Please sign in to comment.