Skip to content

Commit

Permalink
add uploadSourceImageAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
chregu committed Jul 5, 2018
1 parent 235c25a commit a26393e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -73,13 +74,28 @@ public function setCredentials($key)
* @return ResponseInterface
*/
protected function call($method, $path, array $options = [], $needsCredentials = true)
{
return $this->callAsync($method, $path, $options, $needsCredentials)->wait();
}

/**
* Call the API rokka endpoint.
*
* @param string $method HTTP method to use
* @param string $path Path on the API
* @param array $options Request options
* @param bool $needsCredentials True if credentials are needed
*
* @return PromiseInterface
*/
protected function callAsync($method, $path, array $options = [], $needsCredentials = true)
{
$options['headers'][self::API_VERSION_HEADER] = $this->apiVersion;

if ($needsCredentials) {
$options['headers'][self::API_KEY_HEADER] = $this->credentials['key'];
}

return $this->client->request($method, $path, $options);
return $this->client->requestAsync($method, $path, $options);
}
}
29 changes: 24 additions & 5 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
use Rokka\Client\Core\DynamicMetadata\DynamicMetadataInterface;
Expand Down Expand Up @@ -70,6 +71,19 @@ public function __construct(ClientInterface $client, $defaultOrganization, $apiK
* @return SourceImageCollection If no image contents are provided to be uploaded
*/
public function uploadSourceImage($contents, $fileName, $organization = '', $options = null)
{
return $this->uploadSourceImageAsync($contents, $fileName, $organization, $options)->wait();
}

/**
* @param string $contents
* @param string $fileName
* @param string $organization
* @param array|null $options
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function uploadSourceImageAsync($contents, $fileName, $organization = '', $options = null)
{
if (empty($contents)) {
throw new \LogicException('You need to provide an image content to be uploaded');
Expand Down Expand Up @@ -97,12 +111,17 @@ public function uploadSourceImage($contents, $fileName, $organization = '', $opt
}
}

$contents = $this
->call('POST', self::SOURCEIMAGE_RESOURCE.'/'.$this->getOrganization($organization), ['multipart' => $requestOptions])
->getBody()
->getContents();
return $this
->callAsync(
'POST',
self::SOURCEIMAGE_RESOURCE.'/'.$this->getOrganization($organization),
['multipart' => $requestOptions]
)
->then(function (Response $value) {
$contents = $value->getBody()->getContents();

return SourceImageCollection::createFromJsonResponse($contents);
return SourceImageCollection::createFromJsonResponse($contents);
});
}

/**
Expand Down

0 comments on commit a26393e

Please sign in to comment.