Skip to content

Commit

Permalink
add public URL method to object
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Hannaford committed Nov 24, 2015
1 parent bde994a commit 6bcf7ac
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Common/Api/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,12 @@ public function newInstance()
{
return new static($this->client, $this->api);
}
}

/**
* @return \GuzzleHttp\Psr7\Uri
*/
protected function getHttpBaseUrl()
{
return $this->client->getConfig('base_url');
}
}
15 changes: 15 additions & 0 deletions src/Common/Transport/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace OpenStack\Common\Transport;

use function GuzzleHttp\Psr7\uri_for;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

class Utils
{
Expand Down Expand Up @@ -59,4 +61,17 @@ public static function normalizeUrl($url)

return rtrim($url, '/') . '/';
}

/**
* Add an unlimited list of paths to a given URI.
*
* @param UriInterface $uri
* @param ...$paths
*
* @return UriInterface
*/
public static function addPaths(UriInterface $uri, ...$paths)
{
return uri_for(rtrim((string) $uri, '/') . '/' . implode('/', $paths));
}
}
13 changes: 12 additions & 1 deletion src/ObjectStore/v1/Models/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenStack\ObjectStore\v1\Models;

use OpenStack\Common\Transport\Utils;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use OpenStack\Common\Resource\AbstractResource;
Expand Down Expand Up @@ -56,10 +57,20 @@ public function populateFromResponse(ResponseInterface $response)
$this->metadata = $this->parseMetadata($response);
}

/**
* Retrieves the public URI for this resource.
*
* @return \GuzzleHttp\Psr7\Uri
*/
public function getPublicUri()
{
return Utils::addPaths($this->getHttpBaseUrl(), $this->containerName, $this->name);
}

/**
* @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject}
*
* @return $this|\OpenStack\Common\Resource\ResourceInterface|void
* @return $this
*/
public function create(array $data)
{
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/ObjectStore/v1/Models/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenStack\Test\ObjectStore\v1\Models;

use function GuzzleHttp\Psr7\uri_for;
use GuzzleHttp\Psr7\Stream;
use OpenStack\ObjectStore\v1\Api;
use OpenStack\ObjectStore\v1\Models\Object;
Expand Down Expand Up @@ -118,4 +119,16 @@ public function test_It_Copies()
'destination' => $headers['Destination']
]);
}

public function test_It_Gets_Public_Uri()
{
$this->client->getConfig('base_url')
->shouldBeCalled()
->willReturn(uri_for('myopenstack.org:9000/tenantId'));

$this->object->containerName = 'foo';
$this->object->name = 'bar';

$this->assertEquals(uri_for('myopenstack.org:9000/tenantId/foo/bar'), $this->object->getPublicUri());
}
}

0 comments on commit 6bcf7ac

Please sign in to comment.