chore: complete update
This commit is contained in:
@@ -2,27 +2,129 @@
|
||||
|
||||
namespace App\Framework\HttpClient;
|
||||
|
||||
use App\Framework\Http\HeaderManipulator;
|
||||
use App\Framework\Http\Headers;
|
||||
use App\Framework\Http\Status;
|
||||
use App\Framework\HttpClient\Exception\CurlExecutionFailed;
|
||||
use App\Framework\HttpClient\Exception\CurlNotInitialized;
|
||||
use CurlHandle;
|
||||
|
||||
final readonly class CurlHttpClient implements HttpClient
|
||||
{
|
||||
public function __construct(
|
||||
private CurlResponseParser $responseParser = new CurlResponseParser,
|
||||
private AuthenticationHandler $authenticationHandler = new AuthenticationHandler,
|
||||
private CurlRequestBuilder $requestBuilder = new CurlRequestBuilder,
|
||||
){}
|
||||
|
||||
public function initializeCurl(): \CurlHandle
|
||||
{
|
||||
$curlHandle = curl_init();
|
||||
|
||||
if($curlHandle === false) {
|
||||
throw new CurlNotInitialized();
|
||||
}
|
||||
|
||||
return $curlHandle;
|
||||
}
|
||||
|
||||
public function configureCurlRequest(CurlHandle $ch, ClientRequest $request): void
|
||||
{
|
||||
$options = $this->requestBuilder->buildOptions($request);
|
||||
|
||||
if($request->options->auth !== []) {
|
||||
|
||||
$authResult = $this->authenticationHandler->configure($request->options->auth, $request->headers);
|
||||
|
||||
if($authResult->headers !== $request->headers) {
|
||||
$updatedRequest = $request->with(['headers' => $authResult->headers]);
|
||||
$options = $this->requestBuilder->buildOptions($updatedRequest);
|
||||
} else {
|
||||
$options = $this->requestBuilder->buildOptions($request);
|
||||
}
|
||||
|
||||
if(!empty($authResult->curlOptions)) {
|
||||
$options = array_replace($options, $authResult->curlOptions);
|
||||
}
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, $options);
|
||||
}
|
||||
|
||||
public function executeCurlRequest(CurlHandle $ch): string
|
||||
{
|
||||
$rawResponse = curl_exec($ch);
|
||||
|
||||
if($rawResponse === false) {
|
||||
throw new CurlExecutionFailed(curl_error($ch), curl_errno($ch));
|
||||
}
|
||||
|
||||
return $rawResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CurlExecutionFailed
|
||||
*/
|
||||
public function send(ClientRequest $request): ClientResponse
|
||||
{
|
||||
$ch = curl_init();
|
||||
$ch = $this->initializeCurl();
|
||||
|
||||
try {
|
||||
$this->configureCurlRequest($ch, $request);
|
||||
$rawResponse = $this->executeCurlRequest($ch);
|
||||
|
||||
return $this->responseParser->parse($rawResponse, $ch);
|
||||
} finally {
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
// URL mit Query-Parametern verarbeiten
|
||||
$url = $request->url;
|
||||
if (!empty($request->options->query)) {
|
||||
$separator = str_contains($url, '?') ? '&' : '?';
|
||||
$url .= $separator . http_build_query($request->options->query);
|
||||
}
|
||||
|
||||
$options = [
|
||||
CURLOPT_URL => $request->url,
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_CUSTOMREQUEST => $request->method->value,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_TIMEOUT => $request->options->timeout,
|
||||
CURLOPT_CONNECTTIMEOUT => $request->options->connectTimeout,
|
||||
CURLOPT_FOLLOWLOCATION => $request->options->followRedirects,
|
||||
CURLOPT_MAXREDIRS => $request->options->maxRedirects,
|
||||
CURLOPT_SSL_VERIFYPEER => $request->options->verifySsl,
|
||||
CURLOPT_SSL_VERIFYHOST => $request->options->verifySsl ? 2 : 0,
|
||||
];
|
||||
|
||||
// User-Agent setzen wenn vorhanden
|
||||
if ($request->options->userAgent !== null) {
|
||||
$options[CURLOPT_USERAGENT] = $request->options->userAgent;
|
||||
}
|
||||
|
||||
// Proxy setzen wenn vorhanden
|
||||
if ($request->options->proxy !== null) {
|
||||
$options[CURLOPT_PROXY] = $request->options->proxy;
|
||||
}
|
||||
|
||||
// Authentifizierung einrichten
|
||||
if ($request->options->auth !== null) {
|
||||
$this->setupAuthentication($ch, $request->options->auth, $request->headers);
|
||||
}
|
||||
|
||||
// Request-Body verarbeiten
|
||||
if ($request->body !== '') {
|
||||
$options[CURLOPT_POSTFIELDS] = $request->body;
|
||||
}
|
||||
|
||||
// Headers formatieren und setzen
|
||||
if (count($request->headers->all()) > 0) {
|
||||
$options[CURLOPT_HTTPHEADER] = $request->headers->all();
|
||||
$options[CURLOPT_HTTPHEADER] = HeaderManipulator::formatForCurl($request->headers);
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, $options);
|
||||
@@ -30,7 +132,7 @@ final readonly class CurlHttpClient implements HttpClient
|
||||
$raw = curl_exec($ch);
|
||||
|
||||
if ($raw === false) {
|
||||
throw new HttpException(curl_error($ch), curl_errno($ch));
|
||||
throw new CurlExecutionFailed(curl_error($ch), curl_errno($ch));
|
||||
}
|
||||
|
||||
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
@@ -38,12 +140,12 @@ final readonly class CurlHttpClient implements HttpClient
|
||||
$body = substr($raw, $headerSize);
|
||||
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
#$headers = Headers::fromString($headersRaw);
|
||||
$headers = new Headers();
|
||||
|
||||
return new ClientResponse(Status::from($status), $headers, $body);
|
||||
$headers = HeaderManipulator::fromString($headersRaw);
|
||||
|
||||
return new ClientResponse(
|
||||
status: Status::from($status),
|
||||
headers: $headers,
|
||||
body: $body
|
||||
);*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user