.
This commit is contained in:
@@ -125,12 +125,19 @@ class ShayeganClient
|
||||
$url = $this->endpointUrl($endpoint, $query);
|
||||
$timeout = max(5, $this->config->getInt('shayegan_timeout_seconds', 30));
|
||||
|
||||
$clientRequest = [
|
||||
'endpoint' => $endpoint,
|
||||
'url' => $url,
|
||||
'query' => $query,
|
||||
'payload' => $this->redactPayload($payload),
|
||||
];
|
||||
|
||||
$this->logTraffic('request', [
|
||||
'style' => $style,
|
||||
'endpoint' => $endpoint,
|
||||
'url' => $url,
|
||||
'timeout_seconds' => $timeout,
|
||||
'payload' => $this->redactPayload($payload),
|
||||
'payload' => $clientRequest['payload'],
|
||||
]);
|
||||
|
||||
try {
|
||||
@@ -147,13 +154,14 @@ class ShayeganClient
|
||||
}
|
||||
|
||||
$error = $this->extractErrorMessage($json);
|
||||
$serverResponse = $this->serverResponse($status, $error !== '' ? $error : 'OK', $json);
|
||||
if ($status >= 400) {
|
||||
$result = [
|
||||
$result = $this->withExchange([
|
||||
'ok' => false,
|
||||
'message' => $error !== '' ? $error : ('HTTP ' . $status),
|
||||
'http_status' => $status,
|
||||
'body' => $json,
|
||||
];
|
||||
], $clientRequest, $serverResponse);
|
||||
$this->logTraffic('response', [
|
||||
'style' => $style,
|
||||
'endpoint' => $endpoint,
|
||||
@@ -168,12 +176,12 @@ class ShayeganClient
|
||||
}
|
||||
|
||||
if ($error !== '') {
|
||||
$result = [
|
||||
$result = $this->withExchange([
|
||||
'ok' => false,
|
||||
'message' => $error,
|
||||
'http_status' => $status,
|
||||
'body' => $json,
|
||||
];
|
||||
], $clientRequest, $serverResponse);
|
||||
$this->logTraffic('response', [
|
||||
'style' => $style,
|
||||
'endpoint' => $endpoint,
|
||||
@@ -187,12 +195,12 @@ class ShayeganClient
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result = [
|
||||
$result = $this->withExchange([
|
||||
'ok' => true,
|
||||
'message' => 'OK',
|
||||
'http_status' => $status,
|
||||
'body' => $json,
|
||||
];
|
||||
], $clientRequest, $serverResponse);
|
||||
$this->logTraffic('response', [
|
||||
'style' => $style,
|
||||
'endpoint' => $endpoint,
|
||||
@@ -213,13 +221,13 @@ class ShayeganClient
|
||||
'payload' => $this->redactPayload($payload),
|
||||
]);
|
||||
|
||||
return [
|
||||
return $this->withExchange([
|
||||
'ok' => false,
|
||||
'message' => 'timeout/connection: ' . $e->getMessage(),
|
||||
'http_status' => null,
|
||||
'body' => null,
|
||||
'timeout' => true,
|
||||
];
|
||||
], $clientRequest, $this->serverResponse(null, 'timeout/connection: ' . $e->getMessage(), null));
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('shayegan.request_failed', [
|
||||
'endpoint' => $endpoint,
|
||||
@@ -229,15 +237,58 @@ class ShayeganClient
|
||||
'payload' => $this->redactPayload($payload),
|
||||
]);
|
||||
|
||||
return [
|
||||
return $this->withExchange([
|
||||
'ok' => false,
|
||||
'message' => $e->getMessage(),
|
||||
'http_status' => null,
|
||||
'body' => null,
|
||||
];
|
||||
], $clientRequest, $this->serverResponse(null, $e->getMessage(), null));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $result
|
||||
* @return array{client_request:mixed,server_response:mixed}
|
||||
*/
|
||||
public function debugExchange(array $result): array
|
||||
{
|
||||
return [
|
||||
'client_request' => $result['client_request'] ?? null,
|
||||
'server_response' => $result['server_response'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $result
|
||||
* @param array<string, mixed> $clientRequest
|
||||
* @param array<string, mixed> $serverResponse
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function withExchange(array $result, array $clientRequest, array $serverResponse): array
|
||||
{
|
||||
$result['client_request'] = $clientRequest;
|
||||
$result['server_response'] = $serverResponse;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $body
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function serverResponse(?int $status, ?string $message, $body): array
|
||||
{
|
||||
$response = [
|
||||
'message' => $message,
|
||||
'http_status' => $status,
|
||||
];
|
||||
if (is_array($body)) {
|
||||
$response['body'] = $body;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, int|string> $query
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user