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

feat(HTTPClient): Provide wrapped access to Guzzle's asyncRequest() #38613

Merged
merged 1 commit into from Jun 28, 2023

Conversation

nickvergessen
Copy link
Member

@nickvergessen nickvergessen commented Jun 2, 2023

Summary

Following the approach discussed in #35959 to allow making async requests, e.g. required for interaction with webhooks in a non-blocking way.

Sample usage

$client = $this->clientService->newClient();
$promise = $client->postAsync($webhook->getUrl(), $data);

$promise->then(function (IResponse $response) use ($webhook) {
	if ($response->getStatusCode() !== Http::STATUS_OK && $response->getStatusCode() !== Http::STATUS_ACCEPTED) {
		$this->logger->error('Webhook responded with unexpected status code, increasing error count');
	}
	$this->logger->debug('Webhook successfully executed');
}, function (RequestException $exception) {
	$this->logger->error('Webhook error occurred, increasing error count', ['exception' => $exception]);
});

Checklist

@nickvergessen nickvergessen added enhancement 2. developing Work in progress pending documentation This pull request needs an associated documentation update labels Jun 2, 2023
@nickvergessen nickvergessen added this to the Nextcloud 28 milestone Jun 2, 2023
@nickvergessen nickvergessen self-assigned this Jun 2, 2023
@nickvergessen
Copy link
Member Author

Tests and docs will follow after my vacation.
But could get the first round of reviews on the approach in the meantime 😎

@nickvergessen nickvergessen force-pushed the feat/35959/async-guzzle-requests branch from 8a73fc2 to f47135b Compare June 26, 2023 10:21
Copy link
Member

@ChristophWurst ChristophWurst left a comment

Choose a reason for hiding this comment

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

Looks good

lib/public/Http/Client/IPromise.php Outdated Show resolved Hide resolved
lib/public/Http/Client/IPromise.php Outdated Show resolved Hide resolved
lib/public/Http/Client/IPromise.php Outdated Show resolved Hide resolved
@nickvergessen
Copy link
Member Author

@come-nc @blizzz second review?

Comment on lines +63 to +69
if ($onFulfilled !== null) {
$wrappedOnFulfilled = static function (ResponseInterface $response) use ($onFulfilled) {
$onFulfilled(new Response($response));
};
} else {
$wrappedOnFulfilled = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What does that do?
And the wrapping around onRejected is even weirder as it seems to just pass on the call?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh it wraps the internal $response object in something from OCP, right? Then I get it, but I do not get the one for onRejected.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah it's about only exposing OCP.
Same basically on the rejection, we are removing the Guzzle "type hint" and just require a \Exception eating closure instead of a GuzzleHttp\Exception\RequestException one.

Signed-off-by: Joas Schilling <coding@schilljs.com>
@nickvergessen nickvergessen force-pushed the feat/35959/async-guzzle-requests branch from 8b13f2e to ec6728d Compare June 27, 2023 13:54
@nickvergessen
Copy link
Member Author

Rebased and squashed

@nickvergessen nickvergessen merged commit fbc63fe into master Jun 28, 2023
39 checks passed
@nickvergessen nickvergessen deleted the feat/35959/async-guzzle-requests branch June 28, 2023 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2. developing Work in progress enhancement pending documentation This pull request needs an associated documentation update
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Exposing GuzzleHttp\Client::asyncRequest()
4 participants