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

Add async support #323

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Add async support #323

wants to merge 5 commits into from

Conversation

jtojnar
Copy link
Collaborator

@jtojnar jtojnar commented Apr 13, 2023

Unfortunately, it currently does not work because HttpFulfilledPromise is broken (does not allow to be converted to another promise type).

@jtojnar
Copy link
Collaborator Author

jtojnar commented Apr 13, 2023

I guess we could introduce a dependency on a concrete promise library such as https://github.com/reactphp/promise/tree/2.x

@j0k3r
Copy link
Owner

j0k3r commented Apr 13, 2023

I never worked on async/promise on PHP so I might not be able to properly review it. But will it force people to use async/promise or will they still be able to use the sync way like before?

@jtojnar
Copy link
Collaborator Author

jtojnar commented Apr 13, 2023

We can provide sync api by just calling wait() on the promise (that is what HttpClient::fetch() does.

@jtojnar
Copy link
Collaborator Author

jtojnar commented Apr 14, 2023

Tests depend on j0k3r/httplug-ssrf-plugin#10

@jtojnar jtojnar force-pushed the async branch 2 times, most recently from ade7370 to 5c6ba77 Compare April 18, 2023 12:53
@jtojnar
Copy link
Collaborator Author

jtojnar commented Apr 18, 2023

Will want to finish #324 first, since that way conflicts are easier to resolve.

@jtojnar jtojnar force-pushed the async branch 4 times, most recently from 4738de6 to 6b7495b Compare May 23, 2023 19:10
jtojnar added 5 commits March 16, 2024 00:09
It does not support async requests.
Previous versions broke async clients.
It will still be converted to a dual async/sync one by `PluginClient`.
The diff is ugly but I just:

- removed the try block
- moved the code after the catch blocks inside the first closure argument of `then`
- changed the catch blocks into `if`s with `instanceof` checks
- moved them to the second closure argument of `then`
- replaced `fetch` with `fetchAsync`

Unfortunately, HTTPlug’s `HttpFulfilledPromise` is broken (does not allow to be converted to another promise type and only supports resolving with `ResponseInterface`) so we need the ugly hack in `makePromise` to convert it into a promise object from a full-fledged promise library.
I considered ReactPHP’s promise library first but those promises are not natively waitable in version 2.0 and using custom busy-loop waiting or third-party wait (from choval/async) just seemed to block. In the end, I went with Guzzle’s promise library.
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