Merge pull request #183 from exciler/go

Add Debug-Logging for GO API Requests
This commit is contained in:
OpenXE-ERP 2025-02-22 12:46:28 +01:00 committed by GitHub
commit bdadadadb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View File

@ -6,6 +6,7 @@
namespace Xentral\Carrier\Go;
use Psr\Log\LoggerInterface;
use Xentral\Carrier\Go\Data\CreateOrderRequest;
use Xentral\Carrier\Go\Data\CreateOrderResponse;
use Xentral\Carrier\Go\Data\OrderStatus;
@ -16,7 +17,7 @@ class GoApi {
protected string $baseUrl;
public function __construct(protected string $username, protected string $password, bool $testMode = true) {
public function __construct(protected LoggerInterface $logger, protected string $username, protected string $password, bool $testMode = true) {
if ($testMode)
$this->baseUrl = self::BASE_URL_TEST;
else
@ -25,6 +26,12 @@ class GoApi {
public function createOrder(CreateOrderRequest $request): CreateOrderResponse|string {
$curl = curl_init();
try {
$json = json_encode($request, JSON_THROW_ON_ERROR|JSON_PRETTY_PRINT);
$this->logger->debug('GO-API Create Order Request', ['json' => $json]);
} catch (JsonException $e) {
return 'Internal Error: '.$e->getMessage();
}
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $this->baseUrl.'createOrder',
@ -32,13 +39,15 @@ class GoApi {
CURLOPT_USERNAME => $this->username,
CURLOPT_PASSWORD => $this->password,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($request, JSON_THROW_ON_ERROR),
CURLOPT_POSTFIELDS => $json,
]);
$response = json_decode(curl_exec($curl));
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$this->logger->debug('GO-API Create Order Response', ['response' => $response, 'code' => $code]);
if ($code == 200) {
$ret = new CreateOrderResponse();
$ret->hwbNumber = $response->hwbNumber;

View File

@ -27,7 +27,12 @@ class Versandart_go extends Versanddienstleister
parent::__construct($app, $id);
if (!isset($this->id) || !isset($this->settings->username) || !isset($this->settings->password))
return;
$this->api = new GoApi($this->settings->username, $this->settings->password, $this->settings->useTestEndpoint ?? true);
$this->api = new GoApi(
$this->app->Container->get('Logger'),
$this->settings->username,
$this->settings->password,
$this->settings->useTestEndpoint ?? true,
);
}
public function GetName(): string