Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Http/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 21 additions & 3 deletions src/Http/MindeeApiV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
{
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/V2/ClientV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}