Skip to content

Commit

Permalink
refactor(client): http request methods
Browse files Browse the repository at this point in the history
chore: unknown commit message
  • Loading branch information
stainless-bot authored and Stainless Bot committed Nov 5, 2024
1 parent a72d1ea commit c6089f1
Show file tree
Hide file tree
Showing 23 changed files with 254 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.openai.core.http

import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimap
import com.google.common.collect.MultimapBuilder
import com.openai.core.toImmutable

Expand All @@ -11,13 +10,13 @@ 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 @@ -39,10 +39,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("batches")
.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 @@ -69,10 +69,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("batches", 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 All @@ -99,10 +99,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("batches")
.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 @@ -133,10 +133,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("batches", params.getPathParam(0), "cancel")
.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
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("completions")
.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 @@ -36,10 +36,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("embeddings")
.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 @@ -39,10 +39,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("files", 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 All @@ -69,10 +69,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("files")
.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 @@ -99,10 +99,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.DELETE)
.addPathSegments("files", 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("images", "generations")
.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 @@ -42,10 +42,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("models", 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 @@ -75,10 +75,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("models")
.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.DELETE)
.addPathSegments("models", 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegments("moderations")
.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

0 comments on commit c6089f1

Please sign in to comment.