@@ -17,9 +17,11 @@ class Minter(NMDCSearch):
1717 def __init__ (self , env = "prod" ):
1818 super ().__init__ (env = env )
1919
20- def mint (self , nmdc_type : str , client_id : str , client_secret : str ) -> str :
20+ def mint (
21+ self , nmdc_type : str , client_id : str , client_secret : str , count : int = 1
22+ ) -> str | list [str ]:
2123 """
22- Mint a new identifier for a collection.
24+ Mint new identifier(s) for a collection.
2325
2426 Parameters
2527 ----------
@@ -30,16 +32,21 @@ def mint(self, nmdc_type: str, client_id: str, client_secret: str) -> str:
3032 The client ID for the NMDC API.
3133 client_secret : str
3234 The client secret for the NMDC API.
35+ count : int, optional
36+ The number of identifiers to mint. Default is 1.
3337
3438 Returns
3539 -------
36- str
37- The minted identifier.
40+ str or list[str]
41+ If count is 1, returns a single minted identifier as a string.
42+ If count is greater than 1, returns a list of minted identifiers.
3843
3944 Raises
4045 ------
4146 RuntimeError
4247 If the API request fails.
48+ ValueError
49+ If count is less than 1.
4350
4451 Notes
4552 -----
@@ -48,6 +55,10 @@ def mint(self, nmdc_type: str, client_id: str, client_secret: str) -> str:
4855 Do not hard code these values in your code.
4956
5057 """
58+ # Validate count parameter
59+ if count < 1 :
60+ raise ValueError ("count must be at least 1" )
61+
5162 # get the token
5263 client = oauthlib .oauth2 .BackendApplicationClient (client_id = client_id )
5364 oauth = requests_oauthlib .OAuth2Session (client = client )
@@ -57,7 +68,7 @@ def mint(self, nmdc_type: str, client_id: str, client_secret: str) -> str:
5768 client_secret = client_secret ,
5869 )
5970 url = f"{ self .base_url } /pids/mint"
60- payload = {"schema_class" : {"id" : nmdc_type }, "how_many" : 1 }
71+ payload = {"schema_class" : {"id" : nmdc_type }, "how_many" : count }
6172 try :
6273 response = oauth .post (url , data = json .dumps (payload ))
6374 response .raise_for_status ()
@@ -69,4 +80,8 @@ def mint(self, nmdc_type: str, client_id: str, client_secret: str) -> str:
6980 f"API request response: { response .json ()} \n API Status Code: { response .status_code } "
7081 )
7182 # return the response
72- return response .json ()[0 ]
83+ response_data = response .json ()
84+ if count == 1 :
85+ return response_data [0 ]
86+ else :
87+ return response_data
0 commit comments