freescout2ant/Services/AmeiseApiClient.php
2025-04-21 13:19:30 +02:00

39 lines
1.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Modules\AmeiseModule\Services;
use Illuminate\Support\Facades\Http;
class AmeiseApiClient
{
public function __construct(
protected AmeiseTokenService $tokenService
) {}
/**
* Universelle Methode für APICalls (GET, POST, …).
*
* @param string $method HTTPMethode (GET|POST|PUT|PATCH|DELETE)
* @param string $uri Endpunkt relativ zur BaseURI
* @param array $options query|json je nach Methode
*/
public function request(string $method, string $uri, array $options = [])
{
$token = $this->tokenService->getValidAccessToken();
$http = Http::withOptions([
'base_uri' => rtrim(config('ameisemodule.ameise_base_uri'), '/').'/',
])->withHeaders([
'Authorization' => "Bearer {$token}",
]);
return match (strtoupper($method)) {
'GET' => $http->get($uri, $options['query'] ?? []),
'POST' => $http->post($uri, $options['json'] ?? []),
'PUT' => $http->put($uri, $options['json'] ?? []),
'PATCH' => $http->patch($uri, $options['json'] ?? []),
'DELETE' => $http->delete($uri, $options['query'] ?? []),
default => throw new \InvalidArgumentException("Unsupported method {$method}"),
};
}
}