Skip to content

Commit 4100f09

Browse files
🐛 fix API calls & error management for V2 (#162)
1 parent ddf694a commit 4100f09

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/Error/MindeeV2HttpUnknownError.php renamed to src/Error/MindeeV2HttpUnknownException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Unknown HTTP error for the V2 API.
99
*/
10-
class MindeeV2HttpUnknownError extends MindeeV2HttpException
10+
class MindeeV2HttpUnknownException extends MindeeV2HttpException
1111
{
1212
/**
1313
* @param string|null $response Faulty server response.

src/Http/BaseApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ abstract class BaseApi
7979
* @param string $value Value for the base Url.
8080
* @return void
8181
*/
82-
protected function setBaseUrl(string $value)
82+
protected function setBaseUrl(string $value): void
8383
{
8484
$this->baseUrl = $value;
8585
}

src/Http/MindeeApiV2.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// phpcs:enable
1717

1818
use Mindee\Error\MindeeV2HttpException;
19-
use Mindee\Error\MindeeV2HttpUnknownError;
19+
use Mindee\Error\MindeeV2HttpUnknownException;
2020
use Mindee\Input\InferenceParameters;
2121
use Mindee\Input\InputSource;
2222
use Mindee\Input\LocalInputSource;
@@ -106,6 +106,17 @@ public function __construct(?string $apiKey)
106106
}
107107
}
108108

109+
/**
110+
* Sets the base url.
111+
*
112+
* @param string $value Value for the base Url.
113+
* @return void
114+
*/
115+
protected function setBaseUrl(string $value): void
116+
{
117+
$this->baseUrl = $value;
118+
}
119+
109120
/**
110121
* Sets values from environment, if needed.
111122
*
@@ -168,7 +179,7 @@ public function reqPostInferenceEnqueue(InputSource $inputDoc, InferenceParamete
168179
* @return JobResponse|InferenceResponse The processed response object.
169180
* @throws MindeeException Throws if HTTP status indicates an error or deserialization fails.
170181
* @throws MindeeV2HttpException Throws if the HTTP status indicates an error.
171-
* @throws MindeeV2HttpUnknownError Throws if the server sends an unexpected reply.
182+
* @throws MindeeV2HttpUnknownException Throws if the server sends an unexpected reply.
172183
*/
173184
private function processResponse(array $result, string $responseType): InferenceResponse|JobResponse
174185
{
@@ -180,7 +191,7 @@ private function processResponse(array $result, string $responseType): Inference
180191
if ($responseData && isset($responseData['status'])) {
181192
throw new MindeeV2HttpException(new ErrorResponse($responseData));
182193
}
183-
throw new MindeeV2HttpUnknownError(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
194+
throw new MindeeV2HttpUnknownException(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
184195
}
185196

186197
try {
@@ -248,6 +259,7 @@ private function initChannel()
248259
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
249260
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->requestTimeout);
250261
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
262+
251263
curl_setopt($ch, CURLOPT_USERAGENT, $this->getUserAgent());
252264
return $ch;
253265
}
@@ -295,6 +307,7 @@ private function inferenceGetRequest(string $inferenceId): array
295307
* @param InputSource $inputSource File to upload.
296308
* @param InferenceParameters $params Inference parameters.
297309
* @return array
310+
* @throws MindeeException Throws if the cURL operation doesn't go succeed.
298311
*/
299312
private function documentEnqueuePost(
300313
InputSource $inputSource,
@@ -345,6 +358,11 @@ private function documentEnqueuePost(
345358
'data' => curl_exec($ch),
346359
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
347360
];
361+
$curlError = curl_error($ch);
362+
if (!empty($curlError)) {
363+
throw new MindeeException("cURL error:\n$curlError");
364+
}
365+
348366
curl_close($ch);
349367

350368
return $resp;

tests/V2/ClientV2Test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace V2;
44

55
use Mindee\ClientV2;
6+
use Mindee\Error\MindeeException;
67
use Mindee\Http\MindeeApiV2;
78
use Mindee\Input\InferenceParameters;
89
use Mindee\Input\LocalInputSource;
@@ -132,4 +133,24 @@ public function testInferenceLoadsLocally(): void
132133
'Supplier name mismatch'
133134
);
134135
}
136+
public function testInvalidBaseUrlRaisesMindeeException(): void
137+
{
138+
$this->expectException(MindeeException::class);
139+
140+
$original = getenv('MINDEE_V2_BASE_URL') ?: null;
141+
putenv('MINDEE_V2_BASE_URL=https://invalid-v2.mindee.net');
142+
143+
try {
144+
$client = new ClientV2('dummy-key');
145+
$input = new PathInput(\TestingUtilities::getFileTypesDir() . '/pdf/blank_1.pdf');
146+
$params = new InferenceParameters('dummy-model-id');
147+
$client->enqueueAndGetInference($input, $params);
148+
} finally {
149+
if ($original === null) {
150+
putenv('MINDEE_V2_BASE_URL');
151+
} else {
152+
putenv('MINDEE_V2_BASE_URL=' . $original);
153+
}
154+
}
155+
}
135156
}

0 commit comments

Comments
 (0)