Colton Idle
08/29/2021, 6:19 AMpath == "/api/myendpoint" -> {
return MockResponse()
.setResponseCode(200)
.setBody(
"""
{"status":0,"message":"SUCCESS"}
""".trimIndent())
}
I now want to take this json, place it into resources/json/myendpoint.json
and read it from the file instead of having large json snippets in my kotlin code.
As an end result I'd love to be able to do this but I'm a bit in over my head in terms of grabbing a resource and converting it to a string. There seems to be like 1000 different ways to do this via SO and from what I would expect... maybe there's an easy way to do this with okhttp/mockwebserver/okio that I'm missing?
path == "/api/myendpoint" -> {
return MockResponse()
.setResponseCode(200)
.setBody(javaClass.classLoader.getResourceAsStream("json/myendpoint.json").toString())
}
ephemient
08/29/2021, 6:23 AM.toString()
there, it'll return the String form of the InputStream…Colton Idle
08/29/2021, 6:25 AMjavaClass.classLoader.getResourceAsStream("json/myendpoint.json").toString()
is pseudo code so-to-speak, but is there an easy way to just convert it to a string via some kind of okhttp/okio helpers?ephemient
08/29/2021, 6:25 AM.getResource("META-INF/MANIFEST.MF").readText()
would workephemient
08/29/2021, 6:26 AMFileSystem.RESOURCES
Colton Idle
08/29/2021, 6:27 AMjavaClass.classLoader.getResource("json/myendpoint.json").readText()
worked!ephemient
08/29/2021, 6:28 AMFileSystem.RESOURCES.read("json/myendpoint.json".toPath()) { readUtf8() }
to work equivalentlyephemient
08/29/2021, 6:31 AMColton Idle
08/29/2021, 6:45 AMephemient
08/29/2021, 8:23 AMephemient
08/29/2021, 8:23 AM