Files
Scheduler/app/Bots/GenericApi.php
Oskar-Mikael 1565df568d
All checks were successful
Deploy App / deploy (push) Successful in 11s
Fix job dispatch and show log output
2025-09-04 17:38:30 +02:00

83 lines
2.0 KiB
PHP

<?php
namespace App\Bots;
use GuzzleHttp\Client;
use Illuminate\Http\JsonResponse;
class GenericApi implements BotContract
{
protected array $config;
protected Client $client;
public function __construct(array $config = [])
{
$this->config = $config;
}
public function run(): JsonResponse
{
$this->client = new Client();
$options = [];
if (!empty($this->config['headers'])) {
$options['headers'] = json_decode($this->config['headers'], true);
}
if (!empty($this->config['body'])) {
$options['json'] = json_decode($this->config['body'], true);
}
$response = $this->client->request(
$this->config['method'] ?? 'GET',
$this->config['url'],
$options
);
return response()->json([
'status' => $response->getStatusCode(),
'body' => json_decode((string) $response->getBody(), true),
]);
}
public static function configSchema(): array
{
return [
'url' => [
'type' => 'string',
'label' => 'API URL',
'rules' => [
'required',
'url',
]
],
'method' => [
'type' => 'string',
'label' => 'HTTP Method',
'rules' => [
'required',
'in:GET,POST,PUT,DELETE,PATCH',
]
],
'headers' => [
'type' => 'json',
'label' => 'Request Headers (JSON)',
'rules' => [
'nullable',
'json',
]
],
'body' => [
'type' => 'json',
'label' => 'Request Body (JSON)',
'rules' => [
'nullable',
'json',
]
],
];
}
}