Správa endpointov
Oba smery zdieľajú jedno API na správu vo svojom prístupe — Webhooks::inbound() a Webhooks::outbound() pre celú aplikáciu, Webhooks::for($owner)->inbound() a ->outbound() pre jedného vlastníka:
| Metóda | Prichádzajúce | Odchádzajúce |
|---|---|---|
find(int|string $id) | InboundEndpoint | OutboundEndpoint — neznáme, mäkko zmazané alebo cudzie id: EndpointNotFoundException |
endpoints() | Collection<InboundEndpoint> | Collection<OutboundEndpoint> — zoradené podľa id, vlastník prednačítaný |
update($endpoint, $changes) | InboundEndpointChanges | OutboundEndpointChanges |
enable($endpoint) | Vymaže disabled_at a disabled_reason | To isté a consecutive_failures späť na 0 |
disable($endpoint) | disabled_reason = manual; URL odpovedá 404 | EndpointDisabled(manual); čakajúce a opakované doručenia sa zrušia |
delete($endpoint) | Mäkké zmazanie; tajné kľúče sa okamžite vymažú | Mäkké zmazanie; tajné kľúče, URL a vlastné hlavičky sa okamžite vymažú; čakajúce doručenia sa zrušia |
Prichádzajúce
$endpoint = Webhooks::for($team)->inbound()->find($id); // another owner's id: EndpointNotFoundException
Webhooks::for($team)->inbound()->endpoints(); // every endpoint of $team, owner eager-loaded
Webhooks::inbound()->update($endpoint, new InboundEndpointChanges(
name: 'Billing provider',
handler: 'billing',
options: new InboundEndpointOptions(rateLimitPerMinute: 120, toleranceSeconds: 60),
));
Webhooks::inbound()->update($endpoint, new InboundEndpointChanges(clear: ['rate_limit_per_minute', 'tolerance_seconds'])); // back to the config defaults
Webhooks::inbound()->disable($endpoint); // the URL answers 404 until it is enabled again
Webhooks::inbound()->enable($endpoint);
Webhooks::inbound()->delete($endpoint); // secrets wiped at once, the URL answers 404InboundEndpointChanges(?string $name, ?string $handler, ?string $scheme, ?InboundEndpointOptions $options, list<string> $clear = []) — zmenia sa len polia, ktoré zadáte. clear vráti nullable polia na null, takže platí predvolená hodnota z konfigurácie: name, handler, rate_limit_per_minute, max_payload_bytes, allowed_content_types, tolerance_seconds, scheme_options. Akýkoľvek iný názov vyhodí InvalidEndpointException.
Odchádzajúce
$endpoint = Webhooks::for($team)->outbound()->find($id);
Webhooks::outbound()->update($endpoint, new OutboundEndpointChanges(
url: $newUrl, // judged by the SSRF guard again
events: ['invoice.*', 'customer.created'],
headers: ['X-Tenant' => 'acme'], // replaces the custom headers
options: new OutboundEndpointOptions(rateLimitPerMinute: 10, description: 'ERP sync'),
));
Webhooks::outbound()->update($endpoint, new OutboundEndpointChanges(clear: ['headers', 'rate_limit_per_minute']));
Webhooks::outbound()->disable($endpoint); // EndpointDisabled (manual); pending deliveries are cancelled
Webhooks::outbound()->enable($endpoint); // the failure counter starts again from 0
Webhooks::outbound()->delete($endpoint); // secrets, URL and custom headers wiped at onceOutboundEndpointChanges(?string $url, ?array $events, ?string $name, ?array $headers, ?string $scheme, ?OutboundEndpointOptions $options, list<string> $clear = []). Nová URL sa posúdi ako pri novom endpointe (DestinationBlockedException); nové hlavičky sa overia a nahradia staré (prázdne pole ich vymaže). clear prijíma name, description, headers, rate_limit_per_minute a scheme_options. Z konzoly: webhooks:enable {inbound|outbound} {endpoint} a webhooks:endpoints.
Trait pre vlastníka
Pridajte Concerns\HasWebhooks na akýkoľvek model, ktorý vlastní endpointy — tím, účet, používateľa. Balík nepredpokladá žiadnu multitenanciu a endpointy bez vlastníka fungujú tiež:
use RoundlyConsulting\Webhooks\Concerns\HasWebhooks;
final class Team extends Model
{
use HasWebhooks;
}$team->webhooks()->outbound()->create($url, events: ['invoice.*']); // = Webhooks::for($team)
$team->outboundWebhookEndpoints()->where('enabled', true)->get(); // MorphMany relations
$team->inboundWebhookEndpoints()->count();
$team->broadcastWebhook('invoice.paid', ['invoice' => 42]);| Metóda | Vracia |
|---|---|
inboundWebhookEndpoints() | MorphMany<InboundEndpoint> |
outboundWebhookEndpoints() | MorphMany<OutboundEndpoint> |
webhooks() | OwnerWebhooks — rovnaký prístup ako Webhooks::for($this) |
broadcastWebhook(string $eventType, array|Arrayable|JsonSerializable $payload) | Collection<OutboundDelivery> — Webhooks::for($this)->broadcast(…) |
Trait ide cez manažér, takže Webhooks::fake() jeho broadcasty zaznamená.
Limity volieb
InboundEndpointOptions a OutboundEndpointOptions sa overujú pri vytvorení aj úprave; hodnota mimo rozsahu vyhodí InvalidEndpointException s názvom voľby:
| Voľba | Limit |
|---|---|
rateLimitPerMinute | Oba smery: 1 – 1 000 000 |
maxPayloadBytes | Prichádzajúce: 1 – inbound.max_payload_bytes_ceiling (1 MB) |
contentTypes | Prichádzajúce: neprázdny zoznam type/subtype (* povolená v podtype), malými písmenami |
toleranceSeconds | Prichádzajúce: 1 – 3 600 |
schemeOptions | Oba: názvy ^[a-z0-9_]{1,64}$, reťazcové hodnoty ≤ 255 bajtov |
description | Odchádzajúce: ≤ 1 000 znakov |
name | Oba, pri vytvorení a úprave: ≤ 191 znakov |
| Importovaný prichádzajúci tajný kľúč | 1 – 1 024 bajtov, bez riadiacich znakov |
Prejavte lásku k open source
Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.
Ďalšie spôsoby podpory vrátane kryptomienOdoslaním daru súhlasíte s našimi podmienkami prijímania darov.
Chcete to zabudovať do svojho produktu?
Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.