Skip to content

Commit

Permalink
fix: refactoring menu values
Browse files Browse the repository at this point in the history
  • Loading branch information
fmelihh committed May 12, 2024
1 parent bebb690 commit 8758b98
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 115 deletions.
16 changes: 9 additions & 7 deletions src/recommendation_engine/app/domain/menu/entity/getir.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from loguru import logger

from ...entity import BaseEntity
from ...request import RequestValue
from ..values.getir import GetirMenuValue
from ...value_stack import EntityValueStack
from ...processor import Processor, SyncCallParams
from ..values import RequestValue, MenuValue, MenuStack


class GetirMenu(BaseEntity, Processor):
Expand Down Expand Up @@ -31,7 +33,7 @@ def __init__(self, restaurant_slug: str):
method="GET",
headers=self.HEADERS,
)
self.menu_stack = MenuStack()
self.menu_stack = EntityValueStack()

def _retrieve_menu_from_api(self) -> list[dict] | None:
request_template = self.filter_and_search_payload.retrieve_formatted_request(
Expand All @@ -52,7 +54,7 @@ def _retrieve_menu_from_api(self) -> list[dict] | None:
return menu_list

@staticmethod
def _transform_unstructured_data(category: str, menu_value: dict) -> MenuValue:
def _transform_unstructured_data(category: str, menu_value: dict) -> GetirMenuValue:
values = dict()
values["category"] = category
values["product_id"] = menu_value["id"]
Expand All @@ -62,19 +64,19 @@ def _transform_unstructured_data(category: str, menu_value: dict) -> MenuValue:
values["image_url"] = menu_value["imageURL"]
values["full_screen_image_url"] = menu_value["fullScreenImageURL"]
values["is_available"] = menu_value["isAvailable"]
menu_value = MenuValue(**values)
menu_value = GetirMenuValue(**values)
return menu_value

def process(self, process_limit: int | None = None) -> list[MenuValue]:
def process(self, process_limit: int | None = None) -> list[GetirMenuValue]:
menu_list = self._retrieve_menu_from_api()
for entity in menu_list:
category = entity["name"]
category_menu_list = entity["products"]
for category_menu in category_menu_list:
menu = self._transform_unstructured_data(category, category_menu)
self.menu_stack.add_menu(menu)
self.menu_stack.add_value(menu)

if process_limit is not None and len(self.menu_stack) >= process_limit:
break

return self.menu_stack.retrieve_menu_list()
return self.menu_stack.retrieve_values()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .menu import GetirMenuValue
64 changes: 64 additions & 0 deletions src/recommendation_engine/app/domain/menu/values/getir/menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pydantic import HttpUrl
from dataclasses import dataclass

from ..price import Price
from ..menu import MenuValue


@dataclass(frozen=True)
class GetirMenuValue(MenuValue):
category: str
product_id: str
name: str
price: Price
description: str
image_url: str | None
full_screen_image_url: str | None
is_available: bool

def validate_category(self) -> str:
if not isinstance(self.category, str):
raise ValueError("Invalid comment category type expected.")
return self.category

def validate_product_id(self) -> str:
if not isinstance(self.product_id, str):
raise ValueError("Invalid comment product id type expected.")
return self.product_id

def validate_name(self) -> str:
if not isinstance(self.name, str):
raise ValueError("Invalid comment name type expected.")
return self.name

def validate_price(self) -> Price:
if not isinstance(self.price, str):
raise ValueError("Invalid comment price type expected.")

amount = self.price.replace("₺", "").replace(",", ".").strip()
amount = float(amount)
return Price(amount=amount, currency="TL")

def validate_description(self) -> str:
if not isinstance(self.description, str):
raise ValueError("Invalid comment description type expected.")
return self.description

def validate_image_url(self) -> str | None:
if not isinstance(self.image_url, str):
raise ValueError("Invalid comment image url type expected.")
if len(self.image_url) == 0:
return None
return str(HttpUrl(self.image_url))

def validate_full_screen_image_url(self) -> str | None:
if not isinstance(self.full_screen_image_url, str):
raise ValueError("Invalid comment full_screen_image_url type expected.")
if len(self.full_screen_image_url) == 0:
return None
return str(HttpUrl(self.full_screen_image_url))

def validate_is_available(self) -> bool:
if not isinstance(self.is_available, bool):
raise ValueError("Invalid comment is available type expected.")
return self.is_available
58 changes: 1 addition & 57 deletions src/recommendation_engine/app/domain/menu/values/menu.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,8 @@
from pydantic import HttpUrl
from dataclasses import dataclass

from .price import Price
from ...mixins import DataclassValidationMixin


@dataclass(frozen=True)
class MenuValue(DataclassValidationMixin):
category: str
product_id: str
name: str
price: Price
description: str
image_url: str | None
full_screen_image_url: str | None
is_available: bool

def validate_category(self) -> str:
if not isinstance(self.category, str):
raise ValueError("Invalid comment category type expected.")
return self.category

def validate_product_id(self) -> str:
if not isinstance(self.product_id, str):
raise ValueError("Invalid comment product id type expected.")
return self.product_id

def validate_name(self) -> str:
if not isinstance(self.name, str):
raise ValueError("Invalid comment name type expected.")
return self.name

def validate_price(self) -> Price:
if not isinstance(self.price, str):
raise ValueError("Invalid comment price type expected.")

amount = self.price.replace("₺", "").replace(",", ".").strip()
amount = float(amount)
return Price(amount=amount, currency="TL")

def validate_description(self) -> str:
if not isinstance(self.description, str):
raise ValueError("Invalid comment description type expected.")
return self.description

def validate_image_url(self) -> str | None:
if not isinstance(self.image_url, str):
raise ValueError("Invalid comment image url type expected.")
if len(self.image_url) == 0:
return None
return str(HttpUrl(self.image_url))

def validate_full_screen_image_url(self) -> str | None:
if not isinstance(self.full_screen_image_url, str):
raise ValueError("Invalid comment full_screen_image_url type expected.")
if len(self.full_screen_image_url) == 0:
return None
return str(HttpUrl(self.full_screen_image_url))

def validate_is_available(self) -> bool:
if not isinstance(self.is_available, bool):
raise ValueError("Invalid comment is available type expected.")
return self.is_available
pass
19 changes: 0 additions & 19 deletions src/recommendation_engine/app/domain/menu/values/menu_stack.py

This file was deleted.

28 changes: 0 additions & 28 deletions src/recommendation_engine/app/domain/menu/values/request_value.py

This file was deleted.

12 changes: 9 additions & 3 deletions src/recommendation_engine/app/domain/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ class RequestValue:
method: Literal["GET", "POST"] = "POST"
template_loc: Literal["params", "body"] = "body"

def _format_template(self, template_params: dict) -> dict:
def _format_template(self, template_params: dict) -> dict | None:
if not self.template:
return None

return eval(self.template.strip().format(**template_params))

def retrieve_formatted_request(self, template_params: dict) -> dict:
return {
formatted_req = {
"url": self.url,
"method": self.method,
"headers": self.headers,
self.template_loc: self._format_template(template_params),
}
template_param = self._format_template(template_params)
if template_param is not None:
formatted_req[self.template_loc] = template_param
return formatted_req
3 changes: 2 additions & 1 deletion src/recommendation_engine/app/domain/value_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


class EntityValueStack:
_values = []
def __init__(self):
self._values = []

def retrieve_values(self) -> list[Z]:
return self._values
Expand Down

0 comments on commit 8758b98

Please sign in to comment.