Use POST request for everything (should fix a lot of bugs)

This commit is contained in:
Jan-Lukas Else 2018-02-06 20:12:01 +01:00
parent 0eb13a09ab
commit b074461076
1 changed files with 6 additions and 3 deletions

View File

@ -3,7 +3,7 @@ package telegra.ph
import com.github.kittinunf.fuel.android.core.Json
import com.github.kittinunf.fuel.android.extension.responseJson
import com.github.kittinunf.fuel.core.*
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.fuel.httpPost
import com.github.kittinunf.fuel.httpUpload
import com.github.kittinunf.result.Result
import org.json.JSONArray
@ -20,10 +20,13 @@ object TelegraphApi {
}
private fun callService(method: String, parameters: List<Pair<String, Any?>>, handler: (Request, Response, Result<Json, FuelError>) -> Unit) {
method.httpGet(parameters.filter { it.second != null }).responseJson(handler)
val requestObject = JSONObject()
parameters.forEach {
requestObject.put(it.first, it.second)
}
method.httpPost().header("Content-Type" to "application/json").body(requestObject.toString()).responseJson(handler)
}
fun createAccount(shortName: String, authorName: String? = null, authorUrl: String? = null, callback: (success: Boolean, account: Account?, error: String?) -> Unit) {
callService("/createAccount", listOf("short_name" to shortName, "author_name" to authorName, "author_url" to authorUrl)) { _, _, result ->
handleResponse(result, callback) { obj: JSONObject -> callback(true, Account(obj), null) }