Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose response headers in service result as a Pair #4

Merged
merged 2 commits into from
Feb 15, 2023

Conversation

darronschall
Copy link
Contributor

In creating #2, we allowed for sending arbitrary headers, such an ETag value for If-None-Match, or a value for Idempotency-Key.

This PR provides a way to read headers from the service result. The only way to capture the returned ETag value is to read the response headers, which was impossible prior to this PR.

There's also a small consistency change. Since we're using the Headers type in the result for the response, we change the request type to also be Headers to match. This changes the call site a bit since the headers should go through HeadersBuilder:

val requestHeaders = eTag?.let { Headers.build { append(HttpHeaders.IfNoneMatch, eTag) } }

Or, if sending multiple headers, something like:

val requestHeadersBuilder: HeadersBuilder = HeadersBuilder()
eTag?.let { requestHeadersBuilder.append(HttpHeaders.IfNoneMatch, it) }
// Could add more headers here as necessary
val requestHeaders = requestHeadersBuilder.build()

On the response side, use destructuring to get at the pair values:

val (response, responseHeaders) = exampleService.doSomethingOrOther(...)

... or, when you don't care about the response headers:

val (response, _) = exampleService.doSomething(...)

This allows callers to optionally read things like ETag cache keys, etc. For callers not interested in response headers, simply ignore the value:

```kotlin
val (response, _) = exampleService.doSomething(ExampleRequest())
```
This changes the call-site, but provides consistency with the `Headers` returned by the response `Pair`.
@darronschall darronschall merged commit 19e771c into main Feb 15, 2023
@darronschall darronschall deleted the expose-response-headers-in-service-result branch February 15, 2023 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants