Skip to content

Commit e6d8d32

Browse files
authored
Merge pull request #299 from chapa/master
Allow users to provide Guzzle options object requests
2 parents 2bbe999 + 862c51d commit e6d8d32

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

doc/services/object-store/v1/objects.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Download an object
3232
As you will notice, a Stream_ object is returned by this call. For more information about dealing with streams, please
3333
consult `Guzzle's docs`_.
3434

35+
By default, the whole body of the object is fetched before the function returns, set the ``'requestOptions'`` key of
36+
parameter ``$data`` to ``['stream' => true]`` to get the stream before the end of download.
37+
3538
.. _Stream: https://github.com/guzzle/streams/blob/master/src/Stream.php
3639
.. _Guzzle's docs: https://guzzle.readthedocs.org/en/5.3/streams.html
3740

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$openstack = new OpenStack\OpenStack([
6+
'authUrl' => '{authUrl}',
7+
'region' => '{region}',
8+
'user' => [
9+
'id' => '{userId}',
10+
'password' => '{password}'
11+
],
12+
'scope' => ['project' => ['id' => '{projectId}']]
13+
]);
14+
15+
/** @var \GuzzleHttp\Stream\Stream $stream */
16+
$stream = $openstack->objectStoreV1()
17+
->getContainer('{containerName}')
18+
->getObject('{objectName}')
19+
->download(['requestOptions' => ['stream' => true]]);

src/Common/Api/OperatorTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ protected function sendRequest(Operation $operation, array $userValues = [], boo
112112

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

115+
if (array_key_exists('requestOptions', $userValues)) {
116+
$options += $userValues['requestOptions'];
117+
}
118+
115119
return $this->client->$method($operation->getMethod(), $uri, $options);
116120
}
117121

src/ObjectStore/v1/Models/StorageObject.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ public function retrieve()
133133
/**
134134
* This call will perform a `GET` HTTP request for the given object and return back its content in the form of a
135135
* Guzzle Stream object. Downloading an object will transfer all of the content for an object, and is therefore
136-
* distinct from fetching its metadata (a `HEAD` request). The body of an object is not fetched by default to
137-
* improve performance when handling large objects.
136+
* distinct from fetching its metadata (a `HEAD` request). The whole body of the object is fetched before the
137+
* function returns, set the `'requestOptions'` key of {@param $data} to `['stream' => true]` to get the stream
138+
* before the end of download.
138139
*
139140
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::getObject}
140141
*/

tests/integration/ObjectStore/v1/CoreTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ public function objects()
153153
$this->assertInstanceOf(StreamInterface::class, $stream);
154154
$this->assertEquals(1000, $stream->getSize());
155155

156+
$this->logStep('Downloading object using streaming');
157+
/** @var StreamInterface $stream */
158+
require_once $this->sampleFile($replacements, 'objects/download_stream.php');
159+
$this->assertInstanceOf(StreamInterface::class, $stream);
160+
161+
$body = '';
162+
while (!$stream->eof()) {
163+
$body .= $stream->read(64);
164+
}
165+
$this->assertEquals(1000, strlen($body));
166+
156167
$this->logStep('Get object');
157168
require_once $this->sampleFile($replacements, 'objects/get.php');
158169

tests/unit/Common/Api/OperatorTraitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ public function test_it_populates_models_from_arrays()
105105
$data = ['flavor' => [], 'image' => []];
106106
$this->assertInstanceOf(ResourceInterface::class, $this->operator->model(TestResource::class, $data));
107107
}
108+
109+
public function test_guzzle_options_are_forwarded()
110+
{
111+
$this->client->request('GET', 'test', ['headers' => [], 'stream' => true])->willReturn(new Response());
112+
113+
$this->operator->execute($this->def, [
114+
'requestOptions' => ['stream' => true]
115+
]);
116+
117+
$this->addToAssertionCount(1);
118+
}
108119
}
109120

110121
class TestResource extends AbstractResource

0 commit comments

Comments
 (0)