Skip to content

Dynamic HTTP headers support #26

@ermadmi78

Description

@ermadmi78

HTTP headers are passed to the default adapter constructor and are used unchanged for each request. But there are situations when HTTP headers need to be generated before each request. For example, to refresh JWT token:

val context = cinemaContextOf(
    CinemaCompositeKtorAdapter(
        client,
        "http://localhost:8080/graphql",
        "ws://localhost:8080/subscriptions",
        object : CinemaMapper {
            override fun serialize(value: Any): String =
                mapper.writeValueAsString(value)

            override fun <T : Any> deserialize(content: String, contentType: KClass<T>): T =
                mapper.readValue(content, contentType.java)
        },
        mapOf("Authorization" to "Basic YWRtaW46YWRtaW4=") //todo How to refresh JWT token here?
    )
)

Let use suspendable lambda to generate HTTP headers before each request:

val context = cinemaContextOf(
    CinemaCompositeKtorAdapter(
        client,
        "http://localhost:8080/graphql",
        "ws://localhost:8080/subscriptions",
        object : CinemaMapper {
            override fun serialize(value: Any): String =
                mapper.writeValueAsString(value)

            override fun <T : Any> deserialize(content: String, contentType: KClass<T>): T =
                mapper.readValue(content, contentType.java)
        },
        { mapOf("Authorization" to "Basic YWRtaW46YWRtaW4=") } //todo Let use suspendable lambda here!
    )
)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions