Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ get a map of untyped fields of type `Map<String, JsonValue>`. You can then acces
`._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class
to extract it to a desired type.

## Logging

We use the standard [OkHttp logging interceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor).

You can enable logging by setting the environment variable `ONEBUSAWAY_SDK_LOG` to `info`.

```sh
$ export ONEBUSAWAY_SDK_LOG=info
```

Or to `debug` for more verbose logging.

```sh
$ export ONEBUSAWAY_SDK_LOG=debug
```

## Semantic versioning

This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
Expand Down
3 changes: 1 addition & 2 deletions onebusaway-sdk-java-client-okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ plugins {
dependencies {
api(project(":onebusaway-sdk-java-core"))

implementation("com.google.guava:guava:33.0.0-jre")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

testImplementation(kotlin("test"))
testImplementation("org.assertj:assertj-core:3.25.3")
testImplementation("org.slf4j:slf4j-simple:2.0.12")
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.onebusaway.client.okhttp

import com.google.common.collect.ListMultimap
import com.google.common.collect.MultimapBuilder
import java.io.IOException
import java.io.InputStream
import java.net.Proxy
import java.time.Duration
import java.util.concurrent.CompletableFuture
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType
Expand All @@ -18,8 +15,10 @@ import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import okio.BufferedSink
import org.onebusaway.core.RequestOptions
import org.onebusaway.core.http.Headers
import org.onebusaway.core.http.HttpClient
import org.onebusaway.core.http.HttpMethod
import org.onebusaway.core.http.HttpRequest
Expand All @@ -32,14 +31,28 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
HttpClient {

private fun getClient(requestOptions: RequestOptions): okhttp3.OkHttpClient {
val timeout = requestOptions.timeout ?: return okHttpClient
return okHttpClient
.newBuilder()
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30))
.build()
val clientBuilder = okHttpClient.newBuilder()

val logLevel =
when (System.getenv("ONEBUSAWAY_SDK_LOG")?.lowercase()) {
"info" -> HttpLoggingInterceptor.Level.BASIC
"debug" -> HttpLoggingInterceptor.Level.BODY
else -> null
}
if (logLevel != null) {
clientBuilder.addNetworkInterceptor(HttpLoggingInterceptor().setLevel(logLevel))
}

val timeout = requestOptions.timeout
if (timeout != null) {
clientBuilder
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.callTimeout(if (timeout.seconds == 0L) timeout else timeout.plusSeconds(30))
}

return clientBuilder.build()
}

override fun execute(
Expand Down Expand Up @@ -95,7 +108,9 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
}

val builder = Request.Builder().url(toUrl()).method(method.name, body)
headers.forEach(builder::header)
headers.names().forEach { name ->
headers.values(name).forEach { builder.header(name, it) }
}

return builder.build()
}
Expand All @@ -107,7 +122,9 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val

val builder = baseUrl.newBuilder()
pathSegments.forEach(builder::addPathSegment)
queryParams.forEach(builder::addQueryParameter)
queryParams.keys().forEach { key ->
queryParams.values(key).forEach { builder.addQueryParameter(key, it) }
}

return builder.toString()
}
Expand All @@ -133,21 +150,18 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
return object : HttpResponse {
override fun statusCode(): Int = code

override fun headers(): ListMultimap<String, String> = headers
override fun headers(): Headers = headers

override fun body(): InputStream = body!!.byteStream()

override fun close() = body!!.close()
}
}

private fun Headers.toHeaders(): ListMultimap<String, String> {
val headers =
MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
.arrayListValues()
.build<String, String>()
forEach { pair -> headers.put(pair.first, pair.second) }
return headers
private fun okhttp3.Headers.toHeaders(): Headers {
val headersBuilder = Headers.builder()
forEach { (name, value) -> headersBuilder.put(name, value) }
return headersBuilder.build()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import java.time.Duration
import org.onebusaway.client.OnebusawaySdkClient
import org.onebusaway.client.OnebusawaySdkClientImpl
import org.onebusaway.core.ClientOptions
import org.onebusaway.core.http.Headers
import org.onebusaway.core.http.QueryParams

class OnebusawaySdkOkHttpClient private constructor() {

Expand Down Expand Up @@ -36,6 +38,8 @@ class OnebusawaySdkOkHttpClient private constructor() {

fun clock(clock: Clock) = apply { clientOptions.clock(clock) }

fun headers(headers: Headers) = apply { clientOptions.headers(headers) }

fun headers(headers: Map<String, Iterable<String>>) = apply {
clientOptions.headers(headers)
}
Expand All @@ -46,6 +50,8 @@ class OnebusawaySdkOkHttpClient private constructor() {
clientOptions.putHeaders(name, values)
}

fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }

fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.putAllHeaders(headers)
}
Expand All @@ -58,6 +64,8 @@ class OnebusawaySdkOkHttpClient private constructor() {
clientOptions.replaceHeaders(name, values)
}

fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }

fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllHeaders(headers)
}
Expand All @@ -66,6 +74,8 @@ class OnebusawaySdkOkHttpClient private constructor() {

fun removeAllHeaders(names: Set<String>) = apply { clientOptions.removeAllHeaders(names) }

fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }

fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.queryParams(queryParams)
}
Expand All @@ -78,6 +88,10 @@ class OnebusawaySdkOkHttpClient private constructor() {
clientOptions.putQueryParams(key, values)
}

fun putAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.putAllQueryParams(queryParams)
}

fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.putAllQueryParams(queryParams)
}
Expand All @@ -90,6 +104,10 @@ class OnebusawaySdkOkHttpClient private constructor() {
clientOptions.replaceQueryParams(key, values)
}

fun replaceAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}

fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import java.time.Duration
import org.onebusaway.client.OnebusawaySdkClientAsync
import org.onebusaway.client.OnebusawaySdkClientAsyncImpl
import org.onebusaway.core.ClientOptions
import org.onebusaway.core.http.Headers
import org.onebusaway.core.http.QueryParams

class OnebusawaySdkOkHttpClientAsync private constructor() {

Expand Down Expand Up @@ -36,6 +38,8 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {

fun clock(clock: Clock) = apply { clientOptions.clock(clock) }

fun headers(headers: Headers) = apply { clientOptions.headers(headers) }

fun headers(headers: Map<String, Iterable<String>>) = apply {
clientOptions.headers(headers)
}
Expand All @@ -46,6 +50,8 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
clientOptions.putHeaders(name, values)
}

fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }

fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.putAllHeaders(headers)
}
Expand All @@ -58,6 +64,8 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
clientOptions.replaceHeaders(name, values)
}

fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }

fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllHeaders(headers)
}
Expand All @@ -66,6 +74,8 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {

fun removeAllHeaders(names: Set<String>) = apply { clientOptions.removeAllHeaders(names) }

fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }

fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.queryParams(queryParams)
}
Expand All @@ -78,6 +88,10 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
clientOptions.putQueryParams(key, values)
}

fun putAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.putAllQueryParams(queryParams)
}

fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.putAllQueryParams(queryParams)
}
Expand All @@ -90,6 +104,10 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
clientOptions.replaceQueryParams(key, values)
}

fun replaceAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}

fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}
Expand Down
18 changes: 9 additions & 9 deletions onebusaway-sdk-java-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ plugins {
}

dependencies {
api("com.fasterxml.jackson.core:jackson-core:2.14.3")
api("com.fasterxml.jackson.core:jackson-databind:2.14.3")
api("com.google.guava:guava:33.0.0-jre")
api("com.fasterxml.jackson.core:jackson-core:2.18.1")
api("com.fasterxml.jackson.core:jackson-databind:2.18.1")

implementation("com.fasterxml.jackson.core:jackson-annotations:2.14.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.14.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.3")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.3")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.18.1")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.18.1")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.1")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.18.1")
implementation("org.apache.httpcomponents.core5:httpcore5:5.2.4")
implementation("org.apache.httpcomponents.client5:httpclient5:5.3.1")

testImplementation(kotlin("test"))
testImplementation(project(":onebusaway-sdk-java-client-okhttp"))
testImplementation("com.github.tomakehurst:wiremock-jre8:2.35.2")
testImplementation("org.assertj:assertj-core:3.25.3")
testImplementation("org.assertj:assertj-guava:3.25.3")
testImplementation("org.slf4j:slf4j-simple:2.0.12")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.3")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.3")
testImplementation("org.mockito:mockito-core:5.14.2")
testImplementation("org.mockito:mockito-junit-jupiter:5.14.2")
testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ constructor(
) : OnebusawaySdkClientAsync {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
else
clientOptions
.toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ constructor(
) : OnebusawaySdkClient {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
else
clientOptions
.toBuilder()
Expand Down
Loading