ConnectionTest im Backend funktioniert jetzt
This commit is contained in:
@@ -17,9 +17,15 @@ use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use App\Api\Plugins\ApiPluginInterface;
|
||||
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Forms\Components\Actions;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\ViewField;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ApiProviderResource extends Resource
|
||||
{
|
||||
@@ -30,41 +36,130 @@ class ApiProviderResource extends Resource
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
$plugins = self::getAvailablePlugins();
|
||||
|
||||
$livewire = $form->getLivewire();
|
||||
return $form
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label(__('filament.resource.api_provider.form.name'))
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Toggle::make('enabled')
|
||||
->label(__('filament.resource.api_provider.form.enabled'))
|
||||
->default(true),
|
||||
TextInput::make('api_url')
|
||||
->label(__('filament.resource.api_provider.form.api_url'))
|
||||
->required()
|
||||
->url()
|
||||
->maxLength(255),
|
||||
TextInput::make('username')
|
||||
->label(__('filament.resource.api_provider.form.username'))
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('password')
|
||||
->label(__('filament.resource.api_provider.form.password'))
|
||||
->password()
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('token')
|
||||
->label(__('filament.resource.api_provider.form.token'))
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
Select::make('plugin')
|
||||
->options($plugins)
|
||||
->nullable()
|
||||
->label(__('filament.resource.api_provider.form.plugin')),
|
||||
Forms\Components\Section::make()
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label(__('filament.resource.api_provider.form.name'))
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Toggle::make('enabled')
|
||||
->label(__('filament.resource.api_provider.form.enabled'))
|
||||
->default(true),
|
||||
TextInput::make('api_url')
|
||||
->label(__('filament.resource.api_provider.form.api_url'))
|
||||
->required()
|
||||
->url()
|
||||
->maxLength(255),
|
||||
TextInput::make('username')
|
||||
->label(__('filament.resource.api_provider.form.username'))
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('password')
|
||||
->label(__('filament.resource.api_provider.form.password'))
|
||||
->password()
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
TextInput::make('token')
|
||||
->label(__('filament.resource.api_provider.form.token'))
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
Select::make('plugin')
|
||||
->options($plugins)
|
||||
->nullable()
|
||||
->label(__('filament.resource.api_provider.form.plugin')),
|
||||
Actions::make([
|
||||
Action::make('test_connection')
|
||||
->label(__('filament.resource.api_provider.action.test_connection'))
|
||||
->icon(function (\Livewire\Component $livewire) {
|
||||
return match ($livewire->testResultState) {
|
||||
'success' => 'heroicon-o-check-circle',
|
||||
'failed' => 'heroicon-o-x-circle',
|
||||
default => 'heroicon-o-link',
|
||||
};
|
||||
})
|
||||
->color(function (\Livewire\Component $livewire) {
|
||||
return match ($livewire->testResultState) {
|
||||
'success' => 'success',
|
||||
'failed' => 'danger',
|
||||
default => 'gray',
|
||||
};
|
||||
})
|
||||
->action(function (array $data, Forms\Components\Component $component, \Livewire\Component $livewire) {
|
||||
$formData = $component->getLivewire()->form->getState();
|
||||
$apiUrl = str_replace('127.0.0.1', 'localhost', $formData['api_url'] ?? null);
|
||||
$plugin = $formData['plugin'] ?? null;
|
||||
$username = $formData['username'] ?? null;
|
||||
$password = $formData['password'] ?? null;
|
||||
$token = $formData['token'] ?? null;
|
||||
|
||||
try {
|
||||
$http = Http::timeout(25);
|
||||
|
||||
if ($username && $password) {
|
||||
$http->withBasicAuth($username, $password);
|
||||
} elseif ($token) {
|
||||
$http->withToken($token);
|
||||
}
|
||||
|
||||
$response = $http->get($apiUrl);
|
||||
|
||||
if ($response->successful()) {
|
||||
Log::info('External API connection successful.', [
|
||||
'api_url' => $apiUrl,
|
||||
'plugin' => $plugin,
|
||||
]);
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_successful'))
|
||||
->success()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'success');
|
||||
} else {
|
||||
Log::warning('External API connection failed: Non-successful response.', [
|
||||
'api_url' => $apiUrl,
|
||||
'plugin' => $plugin,
|
||||
'status' => $response->status(),
|
||||
'response_body' => $response->body(),
|
||||
]);
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_failed'))
|
||||
->body($response->json('message', 'An unknown error occurred.'))
|
||||
->danger()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
|
||||
}
|
||||
} catch (\Illuminate\Http\Client\RequestException $e) {
|
||||
Log::error('External API connection failed: Timeout or network error.', [
|
||||
'api_url' => $apiUrl,
|
||||
'plugin' => $plugin,
|
||||
'error_message' => $e->getMessage(),
|
||||
]);
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_failed'))
|
||||
->body('Timeout or network error: ' . $e->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('External API connection failed: An unexpected error occurred.', [
|
||||
'api_url' => $apiUrl,
|
||||
'plugin' => $plugin,
|
||||
'error_message' => $e->getMessage(),
|
||||
]);
|
||||
Notification::make()
|
||||
->title(__('filament.resource.api_provider.notification.connection_failed'))
|
||||
->body('An unexpected error occurred: ' . $e->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
$component->getLivewire()->dispatch('testConnectionFinished', result: 'failed');
|
||||
}
|
||||
}),
|
||||
]),
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
@@ -103,42 +198,38 @@ class ApiProviderResource extends Resource
|
||||
Tables\Actions\CreateAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListApiProviders::route('/'),
|
||||
'create' => Pages\CreateApiProvider::route('/create'),
|
||||
'edit' => Pages\EditApiProvider::route('/{record}/edit'),
|
||||
];
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListApiProviders::route('/'),
|
||||
'create' => Pages\CreateApiProvider::route('/create'),
|
||||
'edit' => Pages\EditApiProvider::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
protected static function getAvailablePlugins(): array
|
||||
{
|
||||
$plugins = [];
|
||||
$path = app_path('Api/Plugins');
|
||||
$files = File::files($path);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = $file->getFilenameWithoutExtension();
|
||||
if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
|
||||
continue;
|
||||
protected static function getAvailablePlugins(): array
|
||||
{
|
||||
$plugins = [];
|
||||
$path = app_path('Api/Plugins');
|
||||
$files = File::files($path);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$filename = $file->getFilenameWithoutExtension();
|
||||
if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
|
||||
continue;
|
||||
}
|
||||
$class = "App\Api\Plugins\\" . $filename;
|
||||
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
|
||||
// Do not instantiate here, just get identifier and name if possible statically or by convention
|
||||
// For now, we'll use filename as identifier and name
|
||||
$plugins[$filename] = $filename;
|
||||
}
|
||||
|
||||
$class = 'App\\Api\\Plugins\\' . $filename;
|
||||
if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
|
||||
// Do not instantiate here, just get identifier and name if possible statically or by convention
|
||||
// For now, we'll use filename as identifier and name
|
||||
$plugins[$filename] = $filename;
|
||||
}
|
||||
}
|
||||
return $plugins;
|
||||
}
|
||||
}
|
||||
return $plugins;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user