52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Silva\Adrema\Support;
|
|
|
|
use Cache;
|
|
use Silva\Adrema\Models\Settings;
|
|
use Http;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class Proxy
|
|
{
|
|
public function run(string $url): ?array
|
|
{
|
|
if ($url === 'api/form') {
|
|
return Cache::rememberForever('adrema-all-events', fn () => $this->get($url));
|
|
}
|
|
|
|
return $this->get($url);
|
|
}
|
|
|
|
protected function get(string $url): ?array
|
|
{
|
|
$response = Http::header('Accept', 'application/json')->get($this->url($url));
|
|
|
|
if (!$response->ok) {
|
|
return null;
|
|
}
|
|
|
|
return json_decode($response->body, true);
|
|
}
|
|
|
|
public function submit(string $url): JsonResponse
|
|
{
|
|
$headers = request()->header('X-Adrema-Token')
|
|
? [
|
|
'Authorization' => 'Bearer ' . request()->header('X-Adrema-Token'),
|
|
]
|
|
: [];
|
|
$response = Http::make($this->url($url), 'POST')
|
|
->setOption(CURLOPT_POSTFIELDS, json_encode(request()->input()))
|
|
->header([...$headers, 'Accept' => 'application/json', 'Content-Type' => 'application/json'])
|
|
->send();
|
|
|
|
return response()->json(json_decode($response->body, true), $response->code);
|
|
}
|
|
|
|
private function url(string $internal): string
|
|
{
|
|
return Settings::get('base_url') . str($internal)->start('/') . (request()->query() ? '?' . http_build_query(request()->query()) : '');
|
|
}
|
|
}
|