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
- GET
- PUT
- POST (Custom Pattern)
- DELETE
$result = $this->weclappApiService->get('customer/id/123', [
'serializeNulls' => 'true',
]);
// Response-Shape:
// [
// 'status' => 200,
// 'headers' => [...],
// 'body' => '<raw-string>',
// 'json' => [...assoc array...], // null wenn nicht JSON
// ]
$result = $this->weclappApiService->put('customer/id/123', [
'company' => 'New Name',
]);
// Default: ?ignoreMissingProperties=true → spart unnötige Validation
post() ist hier ein Sonderfall: Weclapp updates gehen meist über PUT. post() ruft intern noch das lokale Common-Modell ab (über Helper::getCommonModel()), bevor es Weclapp aufruft.
$result = $this->weclappApiService->post(
entityName: 'customer',
entityId: '123',
payload: ['company' => 'New Name'],
);
$this->weclappApiService->delete('customer/id/123');
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:
- liest
customAttributes[]aus$currentEntity - sucht das Attribut mit
attributeDefinitionId == 2652615 - wenn gefunden → ersetzt den Value-Slot, behält andere Slots bei
- wenn nicht gefunden → pusht ein neues Attribut ans Ende
- macht ein
PUTmitcustomAttributesals einzigem Payload-Feld
Auto-Detection des Value-Type
| Eingabe | valueType |
|---|---|
bool | booleanValue |
int / float | numberValue |
| sonst | stringValue |
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 viasafeLogOptions())