First, add the Kong Unirest dependency to your project's build file. For Gradle, add the following to your build.gradle file:
dependencies {
implementation 'com.mashape:unirest-java:1.4.9'
}
Now, you can implement the getGithubIssue function using Kotlin and Kong Unirest:
import com.mashape.unirest.http.HttpResponse
import com.mashape.unirest.request.GetRequest
import com.mashape.unirest.request.Route
import com.mashape.unirest.request.RouteParams
import com.mashape.unirest.response.JsonResponse
import com.mashape.unirest.response.UnirestException
import org.apache.commons.lang3.StringEscapeUtils
import org.json.JSONObject
data class GithubIssue(val number: Int, val title: String, val body: JSONObject)
fun getGithubIssue(owner: String, repo: String, number: Int): GithubIssue {
val url = "https://api.github.com/repos/$owner/$repo/issues/$number"
val request = GetRequest.route(Route.of(url).addParam(RouteParams.of("state", "open")))
try {
val response = request.asJson()
if (response.getStatusCode() != 200) {
throw UnirestException.from(request)
}
val jsonObject = JSONObject(StringEscapeUtils.unescapeJson(response.getBody().toString()))
val issue = GithubIssue(jsonObject.getInt("number"), jsonObject.getString("title"), jsonObject.getJSONObject("body"))
return issue
} catch (e: UnirestException) {
if (e.getStatusCode() == 404) {
return null
}
throw e
}
}
This code snippet defines a GithubIssue data class, a getGithubIssue function, and uses Kong Unirest's GetRequest, JsonResponse, and Route to fetch the GitHub issue details. The function returns a GithubIssue object with the number, title, and body fields. If the status code is 404, it returns null, otherwise, it throws an exception.