- How do I install the Oh Dear PHP SDK in a Laravel project?
- Run `composer require ohdearapp/ohdear-php-sdk` to install the package. The SDK requires PHP 8.1+ and is compatible with Laravel 9+. No additional Laravel-specific setup is needed beyond binding the client to your service container if using dependency injection.
- Where should I store my Oh Dear API token for security?
- Store your API token in Laravel’s `.env` file under a custom key (e.g., `OHDEAR_API_TOKEN`) and access it via `config('services.ohdear.token')`. Avoid hardcoding tokens in source files. For enhanced security, use Laravel Vault or environment variable encryption.
- Can I use this SDK to create and manage monitors programmatically?
- Yes. Use the `createMonitor()` method with an associative array of monitor details (e.g., `url`, `type`, `team_id`). The SDK returns a `Monitor` DTO object, and you can list monitors via an iterator for pagination support. Example: `$ohDear->createMonitor(['url' => 'https://example.com', 'type' => 'http'])`.
- How does the SDK handle API errors or validation failures?
- The SDK throws `ValidationException` for validation errors (e.g., invalid monitor data) and `OhDearException` for other API failures. Catch these exceptions to handle errors gracefully. Validation errors include detailed messages via `$exception->errors()`. Always implement retry logic for transient failures.
- Does the SDK support Laravel’s service container for dependency injection?
- Yes. Bind the `OhDear` client to Laravel’s container in a service provider using a singleton binding, like `$this->app->singleton(OhDear::class, fn() => new OhDear(config('services.ohdear.token')));`. This enables clean DI across controllers, jobs, and commands.
- How can I paginate through large monitor lists efficiently?
- The SDK returns monitors as an iterator via `$ohDear->monitors()`. Loop through the iterator to handle pagination automatically. For bulk operations, consider batching requests or using Laravel Queues to avoid timeouts. Cache results if monitors rarely change.
- What Laravel versions and PHP versions are officially supported?
- The SDK requires PHP 8.1+ and is tested with Laravel 9+. While it may work on older Laravel versions, Saloon v4 (the underlying HTTP client) may introduce compatibility issues. Always test thoroughly in your Laravel environment.
- Can I customize the request timeout for API calls?
- Yes. Pass a `timeoutInSeconds` option to the `OhDear` constructor (default: 10s). Example: `$ohDear = new OhDear('token', timeoutInSeconds: 30);`. Adjust this based on your network conditions and API response times, but avoid excessively long timeouts in production.
- How do I mock the Oh Dear API for unit testing?
- Use Saloon’s built-in mocking capabilities or libraries like `vcr` or `mockery` to stub HTTP responses. The SDK’s DTOs make it easy to assert response shapes. Example: Mock `GetMonitorsRequest` and verify returned `Monitor` DTOs in your tests.
- Are there alternatives to this SDK if I need more flexibility?
- For raw API access, use Guzzle or HTTP client directly with Oh Dear’s API endpoints. However, this SDK provides typed DTOs, iterators, and Saloon’s middleware support (e.g., retries, logging), reducing boilerplate. If you need advanced features like webhook handling, consider extending the SDK or using a wrapper.