Skip to content

Commit

Permalink
Add support for image_file in visualSearch Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary authored Aug 28, 2023
1 parent 8d0bb98 commit 1c7d6c4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/Api/Admin/AssetsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ public function assetsByAssetFolder($assetFolder, $options = [])
*
* @return ApiResponse
*
* @throws ApiError
*
* @see https://cloudinary.com/documentation/admin_api#visual_search_for_resources
*/
public function visualSearch($options = [])
Expand All @@ -245,7 +247,15 @@ public function visualSearch($options = [])

$params = ArrayUtils::whitelist($options, ['image_url', 'image_asset_id', 'text']);

return $this->apiClient->get($uri, $params);
// Special handling for file inside Admin API.
if (array_key_exists('image_file', $options)) {
$options['file_field'] = 'image_file';
$options['unsigned'] = true;

return $this->apiClient->postFile($uri, $options['image_file'], $params, $options);
}

return $this->apiClient->postForm($uri, $params);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Api/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private function postLargeFileAsync($endPoint, $fileHandle, $parameters, $option
protected function postSingleChunkAsync($endPoint, $singleChunk, $parameters, $options = [])
{
$filePart = [
'name' => 'file',
'name' => ArrayUtils::get($options, 'file_field', 'file'),
'contents' => $singleChunk,
];

Expand Down
82 changes: 82 additions & 0 deletions tests/Unit/Admin/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

namespace Cloudinary\Test\Unit\Admin;

use Cloudinary\Api\Exception\ApiError;
use Cloudinary\Test\Helpers\MockAdminApi;
use Cloudinary\Test\Helpers\RequestAssertionsTrait;
use Cloudinary\Test\Integration\IntegrationTestCase;
use Cloudinary\Test\Unit\Asset\AssetTestCase;
use Cloudinary\Test\Unit\UnitTestCase;

/**
Expand All @@ -21,6 +24,9 @@ final class AssetsTest extends UnitTestCase
{
use RequestAssertionsTrait;

/**
* @return array[]
*/
public function restoreDeletedAssetSpecificVersionDataProvider()
{
return [
Expand Down Expand Up @@ -185,4 +191,80 @@ public function testRelatedAssetsByAssetIds()
]
);
}


/**
* @return array[]
*/
public function visualSearchDataProvider()
{
return [
[
'options' => [
'image_url' => AssetTestCase::FETCH_IMAGE_URL,
],
'url' => '/resources/visual_search',
'bodyFields' => [
'image_url' => AssetTestCase::FETCH_IMAGE_URL,
],
],
[
'options' => [
'image_asset_id' => self::API_TEST_ASSET_ID,
],
'url' => '/resources/visual_search',
'bodyFields' => [
'image_asset_id' => self::API_TEST_ASSET_ID,
],
],
[
'options' => [
'text' => 'sample image',
],
'url' => '/resources/visual_search',
'bodyFields' => [
'text' => 'sample image',
],
],
];
}

/**
* Test Visual search.
*
* @dataProvider visualSearchDataProvider
*
* @param array $options
* @param string $url
* @param array $bodyFields
*
* @throws ApiError
*/
public function testVisualSearch($options, $url, $bodyFields)
{
$mockAdminApi = new MockAdminApi();
$mockAdminApi->visualSearch($options);
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, $url);
self::assertRequestBodySubset($lastRequest, $bodyFields);
}

/**
* Test Visual search using image file.
*
* @throws ApiError
*/
public function testVisualSearchImageFile()
{
$mockAdminApi = new MockAdminApi();
$mockAdminApi->visualSearch(['image_file' => IntegrationTestCase::TEST_IMAGE_PATH]);
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();

self::assertRequestUrl($lastRequest, '/resources/visual_search');
$body = $lastRequest->getBody()->getContents();

self::assertStringContainsString('image_file', $body);
self::assertStringContainsString('Content-Length: 5110', $body);
}
}

0 comments on commit 1c7d6c4

Please sign in to comment.