You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
2.2 KiB
1 lines
2.2 KiB
<?php
use App\Jobs\AsyncCall;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Symfony\Component\HttpFoundation\Request;
if (!function_exists('get')) {
function get(string $service, string $path, array $data, ?string $queue = null)
{
return call(Request::METHOD_GET, $service, $path, $data, $queue);
}
}
if (!function_exists('post')) {
function post(string $service, string $path, array $data, ?string $queue = null)
{
return call(Request::METHOD_POST, $service, $path, $data, $queue);
}
}
if (!function_exists('put')) {
function put(string $service, string $path, array $data, ?string $queue = null)
{
return call(Request::METHOD_PUT, $service, $path, $data, $queue);
}
}
if (!function_exists('delete')) {
function delete(string $service, string $path, array $data, ?string $queue = null)
{
return call(Request::METHOD_DELETE, $service, $path, $data, $queue);
}
}
if (!function_exists('call')) {
/**
* @return \Illuminate\Http\Client\Response
*/
function call(string $method, string $service, string $path, array $data, ?string $queue = null)
{
// token of this service for send data to other service
$token = 'YT76Nt2ofTbmkiP0ubvnlwOJLBtglA3UubjRhieTiTVP7jGPNX0RlueVOgIc';
// url for reaching the target service
$baseUrl = config("services.$service");
// create a pending request for this url
$pendingRequest = Http::retry(3, 100);
// if command data contain file, then it will be attached to the pending request
foreach ($data as $piece) {
if ($piece instanceof UploadedFile) {
$pendingRequest->attach($piece->getFilename(), $piece);
}
}
// if queue set then queue this command
if ($queue !== null) {
return dispatch(new AsyncCall(...func_get_args()));
}
try {
// otherwise execute the pending request
return $pendingRequest
->withToken($token)
->withoutRedirecting()
->withoutVerifying()
->$method($path, $data);
} catch (Throwable $thr) {
return $thr->response;
}
}
}
|