Skip to content

Commit

Permalink
Customizable OkHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhandwell committed Jul 29, 2019
1 parent 7cfcf3e commit 850e552
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
}

group 'com.github.joelhandwell'
version '1.1.0'
version '1.2.0'

repositories {
jcenter()
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/com/github/joelhandwell/populi/client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import retrofit2.http.POST
import com.github.joelhandwell.populi.Populi.CustomFieldType.*
import com.github.joelhandwell.populi.jaxb.PopuliResponseConverterFactory
import com.github.joelhandwell.populi.jaxb.spaceDelimitedLocalDateTimeFormatter
import okhttp3.OkHttpClient
import retrofit2.http.FieldMap
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -31,19 +32,29 @@ class Populi(
private var password: String? = null
private var accessKey: String? = null
private var baseUrl: String? = null
private var client: OkHttpClient? = null

fun withUsername(username: String) = apply { this.username = username }
fun withPassword(password: String) = apply { this.password = password }
fun withAccessKey(accessKey: String) = apply { this.accessKey = accessKey }
fun withBaseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
fun withClient(client: OkHttpClient) = apply { this.client = client }

fun build(): Populi {

val builder = Retrofit.Builder().baseUrl(baseUrl ?: throw RuntimeException("baseUrl is null"))
val builderWithBaseUrl = Retrofit.Builder().baseUrl(baseUrl ?: throw RuntimeException("baseUrl is null"))

val builderWithClient = if (client == null) {
builderWithBaseUrl
} else {
builderWithBaseUrl.client(client)
}

val builderWithConverterFactory = builderWithClient
.addConverterFactory(PopuliResponseConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())

val api = builder.build().create(PopuliApi::class.java)
val api = builderWithConverterFactory.build().create(PopuliApi::class.java)

if (accessKey == null) {
log.info("fetching accessKey with username and password")
Expand Down
16 changes: 16 additions & 0 deletions src/test/kotlin/com/github/joelhandwell/populi/ClientSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import java.time.LocalDateTime
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient



object ClientSpec : Spek({

Expand Down Expand Up @@ -42,6 +46,18 @@ object ClientSpec : Spek({
assertDegrees(degrees)
}

it("creates api client with custom OkHttpClient"){
val client = OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS).build()

Populi.Builder()
.withBaseUrl("http://localhost")
.withAccessKey("key")
.withClient(client)
.build()
}

it("send request, receive response and parse it into Degrees") {
stubForPopuli("getDegrees", getDegreesXml)
val degrees = populi.getDegrees()
Expand Down

0 comments on commit 850e552

Please sign in to comment.