Skip to content

Commit

Permalink
Merge pull request #1 from TelenorNorway/feature/header-support
Browse files Browse the repository at this point in the history
feat: Support header and multiple headers
  • Loading branch information
telenorpronstad authored Mar 22, 2024
2 parents 23512b0 + 8cb9d00 commit 5052b8d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/kotlin/no/telenor/kt/restclient/util/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.telenor.kt.restclient.util
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.client.RestClient
import org.springframework.web.client.RestClient.RequestBodySpec
import org.springframework.web.client.RestClient.RequestHeadersSpec
import org.springframework.web.client.RestClient.RequestHeadersUriSpec
import org.springframework.web.client.RestClientResponseException
Expand All @@ -13,10 +14,16 @@ import java.net.URI
// --- request body utility---

@Suppress("UNNECESSARY_NOT_NULL_ASSERTION")
fun RestClient.RequestBodySpec.json(value: Any) = this
fun RequestBodySpec.json(value: Any) = this
.body(value)
.contentType(MediaType.APPLICATION_JSON)!!

fun RequestHeadersSpec<*>.header(header: String, value: Any) : RequestHeadersSpec<*> = this
.header(header, value.toString())

fun RequestHeadersSpec<*>.headers(vararg headersWithValue: Pair<String, Any>) : RequestHeadersSpec<*> = this
.headers { httpHeaders -> headersWithValue.forEach { httpHeaders.add(it.first, it.second.toString()) }}

// --- response body utility ---

inline fun <reified T : Any> RequestHeadersSpec<*>.retrieveJsonOrNull(): T? = this
Expand Down

0 comments on commit 5052b8d

Please sign in to comment.