libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.49k stars 924 forks source link

Cancellable GetCallback and friends #916

Open ChrisDirkis opened 1 year ago

ChrisDirkis commented 1 year ago

Is your feature request related to a problem?

I'm trying to implement request cancellation for some requests done via GetCallback. Historically, we'd have a shared_ptr to a flag that's then checked in the callback, but I noticed that 1.10 had some cancellation semantics, so I was interested in using those instead. However, it appears that GetCallback and friends don't support cancellation, only MultiGetAsync and friends do.

Possible Solution

I'd like to see GetCallback be cancellable as MultiGetAsync is. It seems like cpr::async could return AsyncWrapper<T, true>, but the trivial change didn't compile and I haven't looked further into it.

Alternatively, I'd be happy to use MultiGetCallback with 1 request, if such existed.

Alternatives

For now, we can solve this on our end with some shared flag checked in the callback.

Additional Context

No response

COM8 commented 1 year ago

@ChrisDirkis Thanks for creating this feature request. I agree, this would be a nice feature. Performing an async request is minimal more expensive but definitely the way to go here. Else you could use the ProgressCallback and just return false based on a specific flag you store in your code. Docs: https://docs.libcpr.org/advanced-usage.html#setting-callbacks

I will add it onto the TODO list for our next release.