Skip to content

Commit

Permalink
Merge pull request #23 from jordikroon/feature/allow-custom-client
Browse files Browse the repository at this point in the history
Allow custom Guzzle Client
  • Loading branch information
jordikroon authored Jan 13, 2018
2 parents 7c30214 + b6480bc commit c8520d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/Request/VisionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Vision\Annotation\ImageContext;
use Vision\Feature;
use Vision\Hydrator\AnnotationHydrator;
use Vision\Image;
use Vision\Hydrator\AnnotateImageHydrator;
use Vision\Request\Image\ImageInterface;
use Vision\Response\AnnotateImageResponse;
Expand Down Expand Up @@ -66,10 +65,15 @@ public function __construct($apiKey, ImageInterface $image, array $features, Ima
$this->imageContext = $imageContext ?: new ImageContext;
}

public function send()
/**
* @param Client|null $client
*/
public function send(Client $client = null)
{
$client = $client ?: new Client;

try {
$response = (new Client)->post(
$response = $client->post(
$this->getRequestUrl(),
[
'content-type' => 'application/json',
Expand Down Expand Up @@ -110,7 +114,15 @@ public function getRawResponse()
public function getRequestUrl()
{
$visionUrl = self::VISION_ANNOTATE_PREFIX . $this->apiKey;
return str_replace('/' . self::VISION_VERSION . '/', '/' . $this->version . '/', $visionUrl);
return str_replace('/' . self::VISION_VERSION . '/', '/' . $this->getVersion() . '/', $visionUrl);
}

/**
* @return string
*/
public function getVersion()
{
return $this->version;
}

/**
Expand Down
33 changes: 29 additions & 4 deletions src/Vision.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vision;

use GuzzleHttp\Client;
use Vision\Annotation\ImageContext;
use Vision\Request\Image\ImageInterface;
use Vision\Request\VisionRequest;
Expand Down Expand Up @@ -30,29 +31,37 @@ class Vision
/**
* @var ImageContext
*/
protected $imageContext;
protected $imageContext = null;

/**
* @var string
*/
protected $version;
protected $version = VisionRequest::VISION_VERSION;

/**
* @var Client
*/
protected $httpClient;

/**
* @param string $apiKey
* @param Feature[] $features
* @param ImageContext|null $imageContext
* @param string $version
* @param Client|null $httpClient
*/
public function __construct(
$apiKey,
array $features = [],
ImageContext $imageContext = null,
$version = VisionRequest::VISION_VERSION
$version = VisionRequest::VISION_VERSION,
Client $httpClient = null
) {
$this->apiKey = $apiKey;
$this->version = $version;
$this->setFeatures($features);
$this->setImageContext($imageContext);
$this->httpClient = $httpClient ?: new Client;
}

/**
Expand All @@ -66,7 +75,7 @@ public function request(
) {
$this->visionRequest = new VisionRequest($this->apiKey, $image, $this->features, $this->imageContext);
$this->visionRequest->setVersion($this->version);
$this->visionRequest->send();
$this->visionRequest->send($this->httpClient);

return $this->getResponseForType($responseType);
}
Expand Down Expand Up @@ -122,6 +131,22 @@ public function setImageContext($imageContext)
$this->imageContext = $imageContext ?: new ImageContext;
}

/**
* @return Client
*/
public function getHttpClient()
{
return $this->httpClient;
}

/**
* @param Client $httpClient
*/
public function setHttpClient($httpClient)
{
$this->httpClient = $httpClient;
}

/**
* @deprecated
*
Expand Down

0 comments on commit c8520d1

Please sign in to comment.