Skip to content

Commit 428d740

Browse files
committed
changes
1 parent ff05105 commit 428d740

File tree

5 files changed

+105
-41
lines changed

5 files changed

+105
-41
lines changed

src/Client.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace Espo\ApiClient;
55

66
use CurlHandle;
7+
use Espo\ApiClient\Exception\Error;
8+
use Espo\ApiClient\Exception\ResponseError;
79
use InvalidArgumentException;
810
use JsonException;
911
use RuntimeException;
@@ -71,15 +73,16 @@ public function setSecretKey(?string $secretKey): self
7173
* @param string $path A relative URL path. E.g. `Account/00000000000id`.
7274
* @param array<int, mixed>|array<string, mixed>|stdClass|null $data Payload data.
7375
* @param Header[] $headers Headers.
74-
* @return stdClass|array<int, mixed>|string
75-
* @throws Exception
76+
* @return Response A response (on success).
77+
* @throws Error
78+
* @throws ResponseError On error occurred on request.
7679
*/
7780
public function request(
7881
string $method,
7982
string $path,
8083
mixed $data = null,
8184
array $headers = []
82-
): stdClass|array|string {
85+
): Response {
8386

8487
$method = strtoupper($method);
8588
$this->lastCh = null;
@@ -121,7 +124,7 @@ public function request(
121124
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
122125
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
123126
curl_setopt($ch, CURLOPT_HEADER, true);
124-
127+
125128
if ($this->port !== null) {
126129
curl_setopt($ch, CURLOPT_PORT, $this->port);
127130
}
@@ -194,7 +197,7 @@ public function request(
194197
$lastResponse = curl_exec($ch);
195198

196199
if ($lastResponse === false) {
197-
throw new Exception('CURL exec failure.', 0);
200+
throw new Error('CURL exec failure.', 0);
198201
}
199202

200203
$this->lastCh = $ch;
@@ -203,47 +206,49 @@ public function request(
203206
$responseCode = $this->getResponseHttpCode();
204207
$responseContentType = $this->getResponseContentType();
205208

209+
$response = new Response(
210+
$responseCode ?? 0,
211+
$responseContentType,
212+
$parsedResponse['header'],
213+
$parsedResponse['body'],
214+
);
215+
206216
curl_close($ch);
207217

208218
if (
209-
($responseCode !== null && $responseCode >= 200 && $responseCode < 300) &&
210-
!empty($parsedResponse['body'])
219+
$responseCode !== null &&
220+
$responseCode >= 200 &&
221+
$responseCode < 300
211222
) {
212-
if ($responseContentType === 'application/json') {
213-
return json_decode($parsedResponse['body']);
214-
}
215-
216-
return $parsedResponse['body'];
223+
return $response;
217224
}
218225

219226
$responseHeaders = $this->normalizeHeader($parsedResponse['header']);
227+
$errorMessage = $responseHeaders['X-Status-Reason'] ?? '';
220228

221-
$errorMessage = $responseHeaders['X-Status-Reason'] ?? 'Unknown Error';
222-
223-
throw (new Exception($errorMessage, $responseCode ?? 0))
224-
->withBody($parsedResponse['body']);
229+
throw new ResponseError($response, $errorMessage, $responseCode ?? 0);
225230
}
226231

227232
/**
228233
* Get a response content type.
229234
*/
230-
public function getResponseContentType(): ?string
235+
private function getResponseContentType(): ?string
231236
{
232237
return $this->getInfo(CURLINFO_CONTENT_TYPE);
233238
}
234239

235240
/**
236241
* Get a response total time.
237242
*/
238-
public function getResponseTotalTime(): ?int
243+
private function getResponseTotalTime(): ?int
239244
{
240245
return $this->getInfo(CURLINFO_TOTAL_TIME);
241246
}
242247

243248
/**
244249
* Get a response code.
245250
*/
246-
public function getResponseHttpCode(): ?int
251+
private function getResponseHttpCode(): ?int
247252
{
248253
return $this->getInfo(CURLINFO_HTTP_CODE);
249254
}

src/Exception.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Exception/Error.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/* (c) EspoCRM */
3+
4+
namespace Espo\ApiClient\Exception;
5+
6+
use Exception;
7+
8+
class Error extends Exception
9+
{}

src/Exception/ResponseError.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/* (c) EspoCRM */
3+
4+
namespace Espo\ApiClient\Exception;
5+
6+
use Espo\ApiClient\Response;
7+
8+
/**
9+
* Use getCode to obtain HTTP error code. Codes:
10+
* - 400 Bad Request (bad parameters or validation error);
11+
* - 404 Not Found (not existing URL path or record not found);
12+
* - 403 Forbidden (no access to scope or to record);
13+
* - 409 Conflict (can be that a duplicate record already exists).
14+
*/
15+
class ResponseError extends Error
16+
{
17+
final public function __construct(
18+
private Response $response,
19+
string $message = '',
20+
int $code = 0
21+
) {
22+
parent::__construct($message, $code);
23+
}
24+
25+
public function getResponse(): Response
26+
{
27+
return $this->response;
28+
}
29+
}

src/Response.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/* (c) EspoCRM */
3+
4+
namespace Espo\ApiClient;
5+
6+
class Response
7+
{
8+
public function __construct(
9+
private int $code,
10+
private ?string $contentType,
11+
private string $headersPart,
12+
private string $bodyPart
13+
) {}
14+
15+
public function getCode(): int
16+
{
17+
return $this->code;
18+
}
19+
20+
public function getHeadersPart(): string
21+
{
22+
return $this->headersPart;
23+
}
24+
25+
public function getBodyPart(): string
26+
{
27+
return $this->bodyPart;
28+
}
29+
30+
public function getContentType(): ?string
31+
{
32+
return $this->contentType;
33+
}
34+
35+
public function getParsedBody(): mixed
36+
{
37+
if ($this->getContentType() === 'application/json') {
38+
return json_decode($this->getBodyPart());
39+
}
40+
41+
return null;
42+
}
43+
}

0 commit comments

Comments
 (0)