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
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ package com.braintrustdata.api.core.http
import com.braintrustdata.api.core.toImmutable
import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimap
import com.google.common.collect.MultimapBuilder

class HttpRequest
private constructor(
@get:JvmName("method") val method: HttpMethod,
@get:JvmName("url") val url: String?,
@get:JvmName("pathSegments") val pathSegments: List<String>,
@get:JvmName("queryParams") val queryParams: ListMultimap<String, String>,
@get:JvmName("headers") val headers: ListMultimap<String, String>,
@get:JvmName("queryParams") val queryParams: ListMultimap<String, String>,
@get:JvmName("body") val body: HttpRequestBody?,
) {

override fun toString(): String =
"HttpRequest{method=$method, pathSegments=$pathSegments, queryParams=$queryParams, headers=$headers, body=$body}"
"HttpRequest{method=$method, url=$url, pathSegments=$pathSegments, headers=$headers, queryParams=$queryParams, body=$body}"

companion object {
@JvmStatic fun builder() = Builder()
Expand All @@ -27,65 +26,93 @@ private constructor(

private var method: HttpMethod? = null
private var url: String? = null
private var pathSegments: MutableList<String> = ArrayList()
private var queryParams: ListMultimap<String, String> = ArrayListMultimap.create()
private var body: HttpRequestBody? = null
private var pathSegments: MutableList<String> = mutableListOf()
private var headers: ListMultimap<String, String> =
MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER).arrayListValues().build()
private var queryParams: ListMultimap<String, String> = ArrayListMultimap.create()
private var body: HttpRequestBody? = null

fun method(method: HttpMethod) = apply { this.method = method }

fun url(url: String) = apply { this.url = url }

fun addPathSegment(pathSegment: String) = apply { this.pathSegments.add(pathSegment) }
fun addPathSegment(pathSegment: String) = apply { pathSegments.add(pathSegment) }

fun addPathSegments(vararg pathSegments: String) = apply {
for (pathSegment in pathSegments) {
this.pathSegments.add(pathSegment)
}
this.pathSegments.addAll(pathSegments)
}

fun putQueryParam(name: String, value: String) = apply {
this.queryParams.replaceValues(name, listOf(value))
fun headers(headers: Map<String, Iterable<String>>) = apply {
this.headers.clear()
putAllHeaders(headers)
}

fun putQueryParams(name: String, values: Iterable<String>) = apply {
this.queryParams.replaceValues(name, values)
fun putHeader(name: String, value: String) = apply { headers.put(name, value) }

fun putHeaders(name: String, values: Iterable<String>) = apply {
headers.putAll(name, values)
}

fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
queryParams.forEach(this::putQueryParams)
fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
headers.forEach(::putHeaders)
}

fun putAllQueryParams(queryParams: Multimap<String, String>) = apply {
queryParams.asMap().forEach(this::putQueryParams)
fun replaceHeaders(name: String, value: String) = apply {
headers.replaceValues(name, listOf(value))
}

fun putHeader(name: String, value: String) = apply {
this.headers.replaceValues(name, listOf(value))
fun replaceHeaders(name: String, values: Iterable<String>) = apply {
headers.replaceValues(name, values)
}

fun putHeaders(name: String, values: Iterable<String>) = apply {
this.headers.replaceValues(name, values)
fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
headers.forEach(::replaceHeaders)
}

fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
headers.forEach(this::putHeaders)
fun removeHeaders(name: String) = apply { headers.removeAll(name) }

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

fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
this.queryParams.clear()
putAllQueryParams(queryParams)
}

fun putAllHeaders(headers: Multimap<String, String>) = apply {
headers.asMap().forEach(this::putHeaders)
fun putQueryParam(key: String, value: String) = apply { queryParams.put(key, value) }

fun putQueryParams(key: String, values: Iterable<String>) = apply {
queryParams.putAll(key, values)
}

fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
queryParams.forEach(::putQueryParams)
}

fun replaceQueryParams(key: String, value: String) = apply {
queryParams.replaceValues(key, listOf(value))
}

fun replaceQueryParams(key: String, values: Iterable<String>) = apply {
queryParams.replaceValues(key, values)
}

fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
queryParams.forEach(::replaceQueryParams)
}

fun removeQueryParams(key: String) = apply { queryParams.removeAll(key) }

fun removeAllQueryParams(keys: Set<String>) = apply { keys.forEach(::removeQueryParams) }

fun body(body: HttpRequestBody) = apply { this.body = body }

fun build(): HttpRequest =
HttpRequest(
checkNotNull(method) { "`method` is required but was not set" },
url,
pathSegments.toImmutable(),
queryParams.toImmutable(),
headers,
queryParams.toImmutable(),
body,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("v1", "acl")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand All @@ -75,10 +75,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("v1", "acl", params.getPathParam(0))
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
->
Expand Down Expand Up @@ -108,10 +108,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("v1", "acl")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
->
Expand All @@ -138,10 +138,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.DELETE)
.addPathSegments("v1", "acl", params.getPathParam(0))
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.apply { params.getBody().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand Down Expand Up @@ -171,10 +171,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("v1", "acl", "batch-update")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand All @@ -201,10 +201,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.DELETE)
.addPathSegments("v1", "acl")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("v1", "ai_secret")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand All @@ -75,10 +75,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("v1", "ai_secret", params.getPathParam(0))
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
->
Expand Down Expand Up @@ -108,10 +108,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.PATCH)
.addPathSegments("v1", "ai_secret", params.getPathParam(0))
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand Down Expand Up @@ -142,10 +142,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("v1", "ai_secret")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
->
Expand All @@ -172,10 +172,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.DELETE)
.addPathSegments("v1", "ai_secret", params.getPathParam(0))
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.apply { params.getBody().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand All @@ -202,10 +202,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.DELETE)
.addPathSegments("v1", "ai_secret")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand Down Expand Up @@ -235,10 +235,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.PUT)
.addPathSegments("v1", "ai_secret")
.putAllQueryParams(clientOptions.queryParams)
.putAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.putAllHeaders(params.getHeaders())
.putAllQueryParams(clientOptions.queryParams.asMap())
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand Down
Loading