dart-lang / http

A composable API for making HTTP requests in Dart.
https://pub.dev/packages/http
BSD 3-Clause "New" or "Revised" License
1.02k stars 351 forks source link

Sharing CupertinoClient's URLSession #1002

Open kennylevinsen opened 1 year ago

kennylevinsen commented 1 year ago

I have a use-case where I want to:

  1. Use URLSession directly to run efficient large-file downloadTask's.
  2. Make API calls using the simplified interface.

I would like to use the same URLSession for both of these. One way would be to allow a CupertinoClient to be constructed from a URLSession, another to make the internal URLSession public.

brianquinlan commented 1 year ago

The problem is that CupertinoClient needs to control the URLSession delegate.

Why do you want to reuse the URLSession?

kennylevinsen commented 1 year ago

I am making many requests to a single service and would like to use the same connection pool and configuration. For many things I just use the general http interface, but I also need to use downloadTask which is not exposed outside URLSession.

The available options seem to be:

  1. Live with distinct URLSession's which seem suboptimal
  2. Only use URLSession directly, with the unfortunate effect of making everything platform specific
  3. Expand cupertino_http to have some convenience wrappers outside the currently standard http interface
  4. Allow sharing/using the URLSession one way or another

The problem is that CupertinoClient needs to control the URLSession delegate.

Ah, hmm. I suppose the newer async methods taking a URLSessionTaskDelegate would work better here. "Rewrite everything in Swift to use new async APIs" is a larger request than I intended to make though, so I will probably hack around it locally for now.

brianquinlan commented 4 months ago

(3) seems like a good option.

I wonder what a good API would look like? Using callbacks doesn't seem very Dart-y.