@@ -533,10 +533,12 @@ def __handle_databus_file_query__(endpoint_url, query) -> List[str]:
533533 yield value
534534
535535
536- def __handle_databus_file_json__ (json_str : str ) -> List [str ]:
536+ def __handle_databus_artifact_version__ (json_str : str ) -> List [str ]:
537537 """
538538 Parse the JSON-LD of a databus artifact version to extract download URLs.
539539 Don't get downloadURLs directly from the JSON-LD, but follow the "file" links to count access to databus accurately.
540+
541+ Returns a list of download URLs.
540542 """
541543
542544 databusIdUrl = []
@@ -549,6 +551,48 @@ def __handle_databus_file_json__(json_str: str) -> List[str]:
549551 return databusIdUrl
550552
551553
554+ def __get_databus_latest_version_of_artifact__ (json_str : str ) -> str :
555+ """
556+ Parse the JSON-LD of a databus artifact to extract URLs of the latest version.
557+
558+ Returns download URL of latest version of the artifact.
559+ """
560+ json_dict = json .loads (json_str )
561+ versions = json_dict .get ("databus:hasVersion" )
562+
563+ # Single version case {}
564+ if isinstance (versions , dict ):
565+ versions = [versions ]
566+ # Multiple versions case [{}, {}]
567+
568+ version_urls = [v ["@id" ] for v in versions if "@id" in v ]
569+ if not version_urls :
570+ raise ValueError ("No versions found in artifact JSON-LD" )
571+
572+ version_urls .sort (reverse = True ) # Sort versions in descending order
573+ return version_urls [0 ] # Return the latest version URL
574+
575+
576+ def __get_databus_artifacts_of_group__ (json_str : str ) -> List [str ]:
577+ """
578+ Parse the JSON-LD of a databus group to extract URLs of all artifacts.
579+
580+ Returns a list of artifact URLs.
581+ """
582+ json_dict = json .loads (json_str )
583+ artifacts = json_dict .get ("databus:hasArtifact" , [])
584+
585+ result = []
586+ for item in artifacts :
587+ uri = item .get ("@id" )
588+ if not uri :
589+ continue
590+ _ , _ , _ , _ , version , _ = __get_databus_id_parts__ (uri )
591+ if version is None :
592+ result .append (uri )
593+ return result
594+
595+
552596def wsha256 (raw : str ):
553597 return sha256 (raw .encode ('utf-8' )).hexdigest ()
554598
@@ -558,7 +602,7 @@ def __handle_databus_collection__(uri: str) -> str:
558602 return requests .get (uri , headers = headers ).text
559603
560604
561- def __handle_databus_artifact_version__ (uri : str ) -> str :
605+ def __get_json_ld_from_databus__ (uri : str ) -> str :
562606 headers = {"Accept" : "application/ld+json" }
563607 return requests .get (uri , headers = headers ).text
564608
@@ -607,6 +651,7 @@ def download(
607651 client_id: Client ID for token exchange
608652 """
609653
654+ # TODO: make pretty
610655 for databusURI in databusURIs :
611656 host , account , group , artifact , version , file = __get_databus_id_parts__ (databusURI )
612657
@@ -627,15 +672,31 @@ def download(
627672 __download_list__ ([databusURI ], localDir , vault_token_file = token , auth_url = auth_url , client_id = client_id )
628673 # databus artifact version
629674 elif version is not None :
630- json_str = __handle_databus_artifact_version__ (databusURI )
631- res = __handle_databus_file_json__ (json_str )
675+ json_str = __get_json_ld_from_databus__ (databusURI )
676+ res = __handle_databus_artifact_version__ (json_str )
632677 __download_list__ (res , localDir , vault_token_file = token , auth_url = auth_url , client_id = client_id )
633678 # databus artifact
634679 elif artifact is not None :
635- print ("artifactId not supported yet" ) # TODO
680+ json_str = __get_json_ld_from_databus__ (databusURI )
681+ latest = __get_databus_latest_version_of_artifact__ (json_str )
682+ print (f"No version given, using latest version: { latest } " )
683+ json_str = __get_json_ld_from_databus__ (latest )
684+ res = __handle_databus_artifact_version__ (json_str )
685+ __download_list__ (res , localDir , vault_token_file = token , auth_url = auth_url , client_id = client_id )
686+
636687 # databus group
637688 elif group is not None :
638- print ("groupId not supported yet" ) # TODO
689+ json_str = __get_json_ld_from_databus__ (databusURI )
690+ artifacts = __get_databus_artifacts_of_group__ (json_str )
691+ for artifact_uri in artifacts :
692+ print (f"Processing artifact { artifact_uri } " )
693+ json_str = __get_json_ld_from_databus__ (artifact_uri )
694+ latest = __get_databus_latest_version_of_artifact__ (json_str )
695+ print (f"No version given, using latest version: { latest } " )
696+ json_str = __get_json_ld_from_databus__ (latest )
697+ res = __handle_databus_artifact_version__ (json_str )
698+ __download_list__ (res , localDir , vault_token_file = token , auth_url = auth_url , client_id = client_id )
699+
639700 # databus account
640701 elif account is not None :
641702 print ("accountId not supported yet" ) # TODO
0 commit comments