Skip to content

Instantly share code, notes, and snippets.

@droid-ash
Last active August 2, 2022 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save droid-ash/5da56f18f8bd2d56eddcd7f3871c81bd to your computer and use it in GitHub Desktop.
Save droid-ash/5da56f18f8bd2d56eddcd7f3871c81bd to your computer and use it in GitHub Desktop.
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