Skip to content

Commit

Permalink
Add resource method to Illuminate\Http\Client\Response
Browse files Browse the repository at this point in the history
  • Loading branch information
einar-hansen committed Aug 7, 2024
1 parent 0465644 commit 2f35c41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Client;

use ArrayAccess;
use GuzzleHttp\Psr7\StreamWrapper;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use LogicException;
Expand Down Expand Up @@ -107,6 +108,19 @@ public function collect($key = null)
return Collection::make($this->json($key));
}

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

/**
* Get a header from the response.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ public function testResponseObjectAsObject()
$this->assertSame('bar', $response->object()->result->foo);
}

public function testResponseCanBeReturnedAsResource()
{
$this->factory->fake([
'*' => ['result' => ['foo' => 'bar']],
]);

$response = $this->factory->get('http://foo.com/api');

$this->assertIsResource($response->resource());
$this->assertSame('{"result":{"foo":"bar"}}', stream_get_contents($response->resource()));
}

public function testResponseCanBeReturnedAsCollection()
{
$this->factory->fake([
Expand Down

0 comments on commit 2f35c41

Please sign in to comment.