Skip to content

Add Ktor HTTP data source module#3071

Open
k88936 wants to merge 16 commits intoandroidx:mainfrom
k88936:feat/ktor-datasource
Open

Add Ktor HTTP data source module#3071
k88936 wants to merge 16 commits intoandroidx:mainfrom
k88936:feat/ktor-datasource

Conversation

@k88936
Copy link

@k88936 k88936 commented Feb 14, 2026

Summary

Introduce a new lib-datasource-ktor module similar to libraries/datasource_okhttp, providing KtorDataSource as an alternative HTTP data source implementation using Ktor client to solve issue #1646 .

What changed

  • impl: androidx.media3.datasource.DataSource using ktor
  • test: a unit test for DataSourceContractTest. and a unit test for it own.
  • api.txt in the repo root
  • some gradle config for include this module

Why

I recent met the same problem for maintaining both a okhttp client and a ktor client, similar to #1646 (comment)

Copy link
Collaborator

@icbaker icbaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a few commits, and added a few comments too

@k88936 k88936 force-pushed the feat/ktor-datasource branch 3 times, most recently from c0f7038 to 919b31d Compare February 25, 2026 04:10
@k88936 k88936 requested a review from icbaker February 25, 2026 04:31
k88936 and others added 16 commits March 10, 2026 13:48
Introduce a new lib-datasource-ktor module similar to `libraries/datasource_okhttp`.
 Providing KtorDataSource as an alternative HTTP data source implementation using Ktor client.
In the past we have found Robolectric is not realistic enough for
testing fiddly edge cases in HTTP stacks, so running on an emulator with
real Android OS is better.
We can promote it to stable later
io.ktor.http.Headers it self is a case-insensitive map already
This reverts commit b51daad.

fix: failed test case , we not only need a case-insensitively stored map,
 we also need a case-insensitively look up map.
@icbaker icbaker force-pushed the feat/ktor-datasource branch from c6ae2f3 to ca7ae8c Compare March 10, 2026 13:57
if (dataSpec.httpBody != null) {
setBody(dataSpec.httpBody!!)
} else if (dataSpec.httpMethod == DataSpec.HTTP_METHOD_POST) {
setBody(ByteArray(0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

I realised we didn't have any contract tests around POST requests so I added some (5eb1259) and rebased this PR on top - but they all pass if I remove this line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some http clients/servers may treat no body or zero len body differently, , , , , I leave this for compatibility

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