Saltar al contenido principal

PHP

composer require hometown/estimator

Requiere PHP 8.2 o más reciente con ext-curl y ext-json.

Haz una estimación​

use Hometown\Estimator\Hometown;

$hometown = new Hometown(getenv('HOMETOWN_API_KEY'));

$estimate = $hometown->estimates->create(['input' => ['text' => 'sandbox:priced']]);
echo $estimate['status'];

Los objetos son arreglos con las claves de la API. Sus formas viven en Hometown\Estimator\Types para PHPStan, Psalm y tu editor. Cada llamada que lleva tu llave es un método de un recurso: $hometown->estimates->retrieve($id), $hometown->customers->list(limit: 20), $hometown->webhookEndpoints->create([...]). El último argumento acepta timeout. En estimates->create, estimates->followUp y customers->create acepta además tu propio idempotency_key.

Atrapa errores​

use Hometown\Estimator\Exception\HometownException;
use Hometown\Estimator\Exception\InsufficientCreditsException;

try {
$hometown->estimates->create(['input' => ['text' => 'Paint a 12 by 14 ft bedroom']]);
} catch (InsufficientCreditsException) {
// compra créditos en el panel
} catch (HometownException $error) {
error_log("{$error->errorCode} {$error->requestId}");
}

Los nombres de las clases siguen a los códigos: rate_limited es RateLimitedException y validation_failed es ValidationFailedException. Una petición que nunca recibe respuesta lanza ConnectionException.

Revisa un webhook​

Pasa el cuerpo tal como llegó:

use Hometown\Estimator\Webhook;

$event = Webhook::verify(file_get_contents('php://input'), $_SERVER['HTTP_HOMETOWN_SIGNATURE'] ?? null, $secret);

Lanza WebhookSignatureException cuando ninguna firma coincide o la hora de la entrega se aleja más de cinco minutos.