mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
JCenter requires PUT, Github POST.
This commit is contained in:
parent
66c9d43074
commit
418ced38a8
3 changed files with 27 additions and 17 deletions
|
@ -41,14 +41,6 @@ public class Http {
|
|||
return get(null, null, url)
|
||||
}
|
||||
|
||||
private fun builder(user: String?, password: String?) : Request.Builder {
|
||||
val result = Request.Builder()
|
||||
user?.let {
|
||||
result.header("Authorization", Credentials.basic(user, password))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun percentProgressCallback(totalSize: Long) : (Long) -> Unit {
|
||||
return { num: Long ->
|
||||
val progress = num * 100 / totalSize
|
||||
|
@ -61,6 +53,7 @@ public class Http {
|
|||
}
|
||||
|
||||
public fun uploadFile(user: String? = null, password: String? = null, url: String, file: TypedFile,
|
||||
post: Boolean,
|
||||
progressCallback: (Long) -> Unit = {},
|
||||
headers: Headers = Headers.of(),
|
||||
success: (Response) -> Unit = {},
|
||||
|
@ -70,10 +63,18 @@ public class Http {
|
|||
fullHeaders.set("Content-Type", file.mimeType())
|
||||
headers.names().forEach { fullHeaders.set(it, headers.get(it)) }
|
||||
|
||||
val request = builder(user, password)
|
||||
user?.let {
|
||||
fullHeaders.set("Authorization", Credentials.basic(user, password))
|
||||
}
|
||||
|
||||
val requestBuilder = Request.Builder()
|
||||
.headers(fullHeaders.build())
|
||||
.url(url)
|
||||
.post(CountingFileRequestBody(file.file(), file.mimeType(), progressCallback))
|
||||
val request =
|
||||
(if (post)
|
||||
requestBuilder.post(CountingFileRequestBody(file.file(), file.mimeType(), progressCallback))
|
||||
else
|
||||
requestBuilder.put(CountingFileRequestBody(file.file(), file.mimeType(), progressCallback)))
|
||||
.build()
|
||||
|
||||
log(2, "Uploading $file to $url")
|
||||
|
|
|
@ -79,7 +79,7 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors,
|
|||
val url = "$strippedUrl?name=$fileName&label=$fileName"
|
||||
val headers = Headers.of("Authorization", "token $token")
|
||||
val totalSize = typedFile.file().length()
|
||||
http.uploadFile(url = url, file = typedFile, headers = headers,
|
||||
http.uploadFile(url = url, file = typedFile, headers = headers, post = true, // Github requires POST
|
||||
progressCallback = http.percentProgressCallback(totalSize))
|
||||
|
||||
return Observable.just(UploadAssetResponse(tagName, tagName))
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.beust.kobalt.plugin.publish
|
||||
|
||||
import com.beust.kobalt.KobaltException
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.TaskResult
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.maven.Gpg
|
||||
import com.beust.kobalt.maven.Http
|
||||
import com.beust.kobalt.maven.Md5
|
||||
|
@ -31,8 +31,13 @@ open public class UnauthenticatedJCenterApi @Inject constructor(open val http: H
|
|||
const val BINTRAY_URL_API_CONTENT = BINTRAY_URL_API + "/content"
|
||||
}
|
||||
|
||||
fun parseResponse(response: String) : JsonObject {
|
||||
fun parseResponse(response: String, networkResponse: String?) : JsonObject {
|
||||
if (networkResponse != null && ! networkResponse.isBlank()) {
|
||||
val jo = JsonParser().parse(response).asJsonObject
|
||||
return jo
|
||||
} else {
|
||||
return JsonParser().parse(response).asJsonObject
|
||||
}
|
||||
// return Parser().parse(ByteArrayInputStream(response.toByteArray(Charset.defaultCharset()))) as JsonObject
|
||||
}
|
||||
|
||||
|
@ -57,8 +62,11 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
|||
val url = arrayListOf(UnauthenticatedJCenterApi.BINTRAY_URL_API, "packages", username!!, "maven", packageName)
|
||||
.joinToString("/")
|
||||
val response = http.get(username, password, url).getAsString()
|
||||
val jo = parseResponse(response)
|
||||
val jo = parseResponse(response, null)
|
||||
|
||||
jo.get("message")?.let {
|
||||
throw KobaltException("Error from JCenter: $it")
|
||||
}
|
||||
return jo.get("name").asString == packageName
|
||||
}
|
||||
|
||||
|
@ -151,10 +159,11 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
|
|||
filesToUpload.forEach { file ->
|
||||
http.uploadFile(username, password, fileToPath(file) + optionPath,
|
||||
TypedFile(MediaType.ANY_APPLICATION_TYPE.toString(), file),
|
||||
post = false, // JCenter requires PUT
|
||||
success = { r: Response -> results.add(true) },
|
||||
error = { r: Response ->
|
||||
results.add(false)
|
||||
val jo = parseResponse(r.body().string())
|
||||
val jo = parseResponse(r.body().string(), r.networkResponse().body().string())
|
||||
errorMessages.add(jo.get("message").asString ?: "No message found")
|
||||
})
|
||||
val end = if (i >= fileCount) "\n" else ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue