Skip to content

Commit 71bfae0

Browse files
committed
Add AIApi for AI recommendations service, implement ApiGatewayController::getRecommendedProducts for fetching recommended products, update routes and environment variables for AI service integration, and enhance docker-compose.yml configuration.
1 parent 1c00257 commit 71bfae0

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

app/External_Apis/Apis/AIApi.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\External_Apis\Apis;
4+
5+
class AIApi extends BasicApi
6+
{
7+
8+
private const string CONFIG_KEY = 'ai_url';
9+
10+
public static function getAIApi(): string
11+
{
12+
return self::getBaseUrl() . 'api/recommendations';
13+
}
14+
15+
protected static function getConfigKey(): string
16+
{
17+
return self::CONFIG_KEY;
18+
}
19+
}

app/External_Apis/Apis/BasicApi.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ abstract protected static function getConfigKey(): string;
1414

1515
public static function getBaseUrl(): string
1616
{
17-
// USER_SERVICE_URL=http://user-webserver/
1817
return config('services.microservices.' . static::getConfigKey());
1918
}
2019

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\External_Apis\Apis\AIApi;
6+
use App\External_Apis\Services\ProductService;
7+
use App\Grpc\Services\GrpcService;
8+
use Illuminate\Http\Client\ConnectionException;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Http;
11+
use Symfony\Component\HttpFoundation\Response;
12+
13+
class ApiGatewayController extends BaseController
14+
{
15+
public function __construct(
16+
private readonly ProductService $productService,
17+
private readonly GrpcService $grpcService
18+
)
19+
{
20+
}
21+
//
22+
23+
/**
24+
* @throws ConnectionException
25+
*/
26+
public function getRecommendedProducts(Request $request)
27+
{
28+
try {
29+
$res = Http::withToken($request->bearerToken())->get(AIApi::getAIApi());
30+
if ($res->failed())
31+
throw new ConnectionException('Failed to connect to AI API', $res->status());
32+
$recommendedProductData = $res->json('data', []);
33+
$request->merge($this->productService->injectUserId());
34+
$request->merge(['recommended_product_ids' => data_get($recommendedProductData, 'id')]);
35+
$res = $this->grpcService->proxyRequest(
36+
$request,
37+
'product',
38+
'listProducts',
39+
);
40+
if ($res->getStatusCode() == Response::HTTP_OK) {
41+
$filteredProductData = ($res->getData(true));
42+
$filteredProductData = array_map(function ($productData) {
43+
$productData['percentage'] = $recommendedProductData['compatibility_score'] ?? 0;
44+
return $productData;
45+
}, data_get($filteredProductData, 'data', []));
46+
47+
return $res->setData($filteredProductData);
48+
return $res;
49+
} else
50+
throw new ConnectionException('Failed to fetch products', $res->getStatusCode());
51+
} catch (ConnectionException $e) {
52+
return response()->json([
53+
'message' => 'Connection error',
54+
'error' => $e->getMessage()
55+
], 500);
56+
}
57+
}
58+
}

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
'user_url' => env('USER_SERVICE_URL', 'http://user-webserver/'),
4040
'product_url' => env('PRODUCT_SERVICE_URL', 'http://product-webserver/'),
4141
'order_url' => env('ORDER_SERVICE_URL', 'http://order-webserver/'),
42+
'ai_url' => env('AI_SERVICE_URL', 'http://ai-service/'),
4243
]
4344
];

docker-compose.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ services:
22
api-gw:
33
build:
44
context: .
5-
image: ommrgazar315/api-gw:2.0
5+
image: ommrgazar315/api-gw:2.0_grpc
66
container_name: api-gw
77
restart: unless-stopped
88
working_dir: /var/www
@@ -31,6 +31,10 @@ services:
3131
- DB_DATABASE=api-gw_db
3232
- DB_USERNAME=laravel
3333
- DB_PASSWORD=api-gw_pass
34+
- PRODUCT_SERVICE_URL=http://product-webserver/
35+
- USER_SERVICE_URL=http://user-webserver/
36+
- ORDER_SERVICE_URL=http://order-webserver/
37+
- AI_SERVICE_URL=http://ai-webserver/
3438

3539
api-gw_db:
3640
image: mysql:8.0

routes/api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Controllers\ApiGatewayController;
34
use App\Http\Controllers\AuthApiController;
45
use App\Http\Controllers\GoogleApiController;
56
use App\Http\Controllers\OfferApiController;
@@ -68,3 +69,6 @@
6869

6970
Route::post('products/batch', [ProductApiController::class, 'batch']);
7071
Route::post('/offers/batch', [OfferApiController::class, 'batch']);
72+
73+
// AI Service
74+
Route::post('/recommendations', [ApiGatewayController::class, 'getRecommendedProducts'])->middleware('authenticate');

0 commit comments

Comments
 (0)