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

Synchronous text, json, arraybuffer, blob functions #153

Closed
g105b opened this issue Jul 15, 2023 · 1 comment · Fixed by #155
Closed

Synchronous text, json, arraybuffer, blob functions #153

g105b opened this issue Jul 15, 2023 · 1 comment · Fixed by #155

Comments

@g105b
Copy link
Member

g105b commented Jul 15, 2023

Following off of #88 , this thought needs its own issue...

More often than not, I reach for PhpGt/Fetch only because it's my own library, and not because it's the best tool for the job. I hardly ever actually need to make an asynchronous HTTP call, but I still work with Fetch so that I'm eating my own dogfood.

But if I'm always using it for a simple HTTP request, and I always want the HTTP body without anything else, there should be helper functions on the Http class (and possibly exposed globally like with #88).

My idea would allow something like this:

$json = $http->fetchJson("https://api.example.com/getInfo");
echo "Hello, ", $json->getString("name");

Rather than the current approach:

$http->fetch("https://api.example.com/getInfo")
->then(function(Response $response) {
  return $response->json();
})->then(function(JsonObject $json) {
  echo "Hello, ", $json->getString("name");
});
@g105b g105b added this to Backlog in Overview via automation Jul 15, 2023
@g105b g105b moved this from Backlog to Planned in Overview Jul 15, 2023
@g105b
Copy link
Member Author

g105b commented Jan 30, 2024

The way this is achieved in JavaScript:

const response = await fetch("/api");
const jsonObject = await response.json();

This would be nice to replicate in PHP:

$response = $http->awaitFetch("/api");
$jsonObject = $response->awaitJson();

g105b added a commit that referenced this issue Jan 30, 2024
g105b added a commit that referenced this issue Jan 31, 2024
@g105b g105b mentioned this issue Jan 31, 2024
g105b added a commit that referenced this issue Jan 31, 2024
* feature: awaitFetch
closes #153

* tweak: param typehint

* feature: await methods, closes #153

* build: php 8.1 compatibility
Overview automation moved this from Planned to Done January 2024 Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Overview
Done January 2024
Development

Successfully merging a pull request may close this issue.

1 participant