From a26393e25161f65bfa77d112dcf97cbd51a3860b Mon Sep 17 00:00:00 2001 From: Christian Stocker Date: Thu, 5 Jul 2018 13:44:16 +0200 Subject: [PATCH] add uploadSourceImageAsync --- src/Base.php | 18 +++++++++++++++++- src/Image.php | 29 ++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/Base.php b/src/Base.php index adac057..ae872ec 100644 --- a/src/Base.php +++ b/src/Base.php @@ -4,6 +4,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Promise\PromiseInterface; use Psr\Http\Message\ResponseInterface; /** @@ -73,6 +74,21 @@ 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; @@ -80,6 +96,6 @@ protected function call($method, $path, array $options = [], $needsCredentials = $options['headers'][self::API_KEY_HEADER] = $this->credentials['key']; } - return $this->client->request($method, $path, $options); + return $this->client->requestAsync($method, $path, $options); } } diff --git a/src/Image.php b/src/Image.php index 8e46cdf..e7a6860 100644 --- a/src/Image.php +++ b/src/Image.php @@ -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; @@ -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'); @@ -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); + }); } /**