Skip to main content

Weclapp ApiService

App\Services\WeclappApiService

Wrapper um den internen App\Http\Api\Weclapp\ClientController (selbst ein Guzzle-Wrapper mit AuthenticationToken-Header). Bietet:

  • typische REST-Verben (get, post, put, delete)
  • spezialisierte Helper für Custom Attributes (updateCustomAttribute)
  • einheitliches Error-Handling über WeclappApiException

DI

use App\Services\WeclappApiService;

public function __construct(
protected WeclappApiService $weclappApiService
) {}

Standard-Methoden

$result = $this->weclappApiService->get('customer/id/123', [
'serializeNulls' => 'true',
]);

// Response-Shape:
// [
// 'status' => 200,
// 'headers' => [...],
// 'body' => '<raw-string>',
// 'json' => [...assoc array...], // null wenn nicht JSON
// ]

Custom Attributes

Weclapp hat ein generisches Custom-Attributes-System (customAttributes[] mit attributeDefinitionId + Wert in stringValue / numberValue / booleanValue / dateValue).

updateCustomAttribute()

Updated EIN Custom-Attribute auf einer Entity, ohne die anderen zu verlieren.

$current = $this->weclappApiService->get('customer/id/123')['json'];

$this->weclappApiService->updateCustomAttribute(
endpoint: 'customer/id/123',
currentEntity: $current,
attributeDefinitionId: 2652615, // ID aus customAttributeDefinition
value: $mfrId,
valueType: null, // wird automatisch erkannt
);

Der Service:

  1. liest customAttributes[] aus $currentEntity
  2. sucht das Attribut mit attributeDefinitionId == 2652615
  3. wenn gefunden → ersetzt den Value-Slot, behält andere Slots bei
  4. wenn nicht gefunden → pusht ein neues Attribut ans Ende
  5. macht ein PUT mit customAttributes als einzigem Payload-Feld

Auto-Detection des Value-Type

EingabevalueType
boolbooleanValue
int / floatnumberValue
sonststringValue

Manuell über valueType: 'dateValue' setzbar.

Custom-Attributes auslesen — Helper::getCustomAttribute()

use App\Http\Helpers\Weclapp\Helper as wcHelper;

$mfrId = wcHelper::getCustomAttribute(
$entity['customAttributes'],
2652615,
);
// returns first non-attributeDefinitionId-key value (e.g. 'numberValue' contents)

Error-Handling

Alle Fehler werden zu App\Services\WeclappApiException gewrappt:

try {
$r = $this->weclappApiService->get('customer/id/' . $id);
} catch (WeclappApiException $e) {
$e->getStatus(); // HTTP-Status
$e->getMessage(); // "weclapp API error (HTTP 404) | content-type: application/json | body: ..."
$e->getResponseBody(); // raw body
$e->getResponseJson(); // decoded array oder null
$e->getResponseHeaders(); // headers
}

Im Controller

public function showCustomer(WeclappApiService $weclapp, int $id)
{
try {
$result = $weclapp->get("customer/id/{$id}");
return response()->json($result['json'] ?? ['body' => $result['body']]);
} catch (WeclappApiException $e) {
return response()->json([
'message' => $e->getMessage(),
'status' => $e->getStatus(),
'body' => $e->getResponseBody(),
'json' => $e->getResponseJson(),
], $e->getStatus() ?: 500);
}
}

Logging

  • Transport-Fehler → Log::error('weclapp transport error', [...])
  • HTTP-Fehler ≥ 400 → Log::warning('weclapp api request failed', [...]) mit safer Headers (Token wird zu *** redacted via safeLogOptions())