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

ktor2 extension module new HttpResponse.bodyAsSourceReader() method #54

Closed
vanniktech opened this issue Aug 26, 2024 · 12 comments
Closed

Comments

@vanniktech
Copy link

vanniktech commented Aug 26, 2024

So in the ktor2 extension module there are a few helper methods but the one I'm missing is:

io.ktor.client.statement.HttpResponse.bodyAsSourceReader(): com.fleeksoft.ksoup.io.SourceReader

I want to do all of the request handling myself but then when I get the response convert it to a SourceReader so that I can use Ksoup to parse it.

I've already tried to do this myself with:

@file:OptIn(InternalAPI::class)

private suspend fun sourceReader(response: HttpResponse) =
    SourceReaderImpl(response.bodyAsChannel().readBuffer)

But I'm not getting past the error that InternalAPI has:

@RequiresOptIn(
    level = RequiresOptIn.Level.ERROR,
    message = "This API is internal in Ktor and should not be used. It could be removed or changed without notice."
)

which causes my build to fail. Are you using an older version of ktor or how are you getting past this restriction?

@itboy87
Copy link
Collaborator

itboy87 commented Aug 26, 2024

@vanniktech I don't ktor2 have any response.bodyAsChannel().readBuffer that is from ktor3

@vanniktech
Copy link
Author

Ktor does have a bodyAsChannel().readBuffer but it is internal.

@itboy87
Copy link
Collaborator

itboy87 commented Aug 26, 2024

Ktor does have a bodyAsChannel().readBuffer but it is internal.

@vanniktech I double checked, and Ktor 2 doesn't have the readBuffer field, it's only available in Ktor 3. Could you please provide me with a reference where is that in ktor 2?

@vanniktech
Copy link
Author

I see it in my IDE:

Screenshot 2024-08-26 at 22 15 30

But not in the GitHub file

Hint: My IDE is via a different dependency in a different module jumping to ktor3 sources, even though the module that I am in has no dependency to ktor3 :-( (I realized this just now.)

It should still be possible to have an extension method on HttpResponse to convert it into a SourceReader, right? ByteReadChannel exposes a bunch of different read methods.


I also know why I got confused now. I also saw you using it here:

Screenshot 2024-08-26 at 22 18 32

and for some reason I thought com.fleeksoft.ksoup.network is the shared module, but it is not:

Screenshot 2024-08-26 at 22 21 12

I got confused that the ktor3 modules have no suffix, where as the ktor2 modules to have suffixes. Ideally, also the ktor3 modules would have their suffix; so that the name is explicit and there's parity with the properly named korlibs modules.

@itboy87
Copy link
Collaborator

itboy87 commented Aug 27, 2024

@vanniktech Ktor2 is in a different branch called ‘ktor2’. I tried to use ReadByteChannel to create a SourceReader, but the main problem is that all of its functions are suspended, which broke a lot of the library code. Under the hood, it uses some kind of memory classes that are not easy to work with.

@vanniktech
Copy link
Author

Hmm then never mind, I heard somewhere that ktor3 is close to cutting a RC1 version so it should be stable soon.

@itboy87
Copy link
Collaborator

itboy87 commented Aug 31, 2024

@vanniktech  I’m closing this as I don’t have any plans to implement SourceReader for ByteChannel for Ktor 2. I suggest upgrading to Ktor 3

@itboy87 itboy87 closed this as completed Aug 31, 2024
@vanniktech
Copy link
Author

Could you provide a HttpResponse.bodyAsSourceReader() for ktor3? I don't think that exists there.

@itboy87
Copy link
Collaborator

itboy87 commented Sep 1, 2024

Its not extension function yet but you can do it like

SourceReader.from(httpResponse.bodyAsChannel().readBuffer)

@itboy87
Copy link
Collaborator

itboy87 commented Sep 1, 2024

Hi @vanniktech Sorry for the confusion. In the upcoming 0.1.6 version, you can use SourceReader.from(httpResponse.bodyAsChannel().readBuffer). In 0.1.6-alpha01, you can achieve this using SourceReaderImpl(httpResponse.bodyAsChannel().readBuffer). However, in 0.1.6, SourceReaderImpl will become internal, and you'll need to use SourceReader.from. But adding ext func HttpResponse .bodyAsSourceReader is also goo d idea i will add that too.

@itboy87 itboy87 reopened this Sep 1, 2024
@vanniktech
Copy link
Author

But adding ext func HttpResponse .bodyAsSourceReader is also goo d idea i will add that too.

Amazing, thanks!

@itboy87
Copy link
Collaborator

itboy87 commented Sep 4, 2024

HttpResponse.asSourceReader() added in version 0.1.6. Thanks

@itboy87 itboy87 closed this as completed Sep 4, 2024
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

No branches or pull requests

2 participants