- How do I install the Oh Dear PHP SDK in a Laravel project?
- Run `composer require ohdearapp/ohdear-php-sdk` in your project directory. The SDK is dependency-free and works with Laravel’s service container. No additional Laravel-specific setup is required unless you want to bind it to the container for dependency injection.
- Which Laravel versions does this SDK support?
- The SDK itself is framework-agnostic but is designed to work with Laravel 8.x and later. It leverages Saloon v4, which has no Laravel-specific dependencies. Test compatibility with your Laravel version by checking the SDK’s `composer.json` for PHP version requirements.
- Can I use this SDK to create and manage Oh Dear monitors programmatically?
- Yes. The SDK provides methods like `createMonitor()` and `monitors()` to list, create, and update monitors. You can pass an array of monitor configurations (e.g., URL, type, team ID) to these methods. Refer to the Oh Dear API docs for valid configuration options.
- How do I handle API errors or validation failures in the SDK?
- The SDK throws `ValidationException` for validation errors, which includes detailed error messages via `getMessage()` or `errors()`. For other API errors, it throws `OhDearException`. Wrap SDK calls in try-catch blocks to handle these gracefully, or extend the exceptions to integrate with Laravel’s error handling (e.g., Sentry).
- Is it possible to integrate this SDK with Laravel’s queue system for async operations?
- Yes. Wrap SDK calls in Laravel queue jobs (e.g., `CreateMonitorJob`) to avoid blocking requests. Instantiate the `OhDear` client in the job’s constructor or fetch it from the Laravel container. Use `dispatchSync()` or `dispatch()` to queue the job, depending on your needs.
- How do I authenticate with multiple Oh Dear API tokens in a Laravel app?
- Store tokens in environment variables (e.g., `OHDEAR_TOKEN_1`, `OHDEAR_TOKEN_2`) and dynamically instantiate the `OhDear` client with the desired token. For advanced use cases, bind multiple clients to the Laravel container using different token configurations or use a facade to switch tokens contextually.
- Can I cache Oh Dear API responses in Laravel to reduce rate limits or improve performance?
- Yes. Cache responses using Laravel’s cache system (e.g., Redis) or Saloon’s built-in caching. For example, cache monitor lists for 5 minutes with `Cache::remember()`. Avoid caching sensitive data like monitor states if you need real-time updates. Monitor Oh Dear’s rate limits to ensure compliance.
- Does this SDK support webhooks or real-time updates from Oh Dear?
- The SDK itself is synchronous and doesn’t handle webhooks directly. However, you can extend it to listen to Oh Dear’s webhooks (e.g., for monitor status changes) by creating a Laravel route or queue job to process incoming webhook payloads. Combine this with the SDK to fetch additional monitor details as needed.
- How do I test my Laravel app’s integration with the Oh Dear SDK?
- Use Saloon’s mocking capabilities to simulate API responses in PHPUnit tests. For example, mock `GetMonitorsRequest` to return predefined monitor data: `Saloon::shouldReceive('get')->with('monitors')->andReturn(new OhDearResponse([...]))`. Test error cases by mocking exceptions or HTTP failures.
- Are there alternatives to this SDK for integrating Oh Dear with Laravel?
- The Oh Dear PHP SDK is the official, maintained solution. Alternatives include using Guzzle directly to call the Oh Dear API or building a custom wrapper around Saloon. However, the official SDK provides DTOs, enums, and validation, reducing boilerplate and ensuring compatibility with Oh Dear’s API changes.