Coverage for fio_wrapper/endpoints/endpoints_v1/planet.py: 100%
37 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 planet information from FIO.
2"""
3from typing import List, Optional
4from fio_wrapper.endpoints.abstracts.abstract_endpoint import AbstractEndpoint
5from fio_wrapper.endpoints.abstracts.abstract_planet import AbstractPlanet
6from fio_wrapper.exceptions import (
7 PlanetNotFound,
8 PlanetSearchDistanceChecksInvalid,
9 PlanetSearchInvalidRequest,
10 PlanetSearchMaterialsInvalid,
11)
12from fio_wrapper.models.planet_models import (
13 PlanetFull,
14 PlanetFullList,
15 PlanetList,
16 PlanetSiteList,
17)
18from fio_wrapper.validators import (
19 validate_planet_search_distance_checks,
20 validate_planet_search_materials,
21)
24class Planet(AbstractPlanet, AbstractEndpoint):
25 """Planet endpoint wrapper"""
27 # /planet/{Planet}
28 def get(self, planet: str, timeout: Optional[float] = None) -> PlanetFull:
29 """Gets full planet data from FIO
31 Args:
32 planet (str): PlanetId, PlanetNaturalId or PlanetName
33 timeout (float, optional): Request timeout in seconds. Defaults to None.
35 Raises:
36 PlanetNotFound: Planet not found
38 Returns:
39 PlanetFull: Full planet information
40 """
41 (status, data) = self.adapter.get(
42 endpoint=self.urls.planet_get_url(planet=planet),
43 err_codes=[204],
44 timeout=timeout,
45 )
47 if status == 200:
48 return PlanetFull.model_validate(data)
49 elif status == 204:
50 raise PlanetNotFound("Planet not found")
52 # /planet/allplanets
53 def all(self, timeout: Optional[float] = None) -> PlanetList:
54 """Gets a list of all Planets with minimal information from FIO
56 Args:
57 timeout (float, optional): Request timeout in seconds. Defaults to None.
59 Returns:
60 PlanetList: List of Planets as List[Planet]
61 """
62 (_, data) = self.adapter.get(
63 endpoint=self.urls.planet_all_url(), timeout=timeout
64 )
66 return PlanetList.model_validate(data)
68 # /planet/allplanets/full
69 def full(self, timeout: Optional[float] = None) -> PlanetFullList:
70 """Gets a list of all planets from FIO with full planet information
72 Args:
73 timeout (float, optional): Request timeout in seconds. Defaults to None.
75 Returns:
76 PlanetFullList: List of Planets with full information as List[PlanetFull]
77 """
78 (_, data) = self.adapter.get(
79 endpoint=self.urls.planet_full_url(), timeout=timeout
80 )
82 return PlanetFullList.model_validate(data)
84 # /planet/sites/{Planet}
85 def sites(self, planet: str, timeout: Optional[float] = None) -> PlanetSiteList:
86 """Gets a list of sites on the planet from FIO
88 Args:
89 planet (str): PlanetId, PlanetNaturalId or PlanetName
90 timeout (float, optional): Request timeout in seconds. Defaults to None.
92 Raises:
93 PlanetNotFound: Planet not found
95 Returns:
96 PlanetSiteList: List of Planet sites as List[PlanetSite]
97 """
98 (status, data) = self.adapter.get(
99 endpoint=self.urls.planet_sites_url(planet=planet),
100 err_codes=[204],
101 timeout=timeout,
102 )
104 if status == 200:
105 return PlanetSiteList.model_validate(data)
106 elif status == 204:
107 raise PlanetNotFound("Planet not found")
109 # /planet/search
110 def search(
111 self,
112 materials: List[str] = None,
113 include_rocky: bool = False,
114 include_gaseous: bool = False,
115 include_low_gravity: bool = False,
116 include_high_gravity: bool = False,
117 include_low_pressure: bool = False,
118 include_high_pressure: bool = False,
119 include_low_temperature: bool = False,
120 include_high_temperature: bool = False,
121 must_be_fertile: bool = False,
122 must_have_localmarket: bool = False,
123 must_have_cogc: bool = False,
124 must_have_war: bool = False,
125 must_have_adm: bool = False,
126 must_have_shy: bool = False,
127 distance_checks: List[str] = None,
128 timeout: Optional[float] = None,
129 ) -> PlanetFullList:
130 """Performs a search request towards FIO to find a planet matching the search parameters
132 Args:
133 materials (List[str], optional): List of materials to search for, e.g. ["FEO", "LST"].
134 include_rocky (bool, optional): Planet can be Rocky.
135 include_gaseous (bool, optional): Planet can be Gaseous.
136 include_low_gravity (bool, optional): Planet can be low gravity.
137 include_high_gravity (bool, optional): Planet can be high gravity.
138 include_low_pressure (bool, optional): Planet can be low pressure.
139 include_high_pressure (bool, optional): Planet can be high pressure.
140 include_low_temperature (bool, optional): Planet can be low temperature.
141 include_high_temperature (bool, optional): Planet can be high temperature.
142 must_be_fertile (bool, optional): Planet must be Fertile.
143 must_have_localmarket (bool, optional): Planet must have a Local Market.
144 must_have_cogc (bool, optional): Planet must have a Chamber of Glboal Commerce.
145 must_have_war (bool, optional): Planet must have warehouses.
146 must_have_adm (bool, optional): Planet must have a Planetary Administration Center.
147 must_have_shy (bool, optional): Planet must have a Shipyard.
148 distance_checks (List[str], optional): List of other planets to check distance to, e.g. ["ANT", "MOR"].
149 timeout (float, optional): Request timeout in seconds. Defaults to None.
151 Raises:
152 PlanetSearchMaterialsInvalid: _description_
153 PlanetSearchDistanceChecksInvalid: _description_
154 PlanetSearchInvalidRequest: _description_
156 Returns:
157 PlanetFullList: List of Planets with full information as List[PlanetFull]
158 """
160 # accept None as materials, don't include materials in search
161 if materials is not None:
162 if not validate_planet_search_materials(materials=materials):
163 raise PlanetSearchMaterialsInvalid(
164 "Invalid materials provided. Can check for up to 4 materials."
165 )
167 if distance_checks is not None:
168 if not validate_planet_search_distance_checks(
169 distance_checks=distance_checks
170 ):
171 raise PlanetSearchDistanceChecksInvalid(
172 "Invalid distance checks. Can check for up to 10 distances."
173 )
175 (status, data) = self.adapter.post(
176 endpoint=self.urls.planet_search_url(),
177 data={
178 "Materials": [] if materials is None else materials,
179 "IncludeRocky": include_rocky,
180 "IncludeGaseous": include_gaseous,
181 "IncludeLowGravity": include_low_gravity,
182 "IncludeHighGravity": include_high_gravity,
183 "IncludeLowPressure": include_low_pressure,
184 "IncludeHighPressure": include_high_pressure,
185 "IncludeLowTemperature": include_low_temperature,
186 "IncludeHighTemperature": include_high_temperature,
187 "MustBeFertile": must_be_fertile,
188 "MustHaveLocalMarket": must_have_localmarket,
189 "MustHaveChamberOfCommerce": must_have_cogc,
190 "MustHaveWarehouse": must_have_war,
191 "MustHaveAdministrationCenter": must_have_adm,
192 "MustHaveShipyard": must_have_shy,
193 "DistanceChecks": [] if distance_checks is None else distance_checks,
194 },
195 err_codes=[400],
196 timeout=timeout,
197 )
199 if status == 200:
200 return PlanetFullList.model_validate(data)
201 elif status == 400:
202 raise PlanetSearchInvalidRequest("Failed to parse payload")