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

Fix analyze API endpoint #399

Merged
merged 1 commit into from
Mar 21, 2024
Merged
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
13 changes: 8 additions & 5 deletions src/Api/Admin/AnalysisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ trait AnalysisTrait
* @param string $inputType The type of input for the asset to analyze ('uri').
* @param string $analysisType The type of analysis to run ('google_tagging', 'captioning', 'fashion').
* @param string $uri The URI of the asset to analyze.
* @param array $parameters Additional parameters.
*
*
* @return ApiResponse
*
* @see AdminApi::analyzeAsync()
*
* @see https://cloudinary.com/documentation/media_analyzer_api_reference
*/
public function analyze($inputType, $analysisType, $uri = null)
public function analyze($inputType, $analysisType, $uri = null, $parameters = null)
{
return $this->analyzeAsync($inputType, $analysisType, $uri)->wait();
return $this->analyzeAsync($inputType, $analysisType, $uri, $parameters)->wait();
}

/**
Expand All @@ -47,16 +49,17 @@ public function analyze($inputType, $analysisType, $uri = null)
* @param string $inputType The type of input for the asset to analyze ('uri').
* @param string $analysisType The type of analysis to run ('google_tagging', 'captioning', 'fashion').
* @param string $uri The URI of the asset to analyze.
* @param array $parameters Additional parameters.
*
* @return PromiseInterface
*
* @see https://cloudinary.com/documentation/media_analyzer_api_reference
*/
public function analyzeAsync($inputType, $analysisType, $uri = null)
public function analyzeAsync($inputType, $analysisType, $uri = null, $parameters = null)
{
$endPoint = [ApiEndPoint::ANALYSIS, 'analyze'];
$endPoint = [ApiEndPoint::ANALYSIS, 'analyze', $inputType];

$params = ['input_type' => $inputType, 'analysis_type' => $analysisType, 'uri' => $uri];
$params = ['analysis_type' => $analysisType, 'uri' => $uri, 'parameters' => $parameters];

return $this->apiV2Client->postJsonAsync($endPoint, $params);
}
Expand Down
22 changes: 12 additions & 10 deletions tests/Unit/Admin/AnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

namespace Cloudinary\Test\Unit\Admin;

use Cloudinary\Api\Exception\ApiError;
use Cloudinary\Configuration\Configuration;
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 @@ -31,19 +28,24 @@ final class AnalysisTest extends UnitTestCase
public function testAnalyze()
{
$mockAdminApi = new MockAdminApi();
$mockAdminApi->analyze("uri", "captioning", "https://res.cloudinary.com/demo/image/upload/dog");
$mockAdminApi->analyze(
"uri",
"captioning",
"https://res.cloudinary.com/demo/image/upload/dog",
["custom" => ["model_name" => "my_model", "model_version" => 1]]
);

$lastRequest = $mockAdminApi->getV2MockHandler()->getLastRequest();
$apiV2Configuration = new Configuration();
$lastRequest = $mockAdminApi->getV2MockHandler()->getLastRequest();
$apiV2Configuration = new Configuration();
$apiV2Configuration->api->apiVersion = '2';

self::assertRequestUrl($lastRequest, '/analysis/analyze', "", $apiV2Configuration);
self::assertRequestUrl($lastRequest, '/analysis/analyze/uri', "", $apiV2Configuration);
self::assertRequestJsonBodySubset(
$lastRequest,
[
"input_type"=> "uri",
"analysis_type"=> "captioning",
"uri"=> "https://res.cloudinary.com/demo/image/upload/dog",
"analysis_type" => "captioning",
"uri" => "https://res.cloudinary.com/demo/image/upload/dog",
"parameters" => ["custom" => ["model_name" => "my_model", "model_version" => 1]]
]
);
}
Expand Down
Loading