1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00
kobalt/src/main/kotlin/com/beust/kobalt/maven/Http.kt
2015-10-14 00:21:54 -07:00

80 lines
2.5 KiB
Kotlin

package com.beust.kobalt.maven
import com.beust.kobalt.misc.log
import com.squareup.okhttp.*
import java.io.File
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 {
val client = OkHttpClient();
val request = Request.Builder().url(url)
if (user != null) {
request.header("Authorization", Credentials.basic(user, password))
}
try {
val response = client.newCall(request.build()).execute()
return Body(response.body(), response.code())
} catch(ex: IOException) {
throw KobaltException("Could not load URL ${url}, error: " + ex.getMessage(), ex)
}
}
private val MEDIA_TYPE_BINARY = MediaType.parse("application/octet-stream")
public fun get(url: String) : Body {
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
}
public fun uploadFile(user: String?, password: String?, url: String, file: File,
success: (Response) -> Unit,
error: (Response) -> Unit) {
val request = builder(user, password)
.url(url)
.put(RequestBody.create(MEDIA_TYPE_BINARY, file))
.build()
val response = OkHttpClient().newCall(request).execute()
if (! response.isSuccessful) {
error(response)
} else {
success(response)
}
}
private val JSON = MediaType.parse("application/json; charset=utf-8")
fun post(user: String?, password: String?, url: String, payload: String) : String {
val request = builder(user, password)
.url(url)
.post(RequestBody.create(JSON, payload))
.build()
val response = OkHttpClient().newCall(request).execute()
return response.body().string()
}
}
class KobaltException(s: String? = null, ex: Throwable? = null) : RuntimeException(s, ex) {
constructor(ex: Throwable?) : this(null, ex)
}