@@ -33,13 +33,87 @@ class DataverseDepositPlugin(BaseDepositPlugin):
3333 settings_class = DataverseDepositSettings
3434
3535 def __init__ (self , command , ctx ) -> None :
36+ """
37+ Sets up the DataverseDepositPlugin with data from the hermes toml.
38+ Tests if everything is valid and creates an easyDataverse client.
39+ """
3640 super ().__init__ (command , ctx )
3741 self .config = getattr (self .command .settings , self .platform_name )
42+ self .check_if_all_valid ()
43+ self .client = Dataverse (server_url = self .config .site_url , api_token = self .config .api_token )
44+ self .ctx_path = ContextPath .parse (f"deposit.{ self .platform_name } " )
45+
46+ def check_if_all_valid (self ) -> None :
47+ """
48+ Tests if all conditions are met before starting the rest of the deposit.
49+ """
50+ self .check_version ()
51+ self .check_api_token ()
52+ self .check_target_collection ()
53+ self .check_target_pid ()
54+ self .check_publication_type ()
55+
56+ def check_version (self ) -> None :
57+ """
58+ Tests if the site_url is reachable as dataverse instance.
59+ Also saves the dataverse version in the context incase we want to use it later on.
60+ """
61+ url = f"{ self .config .site_url } /api/info/version"
62+ res = requests .get (url )
63+ if not res .ok :
64+ raise RuntimeError (f"Dataverse ({ self .config .site_url } ) not reachable." )
65+ version_info = res .json ().get ("data" , {}).get ("version" , "" )
66+ self .ctx .update (self .ctx_path ["dataverse_version" ], version_info )
67+
68+ def check_api_token (self ) -> None :
3869 api_token = self .config .api_token
3970 if not api_token :
40- raise DepositionUnauthorizedError ("No valid auth token given for deposition platform" )
41- self .client = Dataverse (server_url = self .config .site_url , api_token = api_token )
42- self .ctx_path = ContextPath .parse (f"deposit.{ self .platform_name } " )
71+ raise DepositionUnauthorizedError ("No api-token given for deposition platform (dataverse)." )
72+ token_valid_url = f"{ self .config .site_url } /api/users/token"
73+ token_valid_response = requests .get (token_valid_url , headers = {"X-Dataverse-key" : api_token })
74+ if not token_valid_response .ok :
75+ raise DepositionUnauthorizedError ("Given api-token for deposition platform (dataverse) is not valid." )
76+
77+ def check_target_collection (self ) -> None :
78+ """
79+ Tests if the target collection exists.
80+ """
81+ target_collection = self .config .target_collection
82+ url = f"{ self .config .site_url } /api/dataverses/{ target_collection } "
83+ res = requests .get (url )
84+ if not res .ok :
85+ raise RuntimeError (f"Dataverse collection '{ target_collection } ' not found." )
86+
87+ def check_target_pid (self ) -> None :
88+ """
89+ Tests if the given pid is valid.
90+ """
91+ if not self .config .target_pid :
92+ return
93+ url = f"{ self .config .site_url } /api/datasets/:persistentId/?persistentId={ self .config .target_pid } "
94+ res = requests .get (url )
95+ if not res .ok :
96+ raise RuntimeError (f"Dataset { self .config .target_pid } not found." )
97+ data = res .json ().get ("data" , {})
98+ if self .config .target_collection and not data .get ("ownerAlias" ) == self .config .target_collection :
99+ _log .warning ("Dataset is not located inside the target collection." )
100+
101+ def check_publication_type (self ) -> None :
102+ """
103+ Tests if the given publication type (most likely "software") is supported by the target dataverse.
104+ """
105+ url = f"{ self .config .site_url } /api/datasets/datasetTypes"
106+ res = requests .get (url )
107+ if res .ok :
108+ types = res .json ().get ("data" , [])
109+ type_names = [t ["name" ] for t in types ]
110+ if self .config .publication_type not in type_names :
111+ raise RuntimeError (
112+ f"Publication type '{ self .config .publication_type } ' not supported on target Dataverse." )
113+ else :
114+ # TBD what to do when showing supported datasetTypes does not work?
115+ # This is currently the case for https://data.fz-juelich.de/ & https://data-beta.fz-juelich.de/
116+ pass
43117
44118 def map_metadata (self ) -> None :
45119 """
@@ -54,7 +128,7 @@ def map_metadata(self) -> None:
54128 def is_initial_publication (self ) -> bool :
55129 return self .config .target_pid is None
56130
57- def update_metadata_on_dataset (self , dataset : Dataset ):
131+ def update_metadata_on_dataset (self , dataset : Dataset ) -> None :
58132 """
59133 Sets metadata on an easyDataverse.Dataset using the depositionMetadata
60134 """
0 commit comments