Skip to content

Commit

Permalink
feat(http) add TokenMessageAuthorizationHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
DeborahPereira4sh committed Jul 18, 2024
1 parent cbfbdb5 commit c43e4aa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.izivia.ocpi.toolkit.common

import TokenMessageAuthorizationHeader
import com.fasterxml.jackson.core.type.TypeReference
import com.izivia.ocpi.toolkit.common.Header.OCPI_FROM_COUNTRY_CODE
import com.izivia.ocpi.toolkit.common.Header.OCPI_FROM_PARTY_ID
Expand Down Expand Up @@ -182,6 +183,7 @@ fun HttpRequest.messageRoutingHeaders(): RequestMessageRoutingHeaders =
fromCountryCode = headers.getByNormalizedKey(OCPI_FROM_COUNTRY_CODE)
)


/**
* It builds headers from a ResponseMessageRoutingHeaders
*/
Expand All @@ -195,6 +197,7 @@ private fun RequestMessageRoutingHeaders.httpHeaders(): Map<String, String> =
.filter { it.value != null }
.mapValues { it.value!! }


fun ResponseMessageRoutingHeaders.httpHeaders(): Map<String, String> =
mapOf(
OCPI_TO_PARTY_ID to toPartyId,
Expand All @@ -205,6 +208,14 @@ fun ResponseMessageRoutingHeaders.httpHeaders(): Map<String, String> =
.filter { it.value != null }
.mapValues { it.value!! }

/**
* It builds MessageAuthorizationHeaders from the headers of the request.
*/
fun HttpRequest.messageAuthorizationHeaders(): TokenMessageAuthorizationHeader =
TokenMessageAuthorizationHeader(
token = parseAuthorizationHeader()
)

/**
* For debugging issues, OCPI implementations are required to include unique IDs via HTTP headers in every
* request/response.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.coroutineContext

/**
* Contains context about the current token message header
*/
data class TokenMessageAuthorizationHeader(
val token: String? = null
) : AbstractCoroutineContextElement(TokenMessageAuthorizationHeader) {
companion object Key : CoroutineContext.Key<TokenMessageAuthorizationHeader>
}

/**
* Retrieves TokenMessageAuthorizationHeader in the current coroutine if it is found.
*/
suspend fun currentTokenMessageAuthorizationHeadersOrNull(): TokenMessageAuthorizationHeader? =
coroutineContext[TokenMessageAuthorizationHeader]

/**
* Retrieves TokenMessageAuthorizationHeader in the current coroutine, and throws IllegalStateException
* if it could not be found.
*/
suspend fun currentTokenMessageAuthorizationHeaders(): TokenMessageAuthorizationHeader =
coroutineContext[TokenMessageAuthorizationHeader]
?: throw IllegalStateException("No TokenMessageAuthorizationHeader object in current coroutine context")

0 comments on commit c43e4aa

Please sign in to comment.