83 lines
2.0 KiB
PHP
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',
|
|
]
|
|
],
|
|
];
|
|
}
|
|
}
|