swift-openapi-urlsession icon indicating copy to clipboard operation
swift-openapi-urlsession copied to clipboard

Background upload / download using URLSession transport

Open hivenet-erwanjestin opened this issue 2 years ago • 5 comments

We are heavily using openapi to generate our REST library. In this library we do files upload and download.

Everything works pretty well but now we want to allow background requests. That means we need to use an URLSession with URLSessionConfiguration.background().

I saw we could override the URLSession but the most challenging part is we need a dedicate delegate for each API we have running in the background.

What's the best way to manage that with an openapi generated library? For now, we are considering doing it manually, so removing this library from our code :'(

hivenet-erwanjestin avatar Sep 13 '23 13:09 hivenet-erwanjestin

Hi @hivenet-erwanjestin,

I think it should be possible to make this work with the generated client. I can't offer much support on using background tasks with URLSession, but how you can proceed:

  • fork the URLSession transport repo only: https://github.com/apple/swift-openapi-urlsession
  • provide the forked version to your Client(...) initializer
  • see what changes you need to make to make background tasks work

After you have it working, you could contribute your changes back to https://github.com/apple/swift-openapi-urlsession so that it also works for others using background tasks.

But you should be able to use the generator in this repo, and the runtime library without any modifications, all the changes should be isolated to the URLSession transport.

czechboy0 avatar Sep 13 '23 14:09 czechboy0

+1 would be nice to have background support

mooshee avatar Aug 27 '24 13:08 mooshee

This is somewhat tricky since background sessions work with files instead of data streams. The code needs to be changed to upload a file or download a file, and per-task delegate is not available with background sessions.

guoye-zhang avatar Sep 16 '24 22:09 guoye-zhang

Good points. Yeah I imagine that if background tasks are to be supported here, we need a proposed design of how it'd all work. Let's just use this issue to track any progress on that, ideally driven by someone who has a use case here.

czechboy0 avatar Sep 17 '24 07:09 czechboy0

What exactly do you need from a use-case perspective? We need to support large background image and video uploads from a file.

mooshee avatar Mar 17 '25 16:03 mooshee