Coverage for fio_wrapper/endpoints/endpoints_v1/material.py: 100%
25 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-16 11:50 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-16 11:50 +0100
1"""Access material information from FIO.
2"""
3from typing import Optional
4from fio_wrapper.endpoints.abstracts.abstract_endpoint import AbstractEndpoint
5from fio_wrapper.endpoints.abstracts.abstract_material import AbstractMaterial
6from fio_wrapper.validators import validate_ticker
7from fio_wrapper.models.material_models import MaterialTicker, MaterialTickerList
8from fio_wrapper.exceptions import MaterialTickerNotFound, MaterialCategoryNotFound
11class Material(AbstractMaterial, AbstractEndpoint):
12 def _validate_ticker(self, material_ticker: str) -> None:
13 """Validates a material ticker
15 Args:
16 material_ticker (str): Material ticker
17 timeout (float, optional): Request timeout in seconds. Defaults to None.
19 Raises:
20 MaterialTickerInvalid: Material ticker can't be None type
21 MaterialTickerInvalid: Material ticker can't be longer than 3 characters
22 MaterialTickerInvalid: Material ticker can't be shorter than 1 character
23 MaterialTickerInvalid: Material ticker can't contain spaces
24 """
25 validate_ticker(material_ticker=material_ticker)
27 def get(
28 self, material_ticker: str, timeout: Optional[float] = None
29 ) -> MaterialTicker:
30 """Gets a single material from FIO
32 Args:
33 material_ticker (str): Material Ticker (e.g., "DW")
34 timeout (float, optional): Request timeout in seconds. Defaults to None.
36 Raises:
37 MaterialTickerNotFound: Material Ticker was not found
39 Returns:
40 MaterialModel: Material
41 """
43 self._validate_ticker(material_ticker=material_ticker)
45 (status, data) = self.adapter.get(
46 endpoint=self.urls.material_get_url(material_ticker=material_ticker),
47 err_codes=[204],
48 timeout=timeout,
49 )
51 if status == 200:
52 return MaterialTicker.model_validate(data)
53 elif status == 204:
54 raise MaterialTickerNotFound("Materialticker not found")
56 def all(self, timeout: Optional[float] = None) -> MaterialTickerList:
57 """Gets all materials from FIO
59 Args:
60 timeout (float, optional): Request timeout in seconds. Defaults to None.
62 Returns:
63 MaterialModelList: List of Materials as List[MaterialModel]
64 """
65 (_, data) = self.adapter.get(
66 endpoint=self.urls.material_allmaterials_url(), timeout=timeout
67 )
68 return MaterialTickerList.model_validate(data)
70 def category(
71 self, category_name: str, timeout: Optional[float] = None
72 ) -> MaterialTickerList:
73 """Gets all materials of specified category
75 Args:
76 category_name (str): Category name (e.g., "agricultural products")
77 timeout (float, optional): Request timeout in seconds. Defaults to None.
79 Raises:
80 MaterialCategoryNotFound: Category was not found
82 Returns:
83 MaterialModelList: List of Materials as List[MaterialModel]
84 """
85 (status, data) = self.adapter.get(
86 endpoint=self.urls.material_get_category(category_name=category_name),
87 err_codes=[204],
88 timeout=timeout,
89 )
91 if status == 200 and len(data) > 0:
92 return MaterialTickerList.model_validate(data)
93 elif status == 204 or len(data) == 0:
94 raise MaterialCategoryNotFound("Material category not found")