From 91d77ab937079178e52d3a37a99cc266421fafd8 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 | 55 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/Base.php b/src/Base.php index 6b1d6e4..041c912 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; /** @@ -83,6 +84,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; @@ -90,7 +106,7 @@ 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 f593a93..8a6ddb6 100644 --- a/src/Image.php +++ b/src/Image.php @@ -4,6 +4,8 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Promise\PromiseInterface; +use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Uri; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -61,9 +63,22 @@ public function __construct(ClientInterface $client, $defaultOrganization, $apiK * @throws GuzzleException * @throws \RuntimeException * - * @return SourceImageCollection If no image contents are provided to be uploaded + * @return SourceImageCollection */ 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 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'); @@ -78,7 +93,7 @@ public function uploadSourceImage($contents, $fileName, $organization = '', $opt } /** - * Upload a source image. + * Upload a source image by url. * * @param string $url url to a remote image * @param string $organization Optional organization @@ -87,9 +102,23 @@ public function uploadSourceImage($contents, $fileName, $organization = '', $opt * @throws GuzzleException * @throws \RuntimeException * - * @return SourceImageCollection If no image contents are provided to be uploaded + * @return SourceImageCollection */ public function uploadSourceImageByUrl($url, $organization = '', $options = null) + { + return $this->uploadSourceImageByUrlAsync($url, $organization, $options)->wait(); + } + + /** + * Upload a source image by url. + * + * @param string $url url to a remote image + * @param string $organization Optional organization + * @param array|null $options Options for creating the image (like meta_user and meta_dynamic) + * + * @return PromiseInterface + */ + public function uploadSourceImageByUrlAsync($url, $organization = '', $options = null) { if (empty($url)) { throw new \LogicException('You need to provide an url to an image'); @@ -912,10 +941,7 @@ private function applyValueTransformationsToUserMeta(array $fields) * @param array|null $options Options for creating the image (like meta_user and meta_dynamic) * @param array $requestOptions multipart options for the guzzle client * - * @throws GuzzleException - * @throws \RuntimeException - * - * @return SourceImageCollection + * @return PromiseInterface */ private function uploadSourceImageInternal($organization, $options, $requestOptions) { @@ -947,11 +973,16 @@ private function uploadSourceImageInternal($organization, $options, $requestOpti ]; } - $contents = $this - ->call('POST', self::SOURCEIMAGE_RESOURCE.'/'.$this->getOrganizationName($organization), ['multipart' => $requestOptions]) - ->getBody() - ->getContents(); + return $this + ->callAsync( + 'POST', + self::SOURCEIMAGE_RESOURCE.'/'.$this->getOrganizationName($organization), + ['multipart' => $requestOptions] + ) + ->then(function (Response $value) { + $contents = $value->getBody()->getContents(); - return SourceImageCollection::createFromJsonResponse($contents); + return SourceImageCollection::createFromJsonResponse($contents); + }); } }