- How does the Payload pattern in labrodev/rest-sdk improve Laravel API integrations?
- The Payload pattern centralizes API logic into reusable classes, reducing boilerplate and enforcing consistency. Each API call is wrapped in a dedicated class (e.g., `UserPayload`), making endpoints, methods, and request data explicit and trackable. This aligns with Laravel’s service-oriented architecture and promotes Single Responsibility Principle (SRP) by decoupling API interactions from business logic.
- Does labrodev/rest-sdk support Laravel 10 and PHP 8.1+?
- Yes, the package is tested on Laravel 8.x/9.x/10.x and requires PHP 8.0+. While it officially supports these versions, always check the `composer.json` constraints for minor version compatibility. For PHP 8.1+, named arguments and attributes are leveraged, ensuring modern syntax support.
- Can I use this package with existing Guzzle HTTP clients in my Laravel app?
- Absolutely. The package is designed to work seamlessly with Guzzle, whether it’s bundled or already used in your project. You can integrate it alongside Laravel’s HTTP facade (`Http::`) or replace direct Guzzle calls with Payload abstractions. The package doesn’t enforce a specific HTTP client, giving you flexibility.
- How do I handle authentication (e.g., API keys, OAuth2) with labrodev/rest-sdk?
- The package doesn’t include built-in OAuth2 support, but you can layer custom middleware or traits for auth logic. For API keys, pass them via the Payload constructor or use Laravel’s binding system. For OAuth2, extend the `BasePayload` class or inject an auth service. Example: `$payload = new OAuthPayload(['token' => $this->authService->getToken()]);`
- What’s the migration path if I already have scattered API calls in my Laravel app?
- Start with a pilot integration: replace 1–2 low-risk API calls (e.g., logging or analytics) using Payload classes. Example: Convert `Http::post()` to `$payload = new LogPayload(['event' => 'user_created']); $payload->send()`. Gradually enforce consistency by creating a base `Payload` class and using traits for shared logic like auth or retries.
- Does labrodev/rest-sdk support API versioning or dynamic endpoints?
- The package supports static endpoints defined in Payload classes, which is ideal for stable APIs. For versioning, use separate Payload classes (e.g., `V1UserPayload`, `V2UserPayload`) or pass version headers dynamically. Dynamic endpoints (e.g., OpenAPI/Swagger-first) aren’t natively supported, so manual implementation or middleware would be required.
- How can I monitor API calls made with this package in production?
- Integrate with Laravel’s logging or monitoring tools (e.g., Scout, Statsd) by extending the `BasePayload` class to log requests/responses. Example: Add `Log::info($this->toArray())` in the `send()` method. For advanced monitoring (e.g., Datadog), use middleware or decorate the HTTP client to track metrics like latency or error rates.
- Are there alternatives to labrodev/rest-sdk for Laravel REST API integrations?
- For simpler needs, consider Laravel’s built-in `Http::` facade or Guzzle directly. For more structure, explore packages like `spatie/laravel-http-client` (for middleware) or `darkaonline/l5-swagger` (for OpenAPI). However, `labrodev/rest-sdk` uniquely enforces the Payload pattern, which is ideal if you prioritize consistency and SRP over minimalism.
- How do I test API calls wrapped in Payload classes?
- Unit test each Payload class by mocking the HTTP client (e.g., Guzzle) and asserting responses. Example: Use Laravel’s `Http::fake()` to simulate API responses. For integration tests, use `Http::assertSent()` to verify calls. The package’s dependency injection via Laravel’s container makes mocking straightforward.
- What are the performance implications of using labrodev/rest-sdk vs. raw Guzzle?
- The package adds minimal overhead—benchmarks show performance comparable to raw Guzzle or Laravel’s HTTP facade. However, it lacks built-in retry/backoff or async support (e.g., queues). For high-throughput APIs, consider using Guzzle promises or Laravel queues alongside Payload classes to offload heavy requests.