Skip to content

Commit cc37332

Browse files
authored
Merge pull request #36 from microbiomedata/copilot/fix-35
Add batch minting support to Minter class with optional count parameter
2 parents 3a63121 + 8a65776 commit cc37332

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

nmdc_api_utilities/minter.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

nmdc_api_utilities/test/test_mint.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,55 @@
33
import logging
44
import os
55
from dotenv import load_dotenv
6+
import pytest
67

78
load_dotenv()
89
ENV = os.getenv("ENV")
910
CLIENT_ID = os.getenv("CLIENT_ID")
1011
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
1112

1213

13-
def test_mint():
14+
def test_mint_single():
15+
"""Test minting a single ID (default behavior)."""
1416
mint = Minter(env=ENV)
1517
results = mint.mint("nmdc:DataObject", CLIENT_ID, CLIENT_SECRET)
1618
assert results
19+
assert isinstance(results, str)
1720
assert "nmdc:dobj" in results
21+
22+
23+
def test_mint_single_explicit():
24+
"""Test minting a single ID with explicit count=1."""
25+
mint = Minter(env=ENV)
26+
results = mint.mint("nmdc:DataObject", CLIENT_ID, CLIENT_SECRET, count=1)
27+
assert results
28+
assert isinstance(results, str)
29+
assert "nmdc:dobj" in results
30+
31+
32+
def test_mint_multiple():
33+
"""Test minting multiple IDs."""
34+
mint = Minter(env=ENV)
35+
results = mint.mint("nmdc:DataObject", CLIENT_ID, CLIENT_SECRET, count=3)
36+
assert results
37+
assert isinstance(results, list)
38+
assert len(results) == 3
39+
for result in results:
40+
assert isinstance(result, str)
41+
assert "nmdc:dobj" in result
42+
43+
44+
def test_mint_invalid_count():
45+
"""Test that invalid count values raise ValueError."""
46+
mint = Minter(env=ENV)
47+
with pytest.raises(ValueError, match="count must be at least 1"):
48+
mint.mint("nmdc:DataObject", CLIENT_ID, CLIENT_SECRET, count=0)
49+
50+
with pytest.raises(ValueError, match="count must be at least 1"):
51+
mint.mint("nmdc:DataObject", CLIENT_ID, CLIENT_SECRET, count=-1)
52+
53+
54+
# Keep the old test name for backward compatibility
55+
def test_mint():
56+
"""Original test for backward compatibility."""
57+
test_mint_single()

0 commit comments

Comments
 (0)