diff --git a/src/Error/MindeeV2HttpUnknownError.php b/src/Error/MindeeV2HttpUnknownException.php similarity index 90% rename from src/Error/MindeeV2HttpUnknownError.php rename to src/Error/MindeeV2HttpUnknownException.php index e2ec4a85..3198c532 100644 --- a/src/Error/MindeeV2HttpUnknownError.php +++ b/src/Error/MindeeV2HttpUnknownException.php @@ -7,7 +7,7 @@ /** * Unknown HTTP error for the V2 API. */ -class MindeeV2HttpUnknownError extends MindeeV2HttpException +class MindeeV2HttpUnknownException extends MindeeV2HttpException { /** * @param string|null $response Faulty server response. diff --git a/src/Http/BaseApi.php b/src/Http/BaseApi.php index 62d6df84..a9bdad22 100644 --- a/src/Http/BaseApi.php +++ b/src/Http/BaseApi.php @@ -79,7 +79,7 @@ abstract class BaseApi * @param string $value Value for the base Url. * @return void */ - protected function setBaseUrl(string $value) + protected function setBaseUrl(string $value): void { $this->baseUrl = $value; } diff --git a/src/Http/MindeeApiV2.php b/src/Http/MindeeApiV2.php index ec122cb9..0c546af7 100644 --- a/src/Http/MindeeApiV2.php +++ b/src/Http/MindeeApiV2.php @@ -16,7 +16,7 @@ // phpcs:enable use Mindee\Error\MindeeV2HttpException; -use Mindee\Error\MindeeV2HttpUnknownError; +use Mindee\Error\MindeeV2HttpUnknownException; use Mindee\Input\InferenceParameters; use Mindee\Input\InputSource; use Mindee\Input\LocalInputSource; @@ -106,6 +106,17 @@ public function __construct(?string $apiKey) } } + /** + * Sets the base url. + * + * @param string $value Value for the base Url. + * @return void + */ + protected function setBaseUrl(string $value): void + { + $this->baseUrl = $value; + } + /** * Sets values from environment, if needed. * @@ -168,7 +179,7 @@ public function reqPostInferenceEnqueue(InputSource $inputDoc, InferenceParamete * @return JobResponse|InferenceResponse The processed response object. * @throws MindeeException Throws if HTTP status indicates an error or deserialization fails. * @throws MindeeV2HttpException Throws if the HTTP status indicates an error. - * @throws MindeeV2HttpUnknownError Throws if the server sends an unexpected reply. + * @throws MindeeV2HttpUnknownException Throws if the server sends an unexpected reply. */ private function processResponse(array $result, string $responseType): InferenceResponse|JobResponse { @@ -180,7 +191,7 @@ private function processResponse(array $result, string $responseType): Inference if ($responseData && isset($responseData['status'])) { throw new MindeeV2HttpException(new ErrorResponse($responseData)); } - throw new MindeeV2HttpUnknownError(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + throw new MindeeV2HttpUnknownException(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); } try { @@ -248,6 +259,7 @@ private function initChannel() curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->requestTimeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_USERAGENT, $this->getUserAgent()); return $ch; } @@ -295,6 +307,7 @@ private function inferenceGetRequest(string $inferenceId): array * @param InputSource $inputSource File to upload. * @param InferenceParameters $params Inference parameters. * @return array + * @throws MindeeException Throws if the cURL operation doesn't go succeed. */ private function documentEnqueuePost( InputSource $inputSource, @@ -345,6 +358,11 @@ private function documentEnqueuePost( 'data' => curl_exec($ch), 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), ]; + $curlError = curl_error($ch); + if (!empty($curlError)) { + throw new MindeeException("cURL error:\n$curlError"); + } + curl_close($ch); return $resp; diff --git a/tests/V2/ClientV2Test.php b/tests/V2/ClientV2Test.php index 497e7b91..38111335 100644 --- a/tests/V2/ClientV2Test.php +++ b/tests/V2/ClientV2Test.php @@ -3,6 +3,7 @@ namespace V2; use Mindee\ClientV2; +use Mindee\Error\MindeeException; use Mindee\Http\MindeeApiV2; use Mindee\Input\InferenceParameters; use Mindee\Input\LocalInputSource; @@ -132,4 +133,24 @@ public function testInferenceLoadsLocally(): void 'Supplier name mismatch' ); } + public function testInvalidBaseUrlRaisesMindeeException(): void + { + $this->expectException(MindeeException::class); + + $original = getenv('MINDEE_V2_BASE_URL') ?: null; + putenv('MINDEE_V2_BASE_URL=https://invalid-v2.mindee.net'); + + try { + $client = new ClientV2('dummy-key'); + $input = new PathInput(\TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf'); + $params = new InferenceParameters('dummy-model-id'); + $client->enqueueAndGetInference($input, $params); + } finally { + if ($original === null) { + putenv('MINDEE_V2_BASE_URL'); + } else { + putenv('MINDEE_V2_BASE_URL=' . $original); + } + } + } }