chore: complete update

This commit is contained in:
2025-07-17 16:24:20 +02:00
parent 899227b0a4
commit 64a7051137
1300 changed files with 85570 additions and 2756 deletions

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Framework\Api;
use App\Framework\HttpClient\ClientResponse;
use RuntimeException;
class ApiException extends RuntimeException
{
public function __construct(
string $message,
int $code,
private readonly ClientResponse $response
) {
parent::__construct($message, $code);
}
public function getResponse(): ClientResponse
{
return $this->response;
}
public function getResponseData(): ?array
{
try {
return json_decode($this->response->body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
return null;
}
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace App\Framework\Api;
use App\Framework\Http\Method;
use App\Framework\HttpClient\ClientOptions;
use App\Framework\HttpClient\ClientRequest;
use App\Framework\HttpClient\ClientResponse;
use App\Framework\HttpClient\CurlHttpClient;
use App\Framework\HttpClient\HttpClient;
trait ApiRequestTrait
{
private string $baseUrl;
private ClientOptions $defaultOptions;
private HttpClient $httpClient;
/**
* Sendet eine Anfrage an die API
*/
public function sendRequest(
Method $method,
string $endpoint,
array $data = [],
?ClientOptions $options = null
): ClientResponse {
$url = $this->baseUrl . '/' . ltrim($endpoint, '/');
$options = $options ?? $this->defaultOptions;
$request = ClientRequest::json(
method: $method,
url: $url,
data: $data,
options: $options
);
$response = $this->httpClient->send($request);
$this->handleResponseErrors($response);
return $response;
}
/**
* Verarbeitet Fehler in der API-Antwort
*/
private function handleResponseErrors(ClientResponse $response): void
{
if ($response->status->value < 400) {
return;
}
$responseData = json_decode($response->body, true) ?: [];
throw new ApiException(
$this->formatErrorMessage($responseData),
$response->status->value,
$response
);
}
/**
* Formatiert eine benutzerfreundliche Fehlermeldung
*/
private function formatErrorMessage(array $responseData): string
{
$errorMessage = 'API-Fehler';
if (isset($responseData['detail'])) {
$errorMessage .= ': ' . $responseData['detail'];
if (isset($responseData['validation_messages'])) {
$errorMessage .= ' - Validierungsfehler: ' . json_encode(
$responseData['validation_messages'],
JSON_UNESCAPED_UNICODE
);
}
} elseif (isset($responseData['error'])) {
$errorMessage .= ': ' . $responseData['error'];
}
return $errorMessage;
}
/**
* Dekodiert den Response-Body als JSON
*/
protected function decodeJson(ClientResponse $response): array
{
return json_decode($response->body, true) ?: [];
}
}