mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better JCenter error handling.
This commit is contained in:
parent
bc575412a4
commit
ffb0397a3c
2 changed files with 18 additions and 30 deletions
|
@ -6,21 +6,11 @@ import com.beust.kobalt.misc.log
|
|||
import com.squareup.okhttp.*
|
||||
import retrofit.mime.TypedFile
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
public class Http {
|
||||
class Body(val body: ResponseBody, val code: Int) {
|
||||
public fun getAsString() : String {
|
||||
return body.string()
|
||||
}
|
||||
public fun getAsStream() : InputStream {
|
||||
return body.byteStream()
|
||||
}
|
||||
}
|
||||
|
||||
public fun get(user: String?, password: String?, url: String) : Body {
|
||||
public fun get(user: String?, password: String?, url: String) : Response {
|
||||
val client = OkHttpClient();
|
||||
val request = Request.Builder().url(url)
|
||||
if (user != null) {
|
||||
|
@ -28,16 +18,13 @@ public class Http {
|
|||
}
|
||||
|
||||
try {
|
||||
val response = client.newCall(request.build()).execute()
|
||||
return Body(response.body(), response.code())
|
||||
return client.newCall(request.build()).execute()
|
||||
} catch(ex: IOException) {
|
||||
throw KobaltException("Could not load URL $url, error: " + ex.message, ex)
|
||||
}
|
||||
}
|
||||
|
||||
private val MEDIA_TYPE_BINARY = MediaType.parse("application/octet-stream")
|
||||
|
||||
public fun get(url: String) : Body {
|
||||
public fun get(url: String) : Response {
|
||||
return get(null, null, url)
|
||||
}
|
||||
|
||||
|
|
|
@ -31,14 +31,15 @@ open public class UnauthenticatedJCenterApi @Inject constructor(open val http: H
|
|||
const val BINTRAY_URL_API_CONTENT = BINTRAY_URL_API + "/content"
|
||||
}
|
||||
|
||||
fun parseResponse(response: String, networkResponse: String?) : JsonObject {
|
||||
if (networkResponse != null && ! networkResponse.isBlank()) {
|
||||
val jo = JsonParser().parse(response).asJsonObject
|
||||
return jo
|
||||
class JCenterResponse(val jo: JsonObject?, val errorMessage: String?)
|
||||
|
||||
fun parseResponse(r: Response) : JCenterResponse {
|
||||
val networkResponse = r.networkResponse()
|
||||
if (networkResponse.code() != 200) {
|
||||
return JCenterResponse(null, networkResponse.message())
|
||||
} else {
|
||||
return JsonParser().parse(response).asJsonObject
|
||||
return JCenterResponse(JsonParser().parse(r.body().string()).asJsonObject, null)
|
||||
}
|
||||
// return Parser().parse(ByteArrayInputStream(response.toByteArray(Charset.defaultCharset()))) as JsonObject
|
||||
}
|
||||
|
||||
// fun getPackage() : JCenterPackage {
|
||||
|
@ -61,13 +62,13 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
|||
fun packageExists(packageName: String) : Boolean {
|
||||
val url = arrayListOf(UnauthenticatedJCenterApi.BINTRAY_URL_API, "packages", username!!, "maven", packageName)
|
||||
.joinToString("/")
|
||||
val response = http.get(username, password, url).getAsString()
|
||||
val jo = parseResponse(response, null)
|
||||
val jcResponse = parseResponse(http.get(username, password, url))
|
||||
|
||||
jo.get("message")?.let {
|
||||
throw KobaltException("Error from JCenter: $it")
|
||||
if (jcResponse.errorMessage != null) {
|
||||
throw KobaltException("Error from JCenter: ${jcResponse.errorMessage}")
|
||||
}
|
||||
return jo.get("name").asString == packageName
|
||||
|
||||
return jcResponse.jo!!.get("name").asString == packageName
|
||||
}
|
||||
|
||||
// class ForPost(val name: String, val license: Array<String>)
|
||||
|
@ -163,8 +164,8 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
|||
success = { r: Response -> results.add(true) },
|
||||
error = { r: Response ->
|
||||
results.add(false)
|
||||
val jo = parseResponse(r.body().string(), r.networkResponse().body().string())
|
||||
errorMessages.add(jo.get("message").asString ?: "No message found")
|
||||
val jcResponse = parseResponse(r)
|
||||
errorMessages.add(jcResponse.errorMessage!!)
|
||||
})
|
||||
val end = if (i >= fileCount) "\n" else ""
|
||||
log(1, " Uploading " + (i++) + " / $fileCount " + dots(fileCount, results) + end, false)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue