Coverage for fio_wrapper/endpoints/endpoints_v1/localmarket.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-16 11:50 +0100

1"""Access local market information from FIO. 

2""" 

3from typing import Tuple, Optional 

4from fio_wrapper.endpoints.abstracts.abstract_endpoint import AbstractEndpoint 

5from fio_wrapper.endpoints.abstracts.abstract_localmarket import AbstractLocalMarket 

6from fio_wrapper.exceptions import ( 

7 CompanyOrAdsNotFound, 

8 PlanetNotFound, 

9 PlanetOrAdsNotFound, 

10) 

11 

12from fio_wrapper.models.localmarket_models import ( 

13 LocalMarketAdList, 

14 LocalMarketAds, 

15 LocalMarketShippingAdList, 

16) 

17from fio_wrapper.validators import validate_localmarket_adtype 

18 

19 

20class LocalMarket(AbstractLocalMarket, AbstractEndpoint): 

21 # /localmarket/planet/{Planet} 

22 def planet(self, planet: str, timeout: Optional[float] = None) -> LocalMarketAds: 

23 """Gets local market ads for planet 

24 

25 Args: 

26 planet (str): PlanetId, PlanetNaturalId, PlanetName 

27 timeout (float, optional): Request timeout in seconds. Defaults to None. 

28 

29 Raises: 

30 PlanetNotFound: Planet not found 

31 

32 Returns: 

33 LocalMarketAds: List of ads 

34 """ 

35 (status, data) = self.adapter.get( 

36 endpoint=self.urls.localmarket_planet_url(planet), 

37 err_codes=[204], 

38 timeout=timeout, 

39 ) 

40 

41 if status == 200: 

42 return LocalMarketAds.model_validate(data) 

43 elif status == 204: 

44 raise PlanetNotFound("Planet not found") 

45 

46 def _planet_type( 

47 self, planet: str, adtype: str, timeout: Optional[float] = None 

48 ) -> Tuple[int, any]: 

49 validate_localmarket_adtype(adtype=adtype) 

50 

51 return self.adapter.get( 

52 endpoint=self.urls.localmarket_planet_type_url( 

53 planet=planet, adtype=adtype 

54 ), 

55 err_codes=[204], 

56 timeout=timeout, 

57 ) 

58 

59 # /localmarket/planet/{Planet}/{Type} 

60 def planet_buy( 

61 self, planet: str, timeout: Optional[float] = None 

62 ) -> LocalMarketAdList: 

63 """Gets all BUY ads from the planets local market 

64 

65 Args: 

66 planet (str): PlanetId, PlanetNaturalId, PlanetName 

67 timeout (float, optional): Request timeout in seconds. Defaults to None. 

68 

69 Raises: 

70 PlanetOrAdsNotFound: Planet not found or no ads 

71 

72 Returns: 

73 LocalMarketAdList: List of planet local market BUY ads 

74 """ 

75 (status, data) = self._planet_type(planet=planet, adtype="BUY", timeout=timeout) 

76 

77 if status == 200: 

78 return LocalMarketAdList.model_validate(data) 

79 elif status == 204: 

80 raise PlanetOrAdsNotFound("Planet not found or no ads on planet") 

81 

82 def planet_sell( 

83 self, planet: str, timeout: Optional[float] = None 

84 ) -> LocalMarketAdList: 

85 """Gets all SELL ads from planets local market 

86 

87 Args: 

88 planet (str): PlanetId, PlanetNaturalId, PlanetName 

89 timeout (float, optional): Request timeout in seconds. Defaults to None. 

90 

91 Raises: 

92 PlanetOrAdsNotFound: Planet not found or no ads 

93 

94 Returns: 

95 LocalMarketAdList: List of planet local market SELL ads 

96 """ 

97 (status, data) = self._planet_type( 

98 planet=planet, adtype="SELL", timeout=timeout 

99 ) 

100 

101 if status == 200: 

102 return LocalMarketAdList.model_validate(data) 

103 elif status == 204: 

104 raise PlanetOrAdsNotFound("Planet not found or no ads on planet") 

105 

106 def planet_shipping( 

107 self, planet: str, timeout: Optional[float] = None 

108 ) -> LocalMarketShippingAdList: 

109 """Gets a list of planets shipping ads 

110 

111 Args: 

112 planet (str): PlanetId, PlanetNaturalId, PlanetName 

113 timeout (float, optional): Request timeout in seconds. Defaults to None. 

114 

115 Raises: 

116 PlanetOrAdsNotFound: Planet not found or no ads 

117 

118 Returns: 

119 LocalMarketShippingAdList: List of planet local market SHIPPING ads 

120 """ 

121 (status, data) = self._planet_type( 

122 planet=planet, adtype="SHIP", timeout=timeout 

123 ) 

124 

125 if status == 200: 

126 return LocalMarketShippingAdList.model_validate(data) 

127 elif status == 204: 

128 raise PlanetOrAdsNotFound("Planet not found or no ads on planet") 

129 

130 # /localmarket/shipping/source/{SourcePlanet} 

131 def shipping_from( 

132 self, planet: str, timeout: Optional[float] = None 

133 ) -> LocalMarketShippingAdList: 

134 """Gets a list of SHIPPING ads starting from planet 

135 

136 Args: 

137 planet (str): PlanetId, PlanetNaturalId, PlanetName 

138 timeout (float, optional): Request timeout in seconds. Defaults to None. 

139 

140 Raises: 

141 PlanetOrAdsNotFound: Planet not found or no ads 

142 

143 Returns: 

144 LocalMarketShippingAdList: List of shipping ads from planet 

145 """ 

146 (status, data) = self.adapter.get( 

147 endpoint=self.urls.localmarket_shipping_source_url(planet=planet), 

148 err_codes=[204], 

149 timeout=timeout, 

150 ) 

151 

152 if status == 200: 

153 return LocalMarketShippingAdList.model_validate(data) 

154 elif status == 204: 

155 raise PlanetOrAdsNotFound("Planet not found or no ads on planet") 

156 

157 # /localmarket/shipping/destination/{DestinationPlanet} 

158 def shipping_to( 

159 self, planet: str, timeout: Optional[float] = None 

160 ) -> LocalMarketShippingAdList: 

161 """Gets a list of SHIPPING ads ending at planet 

162 

163 Args: 

164 planet (str): PlanetId, PlanetNaturalId, PlanetName 

165 timeout (float, optional): Request timeout in seconds. Defaults to None. 

166 

167 Raises: 

168 PlanetOrAdsNotFound: Planet not found or no ads 

169 

170 Returns: 

171 LocalMarketShippingAdList: List of shipping ads to planet 

172 """ 

173 (status, data) = self.adapter.get( 

174 endpoint=self.urls.localmarket_shipping_destination_url(planet=planet), 

175 err_codes=[204], 

176 timeout=timeout, 

177 ) 

178 

179 if status == 200: 

180 return LocalMarketShippingAdList.model_validate(data) 

181 elif status == 204: 

182 raise PlanetOrAdsNotFound("Planet not found or no ads on planet") 

183 

184 # /localmarket/company/{Company} 

185 def company( 

186 self, companycode: str, timeout: Optional[float] = None 

187 ) -> LocalMarketAds: 

188 """Gets a list of all ads of the specified company 

189 

190 Args: 

191 companycode (str): Company Code (e.g., "SKYP") 

192 timeout (float, optional): Request timeout in seconds. Defaults to None. 

193 

194 Raises: 

195 CompanyOrAdsNotFound: Company not found or company has no ads 

196 

197 Returns: 

198 LocalMarketAds: List of local market ads of company 

199 """ 

200 (status, data) = self.adapter.get( 

201 endpoint=self.urls.localmarket_company_url(companycode=companycode), 

202 err_codes=[204], 

203 timeout=timeout, 

204 ) 

205 

206 if status == 200: 

207 return LocalMarketAds.model_validate(data) 

208 elif status == 204: 

209 raise CompanyOrAdsNotFound("Company not found or no ads from company")