Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change get api deal with the 500 code #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions src/main/kotlin/io/ipfs/kotlin/commands/Get.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.ipfs.kotlin.commands

import io.ipfs.kotlin.IPFSConnection
import com.le.ipfsapi.IPFSConnection
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import java.io.InputStream

Expand All @@ -11,25 +13,58 @@ class Get(val ipfs: IPFSConnection) {
*
* @param hash The hash of the content in base58.
*/
fun cat(hash: String): String = ipfs.callCmd("cat/$hash").use(ResponseBody::string)
fun cat(hash: String): String? {
val response = catApi(hash)
return if (response.isSuccessful && null != response.body()) {
response.body()!!.use(ResponseBody::string)
} else {
null
}
}

/**
* Cat IPFS content and return it as ByteArray.
*
* @param hash The hash of the content in base58.
*/
fun catBytes(hash: String): ByteArray = ipfs.callCmd("cat/$hash").use(ResponseBody::bytes)
fun catBytes(hash: String): ByteArray? {
val response = catApi(hash)
return if (response.isSuccessful && null != response.body()) {
response.body()!!.use { it.bytes() }
} else {
null
}
}

/**
* Cat IPFS content and process it using InputStream.
*
* @param hash The hash of the content in base58.
* @param handler Callback which handle processing the input stream. When the callback return the stream and the request body will be closed.
*/
fun catStream(hash: String, handler: (stream: InputStream) -> Unit): Unit =
ipfs.callCmd("cat/$hash").use { body ->
val inputStream = body.byteStream()
inputStream.use(handler)
}
fun catStream(hash: String, handler: (stream: InputStream?) -> Unit): Unit {
val response = catApi(hash)
if (response.isSuccessful && null != response.body()) {
response.body()!!.use { it.byteStream().use(handler) }
} else {
handler.invoke(null)
}
}

/**
* If you cat a wrong hash, the ipfs http api will response code 500
* and with message. In this case if we use the response boy and cover it
* to stream or byte ,it will not the true content of the hash code,it will
* be the content of the 'Wrong Message'.
*
* So we need check the response code by {response.isSuccessful} to insure
* we go the right response.
*/
private fun catApi(hash: String):Response{
val request = Request.Builder()
.url(ipfs.config.base_url + "cat/$hash")
.build()
return ipfs.config.okHttpClient.newCall(request).execute()
}

}