From 6da362ffc9880ea20f2e369343662d183f3a4903 Mon Sep 17 00:00:00 2001 From: Constantine Nathanson Date: Thu, 21 Mar 2024 13:37:55 +0200 Subject: [PATCH] Fix `analyze` API endpoint --- src/Api/Admin/AnalysisTrait.php | 13 ++++++++----- tests/Unit/Admin/AnalysisTest.php | 22 ++++++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Api/Admin/AnalysisTrait.php b/src/Api/Admin/AnalysisTrait.php index 6757c2bb..382fd811 100644 --- a/src/Api/Admin/AnalysisTrait.php +++ b/src/Api/Admin/AnalysisTrait.php @@ -29,6 +29,8 @@ 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 * @@ -36,9 +38,9 @@ trait AnalysisTrait * * @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(); } /** @@ -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); } diff --git a/tests/Unit/Admin/AnalysisTest.php b/tests/Unit/Admin/AnalysisTest.php index ccc83b5b..4d0b77b7 100644 --- a/tests/Unit/Admin/AnalysisTest.php +++ b/tests/Unit/Admin/AnalysisTest.php @@ -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; /** @@ -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]] ] ); }