Skip to content

Commit

Permalink
Merge pull request #299 from chapa/master
Browse files Browse the repository at this point in the history
Allow users to provide Guzzle options object requests
  • Loading branch information
haphan committed Oct 28, 2020
2 parents 2bbe999 + 862c51d commit e6d8d32
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/services/object-store/v1/objects.rst
Expand Up @@ -32,6 +32,9 @@ Download an object
As you will notice, a Stream_ object is returned by this call. For more information about dealing with streams, please
consult `Guzzle's docs`_.

By default, the whole body of the object is fetched before the function returns, set the ``'requestOptions'`` key of
parameter ``$data`` to ``['stream' => true]`` to get the stream before the end of download.

.. _Stream: https://github.com/guzzle/streams/blob/master/src/Stream.php
.. _Guzzle's docs: https://guzzle.readthedocs.org/en/5.3/streams.html

Expand Down
19 changes: 19 additions & 0 deletions samples/objectstore/v1/objects/download_stream.php
@@ -0,0 +1,19 @@
<?php

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
'authUrl' => '{authUrl}',
'region' => '{region}',
'user' => [
'id' => '{userId}',
'password' => '{password}'
],
'scope' => ['project' => ['id' => '{projectId}']]
]);

/** @var \GuzzleHttp\Stream\Stream $stream */
$stream = $openstack->objectStoreV1()
->getContainer('{containerName}')
->getObject('{objectName}')
->download(['requestOptions' => ['stream' => true]]);
4 changes: 4 additions & 0 deletions src/Common/Api/OperatorTrait.php
Expand Up @@ -112,6 +112,10 @@ protected function sendRequest(Operation $operation, array $userValues = [], boo

$uri = Utils::uri_template($operation->getPath(), $userValues);

if (array_key_exists('requestOptions', $userValues)) {
$options += $userValues['requestOptions'];
}

return $this->client->$method($operation->getMethod(), $uri, $options);
}

Expand Down
5 changes: 3 additions & 2 deletions src/ObjectStore/v1/Models/StorageObject.php
Expand Up @@ -133,8 +133,9 @@ public function retrieve()
/**
* This call will perform a `GET` HTTP request for the given object and return back its content in the form of a
* Guzzle Stream object. Downloading an object will transfer all of the content for an object, and is therefore
* distinct from fetching its metadata (a `HEAD` request). The body of an object is not fetched by default to
* improve performance when handling large objects.
* distinct from fetching its metadata (a `HEAD` request). The whole body of the object is fetched before the
* function returns, set the `'requestOptions'` key of {@param $data} to `['stream' => true]` to get the stream
* before the end of download.
*
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::getObject}
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/ObjectStore/v1/CoreTest.php
Expand Up @@ -153,6 +153,17 @@ public function objects()
$this->assertInstanceOf(StreamInterface::class, $stream);
$this->assertEquals(1000, $stream->getSize());

$this->logStep('Downloading object using streaming');
/** @var StreamInterface $stream */
require_once $this->sampleFile($replacements, 'objects/download_stream.php');
$this->assertInstanceOf(StreamInterface::class, $stream);

$body = '';
while (!$stream->eof()) {
$body .= $stream->read(64);
}
$this->assertEquals(1000, strlen($body));

$this->logStep('Get object');
require_once $this->sampleFile($replacements, 'objects/get.php');

Expand Down
11 changes: 11 additions & 0 deletions tests/unit/Common/Api/OperatorTraitTest.php
Expand Up @@ -105,6 +105,17 @@ public function test_it_populates_models_from_arrays()
$data = ['flavor' => [], 'image' => []];
$this->assertInstanceOf(ResourceInterface::class, $this->operator->model(TestResource::class, $data));
}

public function test_guzzle_options_are_forwarded()
{
$this->client->request('GET', 'test', ['headers' => [], 'stream' => true])->willReturn(new Response());

$this->operator->execute($this->def, [
'requestOptions' => ['stream' => true]
]);

$this->addToAssertionCount(1);
}
}

class TestResource extends AbstractResource
Expand Down

0 comments on commit e6d8d32

Please sign in to comment.