2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.api
|
|
|
|
|
|
|
|
import org.json.JSONArray
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
|
|
|
import java.util.regex.Pattern
|
|
|
|
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.LogCategory
|
2019-09-12 16:05:18 +02:00
|
|
|
import jp.juggler.util.groupEx
|
2018-01-04 19:52:25 +01:00
|
|
|
import okhttp3.Response
|
|
|
|
import okhttp3.WebSocket
|
|
|
|
|
|
|
|
open class TootApiResult(
|
2018-01-12 10:01:25 +01:00
|
|
|
@Suppress("unused") val dummy : Int = 0,
|
2018-01-04 19:52:25 +01:00
|
|
|
var error : String? = null,
|
|
|
|
var response : Response? = null,
|
2018-01-10 16:47:35 +01:00
|
|
|
var bodyString : String? = null
|
2018-01-04 19:52:25 +01:00
|
|
|
) {
|
2018-01-18 22:22:27 +01:00
|
|
|
companion object {
|
|
|
|
private val log = LogCategory("TootApiResult")
|
|
|
|
|
|
|
|
private val reLinkURL = Pattern.compile("<([^>]+)>;\\s*rel=\"([^\"]+)\"")
|
|
|
|
|
|
|
|
private const val NO_INSTANCE = "missing instance name"
|
|
|
|
|
|
|
|
fun makeWithCaption(caption : String?) : TootApiResult {
|
|
|
|
val result = TootApiResult()
|
|
|
|
if(caption?.isEmpty() != false) {
|
|
|
|
result.error = NO_INSTANCE
|
|
|
|
} else {
|
|
|
|
result.caption = caption
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 10:01:25 +01:00
|
|
|
|
2019-09-27 04:30:48 +02:00
|
|
|
var requestInfo = "(no request information)"
|
|
|
|
|
2018-01-13 07:15:52 +01:00
|
|
|
var tokenInfo : JSONObject? = null
|
2018-01-12 10:01:25 +01:00
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
var data : Any? = null
|
2018-01-12 10:01:25 +01:00
|
|
|
set(value) {
|
2018-01-10 16:47:35 +01:00
|
|
|
if(value is JSONArray) {
|
|
|
|
parseLinkHeader(response, value)
|
|
|
|
}
|
|
|
|
field = value
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-12 10:01:25 +01:00
|
|
|
val jsonObject : JSONObject?
|
|
|
|
get() = data as? JSONObject
|
|
|
|
|
|
|
|
val jsonArray : JSONArray?
|
|
|
|
get() = data as? JSONArray
|
|
|
|
|
|
|
|
val string : String?
|
|
|
|
get() = data as? String
|
|
|
|
|
|
|
|
var link_older : String? = null // より古いデータへのリンク
|
|
|
|
var link_newer : String? = null // より新しいデータへの
|
|
|
|
var caption : String = "?"
|
|
|
|
|
|
|
|
constructor() : this(0)
|
|
|
|
|
|
|
|
constructor(error : String) : this(0, error = error)
|
|
|
|
|
2018-01-18 22:22:27 +01:00
|
|
|
constructor(socket : WebSocket) : this(0) {
|
|
|
|
this.data = socket
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:01:25 +01:00
|
|
|
constructor(response : Response, error : String)
|
|
|
|
: this(0, error, response)
|
|
|
|
|
|
|
|
constructor(response : Response, bodyString : String, data : Any?)
|
|
|
|
: this(0, response = response, bodyString = bodyString) {
|
|
|
|
this.data = data
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
// return result.setError(...) と書きたい
|
2018-01-12 10:01:25 +01:00
|
|
|
fun setError(error : String) : TootApiResult {
|
2018-01-04 19:52:25 +01:00
|
|
|
this.error = error
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:22:27 +01:00
|
|
|
private fun parseLinkHeader(response : Response?, array : JSONArray) {
|
2018-01-12 10:01:25 +01:00
|
|
|
response ?: return
|
2018-01-18 22:22:27 +01:00
|
|
|
|
2018-01-12 10:01:25 +01:00
|
|
|
log.d("array size=%s", array.length())
|
|
|
|
|
|
|
|
val sv = response.header("Link")
|
|
|
|
if(sv == null) {
|
|
|
|
log.d("missing Link header")
|
|
|
|
} else {
|
|
|
|
// Link: <https://mastodon.juggler.jp/api/v1/timelines/home?limit=XX&max_id=405228>; rel="next",
|
|
|
|
// <https://mastodon.juggler.jp/api/v1/timelines/home?limit=XX&since_id=436946>; rel="prev"
|
|
|
|
val m = reLinkURL.matcher(sv)
|
|
|
|
while(m.find()) {
|
2019-09-12 16:05:18 +02:00
|
|
|
val url = m.groupEx(1)
|
|
|
|
val rel = m.groupEx(2)
|
2018-01-28 20:03:04 +01:00
|
|
|
// warning.d("Link %s,%s",rel,url);
|
2018-01-12 10:01:25 +01:00
|
|
|
if("next" == rel) link_older = url
|
|
|
|
if("prev" == rel) link_newer = url
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|