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

34 statements  

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

1from typing import List, Optional 

2from fio_wrapper.decorator import apikey_required 

3from fio_wrapper.endpoints.abstracts.abstract_endpoint import AbstractEndpoint 

4from fio_wrapper.endpoints.abstracts.abstract_storage import AbstractStorage 

5from fio_wrapper.exceptions import NoStorageData, NotAuthenticated 

6from fio_wrapper.models.storage_models import StorageList, Storage as StorageModel 

7 

8 

9class Storage(AbstractStorage, AbstractEndpoint): 

10 @apikey_required 

11 def get(self, username: str, timeout: Optional[float] = None) -> StorageList: 

12 """Gets users storage data from FIO 

13 

14 Note: 

15 FIO API Key Required 

16 

17 Args: 

18 username (str): Prosperous Universe username 

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

20 

21 Raises: 

22 NoStorageData: Username has no storage data 

23 NotAuthenticated: Not authenticated or no appropiate permissions 

24 

25 Returns: 

26 StorageList: List of storages 

27 """ 

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

29 endpoint=self.urls.storage_get_url(username=username), 

30 err_codes=[204, 401], 

31 timeout=timeout, 

32 ) 

33 

34 if status == 200: 

35 return StorageList.model_validate(data) 

36 

37 elif status == 204: 

38 raise NoStorageData("Username has no storage data") 

39 elif status == 401: 

40 raise NotAuthenticated("Not authenticated or no appropiate permissions") 

41 

42 @apikey_required 

43 def get_specific( 

44 self, username: str, specific: str, timeout: Optional[float] = None 

45 ) -> StorageModel: 

46 """Gets users specific storage data from FIO 

47 

48 Note: 

49 FIO API Key Required 

50 

51 Args: 

52 username (str): Prosperous Universe username 

53 specific (str): StorageId, PlanetId, PlanetNaturalId or PlanetName 

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

55 

56 Raises: 

57 NoStorageData: Username has no storage data 

58 NotAuthenticated: Not authenticated or no appropiate permissions 

59 

60 Returns: 

61 StorageModel: Storage data 

62 """ 

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

64 endpoint=self.urls.storage_get_specific_url( 

65 username=username, specific=specific 

66 ), 

67 err_codes=[204, 401], 

68 timeout=timeout, 

69 ) 

70 

71 if status == 200: 

72 return StorageModel.model_validate(data) 

73 

74 elif status == 204: 

75 raise NoStorageData("Username has no storage data") 

76 elif status == 401: 

77 raise NotAuthenticated("Not authenticated or no appropiate permissions") 

78 

79 @apikey_required 

80 def planets(self, username: str, timeout: Optional[float] = None) -> List[str]: 

81 """Returns a list of storages from FIO 

82 

83 Note: 

84 FIO API Key Required 

85 

86 Args: 

87 username (str): Prosperous Universe username 

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

89 

90 Raises: 

91 NoStorageData: Username has no storage data 

92 NotAuthenticated: Not authenticated or no appropiate permissions 

93 

94 Returns: 

95 List[str]: List of StorageIds 

96 """ 

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

98 endpoint=self.urls.storage_planets_get_url(username=username), 

99 err_codes=[204, 401], 

100 timeout=timeout, 

101 ) 

102 

103 if status == 200: 

104 return data 

105 elif status == 204: 

106 raise NoStorageData("Username has no storage data") 

107 elif status == 401: 

108 raise NotAuthenticated("Not authenticated or no appropiate permissions")