diff --git a/api.http b/api.http index 1faccc59..624ef1d5 100644 --- a/api.http +++ b/api.http @@ -4,4 +4,9 @@ GET https://ep-reisen.ddev.site/api/product &hotel=SSTGR &key=abcabc123 +### GET hotel data for myep +GET https://ep-reisen.ddev.site/api/hotel + ?hotel=SSTGR + &key=abcabc123 + ### diff --git a/public/typo3conf/ext/ep_products/Classes/Middleware/ProductApiMiddleware.php b/public/typo3conf/ext/ep_products/Classes/Middleware/ProductApiMiddleware.php index 9cfef9f2..f240a7f8 100644 --- a/public/typo3conf/ext/ep_products/Classes/Middleware/ProductApiMiddleware.php +++ b/public/typo3conf/ext/ep_products/Classes/Middleware/ProductApiMiddleware.php @@ -16,29 +16,65 @@ class ProductApiMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - if ('/api/product' !== $request->getUri()->getPath()) { - return $handler->handle($request); + $path = $request->getUri()->getPath(); + + if ('/api/hotel' === $path) { + return $this->handleHotelRequest($request); } + if ('/api/product' === $path) { + return $this->handleProductRequest($request); + } + + return $handler->handle($request); + } + + private function handleHotelRequest(ServerRequestInterface $request): ResponseInterface + { // This is currently fake, we only check for existence. Since we are communicating // on the backchannel only it's not much of a problem. $apiKey = $request->getQueryParams()['key'] ?? null; if (null === $apiKey) { - return (new JsonResponse(['message' => 'No api key provided'], 403)); + return new JsonResponse(['message' => 'No api key provided'], 403); + } + + $hotelCode = $request->getQueryParams()['hotel'] ?? null; + + if (null === $hotelCode) { + return new JsonResponse(['message' => 'No hotel code provided'], 400); + } + + $data = $this->fetchHotelData($hotelCode); + + if ([] === $data) { + return new JsonResponse(['message' => 'Hotel not found'], 404); + } + + return new JsonResponse($data); + } + + private function handleProductRequest(ServerRequestInterface $request): ResponseInterface + { + // This is currently fake, we only check for existence. Since we are communicating + // on the backchannel only it's not much of a problem. + $apiKey = $request->getQueryParams()['key'] ?? null; + + if (null === $apiKey) { + return new JsonResponse(['message' => 'No api key provided'], 403); } $productCode = $request->getQueryParams()['product'] ?? null; $hotelCode = $request->getQueryParams()['hotel'] ?? null; if (null === $productCode) { - return (new JsonResponse(['message' => 'No product code provided'], 400)); + return new JsonResponse(['message' => 'No product code provided'], 400); } $data = $this->fetchProductData($productCode); if (null === $data) { - return (new JsonResponse(['message' => 'Product not found'], 404)); + return new JsonResponse(['message' => 'Product not found'], 404); } if (null !== $hotelCode) {