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

Http-client request result handler before serialization #163

Open
Tetraquark opened this issue Sep 27, 2021 · 2 comments
Open

Http-client request result handler before serialization #163

Tetraquark opened this issue Sep 27, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@Tetraquark
Copy link
Collaborator

Tetraquark commented Sep 27, 2021

There is a situation when optional additional handling of a content of a server response in generated API classes by moko-network-generator plugin is needed.
I need to pass some lambda or fun interface into constructor of *Impl classes. Then the lambda will be called after results receiving and before serialization process (here before 131 line):

return _json.decodeFromString(serializer, result)

Use case: HTTP-server responds with errors using HTTP status 200 and there is some data of the error in the request body. As I understand it, at the moment there is no way to handle such thing using Ktor features because of io.ktor.client.call.DoubleReceiveException.

@Tetraquark Tetraquark added the enhancement New feature or request label Sep 27, 2021
@Alex009
Copy link
Member

Alex009 commented Sep 27, 2021

@Tetraquark hi! i think your case can be solved by something like Logger ktor feature. in this feature all content received (for logs) and later it passed to next pipeline step (just as copy as i know)

@Alex009
Copy link
Member

Alex009 commented Oct 9, 2021

code that help:

scope.receivePipeline.intercept(HttpReceivePipeline.After) { response ->
    val charset = response.contentType()?.charset() ?: Charsets.UTF_8
    // Get body from content
    val body = response.content.readRemaining().readText(
        charset = charset
    )

    // Processing body
    val result = handleBody(body)
    if (result) {
        // Just proceed pipeline
        val byteChannel = ByteReadChannel(
            body.toByteArray(charset)
        )
        proceedWith(context.wrapWithContent(byteChannel).response)
    } else {
        throw Exception()
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants