Files
PiAPS_University_Web/University.Web/app/Services/ApiService.php

67 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class ApiService
{
protected string $baseUrl;
public function __construct()
{
$this->baseUrl = env('API_BASE_URL', 'http://127.0.0.1:8000/api');
}
public function withAuth(): PendingRequest
{
return Http::withToken(Session::get('api_token'))
->baseUrl($this->baseUrl);
}
/**
* @throws ConnectionException
*/
public function get(string $url, array $params = []): PromiseInterface|Response
{
return $this->withAuth()->get($url, $params);
}
/**
* @throws ConnectionException
*/
public function post(string $url, array $data = []): PromiseInterface|Response
{
return $this->withAuth()->post($url, $data);
}
/**
* @throws ConnectionException
*/
public function patch(string $url, array $data = []): PromiseInterface|Response
{
return $this->withAuth()->patch($url, $data);
}
/**
* @throws ConnectionException
*/
public function put(string $url, array $data = []): PromiseInterface|Response
{
return $this->withAuth()->put($url, $data);
}
/**
* @throws ConnectionException
*/
public function delete(string $url, array $params = []): PromiseInterface|Response
{
return $this->withAuth()->delete($url, $params);
}
}