107 lines
3.6 KiB
PHP
107 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
use Illuminate\Support\Facades\Http;
|
|
use GuzzleHttp\Promise\Utils;
|
|
use Auth;
|
|
use Log;
|
|
|
|
class LambdaRequest
|
|
{
|
|
protected const REMOTE_ENV = 'production';
|
|
protected const LOCAL_URLS = [
|
|
'ums' => 'http://localhost:3010/dev/',
|
|
'mag' => 'http://localhost:3012/dev/',
|
|
'wall' => 'http://localhost:3011/dev/',
|
|
'metric' => 'http://localhost:3013/dev/',
|
|
'recognition' => 'http://localhost:3014/dev/',
|
|
'form' => 'http://localhost:3015/dev/',
|
|
];
|
|
protected const PROD_URL = 'https://rapportapi.click/';
|
|
|
|
protected static function genurl($service, $url) {
|
|
if(self::REMOTE_ENV == 'local') {
|
|
return self::LOCAL_URLS[$service] . $url;
|
|
} else {
|
|
if($service == 'ums' && $url == 'contact/get_all') {
|
|
return 'https://agc42avgvglo44kararsv5537e0pmjss.lambda-url.ap-south-1.on.aws/';
|
|
} else {
|
|
return self::PROD_URL . $service . '/' . $url;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public static function postrequest($service, $url, $params, $token = '') {
|
|
$params['application'] = 'rapport';
|
|
$fullurl = self::genurl($service, $url);
|
|
if($token == '') {
|
|
$response = Http::post($fullurl, $params);
|
|
} else {
|
|
$response = Http::withHeaders(['Authorization' => $token])->post($fullurl, $params);
|
|
}
|
|
$response = json_decode($response->getBody()->getContents());
|
|
return $response;
|
|
}
|
|
|
|
public static function getrequest($service, $url, $params, $token = '') {
|
|
$params['application'] = 'rapport';
|
|
$fullurl = self::genurl($service, $url);
|
|
$timeout = 180;
|
|
if(self::REMOTE_ENV != 'local' && $service == 'ums' && $url == 'contact/get_all') {
|
|
$fullurl = 'https://agc42avgvglo44kararsv5537e0pmjss.lambda-url.ap-south-1.on.aws/';
|
|
$timeout = 600;
|
|
}
|
|
if($token == '') {
|
|
$response = Http::timeout($timeout)->get($fullurl, $params);
|
|
} else {
|
|
$response = Http::timeout($timeout)->withHeaders(['Authorization' => $token])->get($fullurl, $params);
|
|
}
|
|
//$response = Http::get($fullurl, $params);
|
|
$response = json_decode($response->getBody()->getContents());
|
|
return $response;
|
|
}
|
|
|
|
public static function signedrequest($type, $service, $url, $data = [], $timeout = 180) {
|
|
$params['application'] = 'rapport';
|
|
$fullurl = self::genurl($service, $url);
|
|
if($type == 'post') {
|
|
$response = Http::timeout($timeout)->withHeaders(['Authorization' => Auth::user()->access_token])->post($fullurl, $data);
|
|
} else {
|
|
$response = Http::timeout($timeout)->withHeaders(['Authorization' => Auth::user()->access_token])->get($fullurl, $data);
|
|
}
|
|
$response = json_decode($response->getBody()->getContents());
|
|
return $response;
|
|
}
|
|
|
|
public static function multipleasyncrequest($reqs, $timeout = 180, $token = '') {
|
|
$params['application'] = 'rapport';
|
|
$promises = [];
|
|
foreach($reqs as $req) {
|
|
$req = (object)$req;
|
|
$fullurl = self::genurl($req->service, $req->url);
|
|
if($req->type == 'post') {
|
|
if($token == '') {
|
|
$promises[] = Http::async()->timeout($timeout)->post($fullurl, $req->params);
|
|
} else {
|
|
$promises[] = Http::async()->timeout($timeout)->withHeaders(['Authorization' => $token])->post($fullurl, $req->params);
|
|
}
|
|
} else {
|
|
if($token == '') {
|
|
$promises[] = Http::async()->timeout($timeout)->get($fullurl, $req->params);
|
|
} else {
|
|
$promises[] = Http::async()->timeout($timeout)->withHeaders(['Authorization' => $token])->get($fullurl, $req->params);
|
|
}
|
|
}
|
|
}
|
|
$responses = Utils::unwrap($promises);
|
|
$resp = [];
|
|
foreach($responses as $response) {
|
|
$resp[] = json_decode($response->getBody()->getContents());
|
|
}
|
|
//$response = Http::get($fullurl, $params);
|
|
//$response = json_decode($response->getBody()->getContents());
|
|
return $resp;
|
|
}
|
|
}
|