Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
add bintray test client
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 18, 2020
1 parent 22090cc commit f2471c8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ out/
store/
intellij-settings/
local.properties
**/scratch.kt
74 changes: 74 additions & 0 deletions src/test/kotlin/Bintray.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import dev.forkhandles.result4k.Failure
import dev.forkhandles.result4k.Result
import dev.forkhandles.result4k.Success
import org.http4k.client.JavaHttpClient
import org.http4k.core.Body
import org.http4k.core.Credentials
import org.http4k.core.HttpHandler
import org.http4k.core.Method.GET
import org.http4k.core.Method.POST
import org.http4k.core.Request
import org.http4k.core.Status
import org.http4k.core.Uri
import org.http4k.core.then
import org.http4k.core.with
import org.http4k.filter.ClientFilters
import org.http4k.format.Jackson
import org.http4k.format.Jackson.auto
import java.time.Instant

data class Package(val owner: String, val packageName: String)

interface Bintray {
fun uploads(pkg: Package): Result<String, Status>
fun repo(owner: String): Result<String, Status>
fun org(owner: String): Result<String, Status>
fun packageGet(pkg: Package): Result<String, Status>

companion object
}

data class Downloads(val from: Instant, val to: Instant)

fun Bintray.Companion.Http(credentials: Credentials, httpHandler: HttpHandler = JavaHttpClient()) = object : Bintray {
private val http = ClientFilters.SetBaseUriFrom(Uri.of("https://api.bintray.com"))
.then(ClientFilters.BasicAuth(credentials))
.then(httpHandler)

override fun uploads(pkg: Package): Result<String, Status> {
val result = http(Request(POST, "/packages/${pkg.owner}/maven/${pkg.packageName}/stats/time_range_downloads"))
.with(Body.auto<Downloads>().toLens() of Downloads(Instant.now(), Instant.now()))

return when {
result.status.successful -> Success(result.bodyString())
else -> Failure(result.status)
}
}

override fun repo(owner: String): Result<String, Status> {
val result = http(Request(GET, "/repos/${owner}/maven"))

return when {
result.status.successful -> Success(Jackson.prettify(result.bodyString()))
else -> Failure(result.status)
}
}

override fun org(owner: String): Result<String, Status> {
val result = http(Request(GET, "/orgs/${owner}"))

return when {
result.status.successful -> Success(Jackson.prettify(result.bodyString()))
else -> Failure(result.status)
}
}

override fun packageGet(pkg: Package): Result<String, Status> {
val result = http(Request(GET, "/packages/${pkg.owner}/maven/${pkg.packageName}"))

return when {
result.status.successful -> Success(Jackson.prettify(result.bodyString()))
else -> Failure(result.status)
}
}
}

0 comments on commit f2471c8

Please sign in to comment.