Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add async requests with promises #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -83,14 +84,29 @@ 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);
}

/**
Expand Down
55 changes: 43 additions & 12 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -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');
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
});
}
}