Skip to content

Instantly share code, notes, and snippets.

@ashish410
Last active Jan 6, 2021
Embed
What would you like to do?
class Request(internal val method: String) {
internal val header: MutableMap<String, String> = HashMap()
internal var url: String? = null
internal var body: ByteArray? = null
private var jsonObjReqListener: JSONObjectListener? = null
private var threadExecutor: ThreadExecutor = ThreadExecutor()
fun url(url: String?): Request {
this.url = url
return this
}
fun body(bodyJson: JSONObject?): Request {
val textBody = bodyJson?.toString()
body = textBody?.toByteArray(charset(UTF_8))
this.header["Content-Type"] = "application/json"
return this
}
fun header(header: Map<String, String>?): Request {
if (header.isNullOrEmpty()) return this
this.header.putAll(header)
return this
}
fun makeRequest(jsonObjectListener: JSONObjectListener?): Request {
this.jsonObjReqListener = jsonObjectListener
threadExecutor.execute(RequestTask(this))
return this
}
internal fun sendResponse(resp: Response?, e: Exception?) {
if (jsonObjReqListener != null) {
if (e != null) jsonObjReqListener?.onFailure(e)
else jsonObjReqListener?.onResponse(resp?.asJSONObject())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment