From 3c9c372ed1bac818331fdd0e380483cd030d8d88 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 3 May 2016 01:42:44 -0800 Subject: [PATCH] Going back to upload .pom manually. --- .../beust/kobalt/plugin/publish/BintrayApi.kt | 87 +++++++++++++------ 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt b/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt index 95728d6e..d93da574 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt @@ -12,24 +12,14 @@ import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn import com.google.gson.JsonArray import com.google.gson.JsonObject +import com.google.gson.JsonParser import com.google.inject.assistedinject.Assisted -import okhttp3.Credentials -import okhttp3.Interceptor -import okhttp3.MediaType -import okhttp3.MultipartBody -import okhttp3.OkHttpClient -import okhttp3.RequestBody +import okhttp3.* import retrofit2.Call import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -import retrofit2.http.Body -import retrofit2.http.GET +import retrofit2.http.* import retrofit2.http.Headers -import retrofit2.http.Multipart -import retrofit2.http.POST -import retrofit2.http.PUT -import retrofit2.http.Part -import retrofit2.http.Path import java.io.File import javax.annotation.Nullable import javax.inject.Inject @@ -44,6 +34,7 @@ class BintrayApi @Inject constructor(val http: Http, companion object { const val BINTRAY_URL_API = "https://api.bintray.com" + const val BINTRAY_URL_API_CONTENT = BINTRAY_URL_API + "/content" } interface IFactory { @@ -89,6 +80,9 @@ class BintrayApi @Inject constructor(val http: Http, init { val builder = OkHttpClient.Builder() +// .addInterceptor(HttpLoggingInterceptor().apply { +// level = HttpLoggingInterceptor.Level.BASIC +// }) builder.interceptors().add(Interceptor { chain -> var original = chain.request(); @@ -180,7 +174,8 @@ class BintrayApi @Inject constructor(val http: Http, fun dots(total: Int, list: List, file: File? = null): String { val spaces: String = Array(total - list.size, { " " }).joinToString("") - return "|" + list.map { if (it) "." else "X" }.joinToString("") + spaces + (if (file != null) "| [ $file ]" else "|") + return "|" + list.map { if (it) "." else "X" }.joinToString("") + spaces + + (if (file != null) "| [ $file ]" else "|") } val results = arrayListOf() @@ -189,22 +184,31 @@ class BintrayApi @Inject constructor(val http: Http, val body = MultipartBody.Part.createFormData("artifact", file.name, RequestBody.create(type, file)); - var upload = if (file.extension != "pom" ) { - service.uploadArtifact(org ?: username!!, project.name, + if (file.extension != "pom") { + val upload = service.uploadArtifact(org ?: username!!, project.name, project.group!!.replace('.', '/'), project.artifactId!!, project.version!!, file.name, body) + val result = upload.execute() + val error = result.errorBody()?.string() + if (result.errorBody() != null) { + errorMessages.add(error!!) + results.add(false) + } else { + results.add(true) + } } else { - service.uploadPom(org ?: username!!, project.name, project.group!!.replace('.', '/'), - project.artifactId!!, project.version!!, file.name, body) + http.uploadFile(username, password, fileToPath(project, file) + optionPath, + Http.TypedFile(com.google.common.net.MediaType.ANY_APPLICATION_TYPE.toString(), file), + post = false, // Bintray requires PUT + success = { r: Response -> results.add(true) }, + error = { r: Response -> + results.add(false) + val jcResponse = parseResponse(r) + errorMessages.add(jcResponse.errorMessage!!) + }) +// service.uploadPom(org ?: username!!, project.name, project.group!!.replace('.', '/'), +// project.artifactId!!, project.version!!, file.name, body) } - val result = upload.execute() - val error = result.errorBody()?.string() - if (result.errorBody() != null) { - errorMessages.add(error!!) - results.add(false) - } else { - results.add(true) - } log(1, " Uploading ${i + 1} / $fileCount " + dots(fileCount, results, file), false) } val success = results @@ -224,6 +228,37 @@ class BintrayApi @Inject constructor(val http: Http, } } + fun fileToPath(project: Project, f: File) : String { + return listOf( + BINTRAY_URL_API_CONTENT, + org ?: username!!, + "maven", + project.name, + project.version!!, + project.group!!.replace(".", "/"), + project.artifactId!!, + project.version!!, + f.name) + .joinToString("/") + } + + class BintrayResponse(val jo: JsonObject?, val errorMessage: String?) + + fun parseResponse(r: Response): BintrayResponse { + val networkResponse = r.networkResponse() + if (networkResponse.code() != 200) { + val message = networkResponse.message() + try { + val errorObject = JsonParser().parse(r.body().string()).asJsonObject + return BintrayResponse(null, message + ": " + errorObject.get("message").asString) + } catch(ex: Exception) { + return BintrayResponse(null, message) + } + } else { + return BintrayResponse(JsonParser().parse(r.body().string()).asJsonObject, null) + } + } + fun JsonObject.addNonNull(name: String, value: String?) { if (value != null) { addProperty(name, value);