Merhabalar bu soruyu daha önce sordum ancak, ilgili cevap ile işlem sağlamam vakit alıyor.(Pattern Kullanma)
Yazmış olduğum 2 apim var. Bu iki apiyi nasıl birbirlerine bağlarım? Birinde fiyat arayacak, bulamazsa diğer serviste arayıp yazdıracak. Bu şekilde bir işlem sağlamam gerekiyor. Kodlarımı aşağıya ekliyorum. Bir api için Service ve Controller kodlarını ekliyorum. Ayrıca Route' u da. Diğer api için de bilgiler bu kodlar ile neredeyse aynı. Yardımlarınız için şimdiden teşekkür ederim.
<?php
namespace App\Services\M;
use App\Core\ServiceResponse;
use App\Interfaces\IMService;
use Facebook\WebDriver\Chrome\ChromeDriver;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Http;
class MService extends BaseMService implements IMService
{
/**
* @param mixed $brand
* @param mixed $model
* @param mixed $kilometerFrom
* @param mixed $kilometerTo
* @param mixed $yearFrom
* @param mixed $yearTo
* @param mixed $fuelTypes
* @param mixed $gearBoxes
* @param mixed $powerFrom
* @param mixed $powerTo
*
* @return ServiceResponse
*/
public function getByParameters(
$brand,
$model,
$kilometerFrom,
$kilometerTo,
$yearFrom,
$yearTo,
$fuelTypes,
$gearBoxes,
$powerFrom,
$powerTo
): ServiceResponse
{
$chromeDriverPath = base_path('chromedriver.exe');
putenv('WEBDRIVER_CHROME_DRIVER=' . $chromeDriverPath);
$endpoint = $this->searchUrl;
$priceList = [];
set_time_limit(3600);
for ($pageCounter = 1; $pageCounter <= 1; $pageCounter++) {
$parameters = [
'ms' => $brand . ';' . $model,
'ml' => $kilometerFrom . ':' . $kilometerTo,
'fr' => $yearFrom . ':' . $yearTo,
'ft' => implode(' ', $fuelTypes),//Bir diziyi metne dönüştürür.
'tr' => implode(' ', $gearBoxes),
'powertype' => 'kw',
'pw' => $powerFrom . ':' . $powerTo,
'cn' => 'DE',
'isSearchRequest' => 'true',
'page' => $pageCounter
];
$chromeDriver = ChromeDriver::start();
$chromeDriver->manage()->window()->minimize();
#http_build_query() fonksiyonu ile URL oluşturuyoruz.
$chromeDriver->get($endpoint . '?' . http_build_query($parameters));
$sources = $chromeDriver->getPageSource();
preg_match_all('~<span class=\"h3 u-block\">(.*?) €</span>~', $sources, $prices);
foreach ($prices[1] as $price) {
$priceList[] = (int)str_replace('.', '', $price);
}
}
$chromeDriver->quit();
$averagePrice = array_sum($priceList) / count($priceList);
return new ServiceResponse(
true,
'Average price calculated successfully.',
200,
intval($averagePrice)
);
}
}
<?php
namespace App\Http\Controllers\Api\MDe;
use App\Core\Controller;
use App\Core\HttpResponse;
use App\Core\ServiceResponse;
use App\Http\Requests\Api\M\PricePredictionController\CheckRequest;
use App\Interfaces\IMService;
use Facebook\WebDriver\Chrome\ChromeDriver;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
class PricePredictionController extends Controller
{
use HttpResponse;
/**
* @var $mService
*/
private $mService;
public function __construct(IMService $mService)
{
$this->mService = $mService;
}
/**
* @param CheckRequest $request
*/
public function check(CheckRequest $request)
{
set_time_limit(3600);
$response = $this->mService->getByParameters(
$request->brand,
$request->model,
$request->kilometerFrom,
$request->kilometerTo,
$request->yearFrom,
$request->yearTo,
$request->fuelTypes ?? [],
$request->gearBoxes ?? [],
$request->powerFrom,
$request->powerTo
);
return $this->httpResponse(
$response->getMessage(),
$response->getStatusCode(),
$response->getData(),
$response->isSuccess()
);
}
}
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::prefix('pricePrediction')->group(function () {
Route::prefix('a')->group(function () {
Route::post('check', [\App\Http\Controllers\Api\A\PricePredictionController::class, 'check']);
});
Route::prefix('m')->group(function () {
Route::post('check', [\App\Http\Controllers\Api\M\PricePredictionController::class, 'check']);
});
});