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 com.squareup.okhttp.*
|
||||||
import retrofit.mime.TypedFile
|
import retrofit.mime.TypedFile
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class Http {
|
public class Http {
|
||||||
class Body(val body: ResponseBody, val code: Int) {
|
public fun get(user: String?, password: String?, url: String) : Response {
|
||||||
public fun getAsString() : String {
|
|
||||||
return body.string()
|
|
||||||
}
|
|
||||||
public fun getAsStream() : InputStream {
|
|
||||||
return body.byteStream()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun get(user: String?, password: String?, url: String) : Body {
|
|
||||||
val client = OkHttpClient();
|
val client = OkHttpClient();
|
||||||
val request = Request.Builder().url(url)
|
val request = Request.Builder().url(url)
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
|
@ -28,16 +18,13 @@ public class Http {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val response = client.newCall(request.build()).execute()
|
return client.newCall(request.build()).execute()
|
||||||
return Body(response.body(), response.code())
|
|
||||||
} catch(ex: IOException) {
|
} catch(ex: IOException) {
|
||||||
throw KobaltException("Could not load URL $url, error: " + ex.message, ex)
|
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) : Response {
|
||||||
|
|
||||||
public fun get(url: String) : Body {
|
|
||||||
return get(null, null, url)
|
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"
|
const val BINTRAY_URL_API_CONTENT = BINTRAY_URL_API + "/content"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseResponse(response: String, networkResponse: String?) : JsonObject {
|
class JCenterResponse(val jo: JsonObject?, val errorMessage: String?)
|
||||||
if (networkResponse != null && ! networkResponse.isBlank()) {
|
|
||||||
val jo = JsonParser().parse(response).asJsonObject
|
fun parseResponse(r: Response) : JCenterResponse {
|
||||||
return jo
|
val networkResponse = r.networkResponse()
|
||||||
|
if (networkResponse.code() != 200) {
|
||||||
|
return JCenterResponse(null, networkResponse.message())
|
||||||
} else {
|
} 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 {
|
// fun getPackage() : JCenterPackage {
|
||||||
|
@ -61,13 +62,13 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
||||||
fun packageExists(packageName: String) : Boolean {
|
fun packageExists(packageName: String) : Boolean {
|
||||||
val url = arrayListOf(UnauthenticatedJCenterApi.BINTRAY_URL_API, "packages", username!!, "maven", packageName)
|
val url = arrayListOf(UnauthenticatedJCenterApi.BINTRAY_URL_API, "packages", username!!, "maven", packageName)
|
||||||
.joinToString("/")
|
.joinToString("/")
|
||||||
val response = http.get(username, password, url).getAsString()
|
val jcResponse = parseResponse(http.get(username, password, url))
|
||||||
val jo = parseResponse(response, null)
|
|
||||||
|
|
||||||
jo.get("message")?.let {
|
if (jcResponse.errorMessage != null) {
|
||||||
throw KobaltException("Error from JCenter: $it")
|
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>)
|
// 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) },
|
success = { r: Response -> results.add(true) },
|
||||||
error = { r: Response ->
|
error = { r: Response ->
|
||||||
results.add(false)
|
results.add(false)
|
||||||
val jo = parseResponse(r.body().string(), r.networkResponse().body().string())
|
val jcResponse = parseResponse(r)
|
||||||
errorMessages.add(jo.get("message").asString ?: "No message found")
|
errorMessages.add(jcResponse.errorMessage!!)
|
||||||
})
|
})
|
||||||
val end = if (i >= fileCount) "\n" else ""
|
val end = if (i >= fileCount) "\n" else ""
|
||||||
log(1, " Uploading " + (i++) + " / $fileCount " + dots(fileCount, results) + end, false)
|
log(1, " Uploading " + (i++) + " / $fileCount " + dots(fileCount, results) + end, false)
|
||||||
|
@ -172,7 +173,7 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
||||||
if (errorMessages.isEmpty()) {
|
if (errorMessages.isEmpty()) {
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
} else {
|
} else {
|
||||||
error("Errors while uploading:\n" + errorMessages.map { " $it" }.joinToString("\n"))
|
error(" Errors while uploading:\n" + errorMessages.map { " $it" }.joinToString("\n"))
|
||||||
return TaskResult(false, errorMessages.joinToString("\n"))
|
return TaskResult(false, errorMessages.joinToString("\n"))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue