| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import typing
- import collections.abc
- from .product import product
- from .exception import not_ready_exception
- class product_response:
- def __new__(
- cls,
- target: product | typing.Iterable[product]
- ) -> list | dict:
- if isinstance(target, product):
- return cls.single(target)
- if isinstance(target, collections.abc.Iterable):
- return cls.collection(target)
- raise TypeError("Bad type for product response generator.")
- def single(target: product) -> dict:
- if not product.ready:
- raise not_ready_exception(target)
- return {
- "name": product.name,
- "description": product.description,
- "author": product.author,
- "image": product.image,
- "stock_count": product.stock_count,
- "barcode": product.barcode
- }
- def collection(targets: typing.Iterable[product]) -> list:
- result = list()
- for count in targets:
- if not isinstance(count, product):
- raise TypeError("Products iterable must contain products.")
- result.append(product_response.single(count))
- return result
|