77
88from twyn .trusted_packages .cache_handler import CacheEntry , CacheHandler
99from twyn .trusted_packages .exceptions import (
10+ EmptyPackagesListError ,
1011 InvalidJSONError ,
1112)
1213
@@ -28,26 +29,19 @@ def __init__(self, source: Optional[str] = None, cache_handler: Union[CacheHandl
2829 self .source = source or self .DEFAULT_SOURCE
2930 self .cache_handler = cache_handler
3031
31- @staticmethod
32- @abstractmethod
33- def _parse (packages_json : dict [str , Any ]) -> set [str ]:
34- """Parse and retrieve the packages within the given json structure."""
35-
3632 @staticmethod
3733 @abstractmethod
3834 def normalize_packages (packages : set [str ]) -> set [str ]:
3935 """Normalize package names to make sure they're valid within the package manager context."""
4036
4137 def _download (self ) -> dict [str , Any ]:
42- packages = requests .get (self .source )
43- packages .raise_for_status ()
38+ response = requests .get (self .source )
39+ response .raise_for_status ()
40+
4441 try :
45- packages_json : dict [ str , Any ] = packages .json ()
42+ return response .json ()
4643 except requests .exceptions .JSONDecodeError as err :
4744 raise InvalidJSONError from err
48- else :
49- logger .debug ("Successfully downloaded trusted packages list from %s" , self .source )
50- return packages_json
5145
5246 def _save_trusted_packages_to_cache_if_enabled (self , packages : set [str ]) -> None :
5347 """Save trusted packages using CacheHandler."""
@@ -69,18 +63,24 @@ def _get_packages_from_cache_if_enabled(self) -> set[str]:
6963 return cache_entry .packages
7064
7165 def get_packages (self ) -> set [str ]:
72- """Download and parse online source of top Python Package Index packages."""
73- packages_to_use = set ()
74- packages_to_use = self ._get_packages_from_cache_if_enabled ()
66+ """Download and parse online source of top packages from the package ecosystem."""
67+ packages = self ._get_packages_from_cache_if_enabled ()
7568 # we don't save the cache here, we keep it as it is so the date remains the original one.
76-
77- if not packages_to_use :
69+ if not packages :
7870 # no cache usage, no cache hit (non-existent or outdated) or cache was empty.
7971 logger .info ("Fetching trusted packages from trusted packages reference..." )
80- packages_to_use = self ._parse (self ._download ())
72+ data = self ._download ()
73+ try :
74+ packages = set (data ["packages" ])
75+ except KeyError as err :
76+ raise InvalidJSONError ("`packages` key not in JSON." ) from err
77+
78+ logger .debug ("Successfully downloaded trusted packages list from %s" , self .source )
79+ if not packages :
80+ raise EmptyPackagesListError
8181
8282 # New packages were downloaded, we create a new entry updating all values.
83- self ._save_trusted_packages_to_cache_if_enabled (packages_to_use )
83+ self ._save_trusted_packages_to_cache_if_enabled (packages )
8484
85- normalized_packages = self .normalize_packages (packages_to_use )
85+ normalized_packages = self .normalize_packages (packages )
8686 return normalized_packages
0 commit comments