wi-adrema-plugin/support/Proxy.php

49 lines
1.2 KiB
PHP
Raw Normal View History

2024-02-03 22:36:56 +01:00
<?php
namespace Silva\Adrema\Support;
2024-06-10 00:13:43 +02:00
use Cache;
2024-02-03 22:36:56 +01:00
use Silva\Adrema\Models\Settings;
use Http;
2024-02-08 02:23:11 +01:00
use Illuminate\Http\JsonResponse;
2024-02-03 22:36:56 +01:00
class Proxy
{
public function run(string $url): ?array
2024-06-10 00:13:43 +02:00
{
if ($url === 'api/form') {
return Cache::rememberForever('adrema-all-events', fn () => $this->get($url));
}
return $this->get($url);
}
protected function get(string $url): ?array
2024-02-03 22:36:56 +01:00
{
2024-02-08 02:23:11 +01:00
$response = Http::acceptJson()->get($this->url($url));
2024-02-03 22:36:56 +01:00
if (!$response->ok()) {
return null;
}
return $response->json();
}
2024-02-08 02:23:11 +01:00
public function submit(string $url): JsonResponse
{
2024-03-06 22:07:04 +01:00
$headers = request()->header('X-Adrema-Token')
? [
'Authorization' => 'Bearer ' . request()->header('X-Adrema-Token'),
]
: [];
$response = Http::acceptJson()->withHeaders($headers)->post($this->url($url), request()->input());
2024-02-08 02:23:11 +01:00
return response()->json($response->json(), $response->status());
}
private function url(string $internal): string
{
2024-04-11 21:38:10 +02:00
return Settings::get('base_url') . str($internal)->start('/') . (request()->query() ? '?' . http_build_query(request()->query()) : '');
2024-02-08 02:23:11 +01:00
}
2024-02-03 22:36:56 +01:00
}