Fixed the bad crashes

This commit is contained in:
Jan-Lk Else 2017-09-13 22:03:05 +02:00
parent 986c5c4042
commit 2ad784dba7
1 changed files with 59 additions and 54 deletions

View File

@ -12,77 +12,78 @@ object Api {
private val ApiBase = "https://api.telegra.ph/" private val ApiBase = "https://api.telegra.ph/"
fun getPage(path: String?, accessToken: String?, callback: (success: Boolean, page: Page?) -> Unit) { fun getPage(path: String?, accessToken: String?, callback: (success: Boolean, page: Page?) -> Unit) {
Bridge.get("${ApiBase}getPage/$path?access_token=%s&return_content=true", accessToken).asJsonObject { _, jsonObject, exception -> try {
if (jsonObject != null && exception == null) try { Bridge.get("${ApiBase}getPage/$path?access_token=%s&return_content=true", accessToken).asString { response, _, _ ->
callback(true, jsonObject.parsePageResponse()) if (response?.isSuccess == true) callback(true, response.asAsonObject()?.toStockJson()?.parsePageResponse())
} catch (e: Exception) { else callback(false, null)
callback(false, null)
} }
else callback(false, null) } catch (e: Exception) {
callback(false, null)
} }
} }
fun createPage(accessToken: String?, content: String?, title: String?, name: String?, callback: (success: Boolean, Page?) -> Unit) { fun createPage(accessToken: String?, content: String?, title: String?, name: String?, callback: (success: Boolean, Page?) -> Unit) {
Bridge.get("${ApiBase}createPage?access_token=%s&title=%s&author_name=%s&content=%s&return_content=true", accessToken, title, name, content).asJsonObject { _, jsonObject, exception -> try {
if (jsonObject != null && exception == null) try { Bridge.get("${ApiBase}createPage?access_token=%s&title=%s&author_name=%s&content=%s&return_content=true", accessToken, title, name, content).asString { response, _, _ ->
callback(true, jsonObject.parsePageResponse()) if (response?.isSuccess == true) callback(true, response.asAsonObject()?.toStockJson()?.parsePageResponse())
} catch (e: Exception) { else callback(false, null)
callback(false, null)
} }
else callback(false, null) } catch (e: Exception) {
callback(false, null)
} }
} }
fun editPage(accessToken: String?, path: String?, content: String?, title: String?, name: String?, callback: (success: Boolean, Page?) -> Unit) { fun editPage(accessToken: String?, path: String?, content: String?, title: String?, name: String?, callback: (success: Boolean, Page?) -> Unit) {
Bridge.get("${ApiBase}editPage/$path?access_token=%s&title=%s&author_name=%s&content=%s&return_content=true", accessToken, title, name, content).asJsonObject { _, jsonObject, exception -> try {
if (jsonObject != null && exception == null) try { Bridge.get("${ApiBase}editPage/$path?access_token=%s&title=%s&author_name=%s&content=%s&return_content=true", accessToken, title, name, content).asString { response, _, _ ->
callback(true, jsonObject.parsePageResponse()) if (response?.isSuccess == true) callback(true, response.asAsonObject()?.toStockJson()?.parsePageResponse())
} catch (e: Exception) { else callback(false, null)
callback(false, null)
} }
else callback(false, null) } catch (e: Exception) {
callback(false, null)
} }
} }
fun createAccount(callback: (accessToken: String?) -> Unit) { fun createAccount(callback: (accessToken: String?) -> Unit) {
Bridge.get("${ApiBase}createAccount?short_name=teleposter").asJsonObject { _, jsonObject, exception -> try {
if (jsonObject != null && exception == null) try { Bridge.get("${ApiBase}createAccount?short_name=teleposter").asString { response, _, _ ->
callback(jsonObject.optJSONObject("result")?.optString("access_token")) if (response?.isSuccess == true) callback(response.asAsonObject()?.toStockJson()?.optJSONObject("result")?.optString("access_token"))
} catch (e: Exception) { else callback(null)
callback(null)
} }
else callback(null) } catch (e: Exception) {
callback(null)
} }
} }
fun getPageList(accessToken: String?, offset: Int = 0, callback: (success: Boolean, MutableList<Page>?) -> Unit) { fun getPageList(accessToken: String?, offset: Int = 0, callback: (success: Boolean, MutableList<Page>?) -> Unit) {
Bridge.get("${ApiBase}getPageList?access_token=%s&limit=200&offset=$offset", accessToken).asJsonObject { _, jsonObject, exception -> try {
if (jsonObject != null && exception == null) try { Bridge.get("${ApiBase}getPageList?access_token=%s&limit=200&offset=$offset", accessToken).asString { response, _, _ ->
jsonObject.optJSONObject("result")?.let { if (response?.isSuccess == true)
val totalCount = it.optInt("total_count") response.asAsonObject()?.toStockJson()?.optJSONObject("result")?.let {
var currentCount = 200 + offset val totalCount = it.optInt("total_count")
val result = mutableListOf<Page>() var currentCount = 200 + offset
it.optJSONArray("pages")?.let { val result = mutableListOf<Page>()
for (i in 0 until it.length()) { it.optJSONArray("pages")?.let {
val page = it.optJSONObject(i)?.parsePage() for (i in 0 until it.length()) {
if (page != null) result.add(page) val page = it.optJSONObject(i)?.parsePage()
} if (page != null) result.add(page)
}
if (currentCount < totalCount) {
getPageList(accessToken, currentCount) { success, pages ->
if (success && pages != null) {
result.addAll(pages)
callback(true, result)
} }
if (!success) callback(false, null)
} }
currentCount += 200 if (currentCount < totalCount) {
} else callback(true, result) getPageList(accessToken, currentCount) { success, pages ->
} ?: callback(false, null) if (success && pages != null) {
} catch (e: Exception) { result.addAll(pages)
callback(false, null) callback(true, result)
}
if (!success) callback(false, null)
}
currentCount += 200
} else callback(true, result)
} ?: callback(false, null)
else callback(false, null)
} }
else callback(false, null) } catch (e: Exception) {
callback(false, null)
} }
} }
@ -126,12 +127,16 @@ object Api {
} }
fun uploadImage(file: File, callback: (url: String?) -> Unit) { fun uploadImage(file: File, callback: (url: String?) -> Unit) {
Bridge.post("http://telegra.ph/upload") try {
.body(MultipartForm().add("FileUpload", file)) Bridge.post("http://telegra.ph/upload")
.asJsonArray { _, jsonArray, _ -> .body(MultipartForm().add("FileUpload", file))
val src = jsonArray?.optJSONObject(0)?.optString("src", null) .asJsonArray { _, jsonArray, _ ->
callback(src) val src = jsonArray?.optJSONObject(0)?.optString("src", null)
} callback(src)
}
} catch (e: Exception) {
callback(null)
}
} }
} }