Appearance
InteractWithPanelViaHttp Trait
The InteractWithPanelViaHttp
trait provides a standardized way to verify API key authentication when interacting with the Module Manager admin panel via HTTP requests.
Features
This trait offers:
- Simple verification of API key configuration
- Consistent error messaging for missing API keys
- Reusable functionality across commands that interact with the Module Manager panel
Methods
verifyIfApiKeyIsSet(): bool
Checks if the Module Manager API key is properly configured.
- Returns:
true
if the API key is setfalse
if the API key is not set
- Behavior:
- Checks the
module-manager.admin_panel.api_key
configuration - Outputs an error message if the API key is not set
- Checks the
Usage Example
php
<?php
namespace Modules\Blog\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use ModuleManager\ModuleManager\Concerns\InteractWithPanelViaHttp;
class CheckModuleUpdateCommand extends Command
{
use InteractWithPanelViaHttp;
protected $signature = 'blog:check-updates';
protected $description = 'Check for updates to the Blog module';
public function handle(): int
{
// First verify API key before making any HTTP requests
if (!$this->verifyIfApiKeyIsSet()) {
return self::FAILURE;
}
// Now proceed with HTTP requests knowing the API key is available
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . config('module-manager.admin_panel.api_key'),
])->get('https://modules.example.com/api/check-updates/blog');
// Process response...
return self::SUCCESS;
}
}
Integration
This trait is commonly used in:
- Module listing commands
- Module download commands
- Module update/removal commands
- Any command that interacts with the Module Manager admin panel
Notes
- This trait is a small but important utility for ensuring consistent API key verification
- It provides a clean way to handle authentication failures early in command execution
- Error messages are displayed using the command's error output method