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

[11.x] Add resource() method to Illuminate\Http\Client\Response #52412

Conversation

einar-hansen
Copy link
Contributor

This PR introduces a new resource() method to the Response class in Laravel. This method allows users to directly obtain a PHP stream resource from the response body, simplifying workflows that involve stream operations.

Motivation
Working with stream resources in Laravel, especially when dealing with external services or large datasets, can sometimes be verbose and require multiple steps. This new method aims to simplify this process, making it more intuitive and reducing the amount of boilerplate code needed.

Changes

  • Added a new resource() method to the Response class.
  • Implemented a corresponding test case to ensure the new functionality works as expected.

New Functionality
The resource() method returns a PHP stream resource from the response body. It uses the amazing GuzzleHttp\Psr7\StreamWrapper that comes with the guzzlehttp/psr7 package, that is required by the framework as a requirement in guzzlehttp/guzzle:

use GuzzleHttp\Psr7\StreamWrapper;

/**
 * Get the body of the response as a PHP stream resource.
 *
 * @return resource
 *
 * @throws \InvalidArgumentException if stream is not readable or writable
 */
public function resource()
{
    return StreamWrapper::getResource($this->response->getBody());
}

Usage Examples

Writing to S3

Before:

use GuzzleHttp\Psr7\StreamWrapper;

$response = Http::get($imageUrl);
Storage::disk('s3')->writeStream(
    'thumbnail.png',
    StreamWrapper::getResource($response->toPsrResponse()->getBody()),
);

After:

$response = Http::get($imageUrl);
Storage::disk('s3')->writeStream('thumbnail.png', $response->resource());

Streaming large JSON responses

My favorite way to work with JSON data from external api's, using halaxa/json-machine.

Before:

use GuzzleHttp\Psr7\StreamWrapper;
use JsonMachine\Items;

$response = Http::get('https://jsonplaceholder.typicode.com/todos/1');
$phpStream = StreamWrapper::getResource($response->toPsrResponse()->getBody());

foreach (Items::fromStream($phpStream) as $key => $value) {
    var_dump($value);
}

After:

use JsonMachine\Items;

$response = Http::get('https://jsonplaceholder.typicode.com/todos/1');

foreach (Items::fromStream($response->resource()) as $key => $value) {
    var_dump($value);
}

I'll make a PR to the docs to include information about the new resource() method.

@taylorotwell taylorotwell merged commit 56200cb into laravel:11.x Aug 7, 2024
29 checks passed
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