1313API_BASE_URL = "http://aind-metadata-service/api/v2/instrument"
1414
1515
16- def get_instrument (instrument_id : str , modification_date : Optional [str ] = None ) -> Optional [dict ]: # pragma: no cover
16+ def get_instrument (
17+ instrument_id : str , modification_date : Optional [str ] = None , suppress_warning : bool = False
18+ ) -> Optional [dict ]: # pragma: no cover
1719 """Get instrument.
1820
1921 Gets the latest record by default, or a specific record if modification_date is provided.
@@ -24,6 +26,8 @@ def get_instrument(instrument_id: str, modification_date: Optional[str] = None)
2426 Instrument identifier.
2527 modification_date : Optional[str]
2628 Optional modification date (YYYY-MM-DD format). If None, returns latest record.
29+ suppress_warning : bool
30+ If True, suppress warning when modification_date not found.
2731
2832 Returns
2933 -------
@@ -48,21 +52,22 @@ def get_instrument(instrument_id: str, modification_date: Optional[str] = None)
4852 for record in matching_records :
4953 if record .get ("modification_date" ) == modification_date :
5054 return record
51- # No matching date found - log available dates
52- available_dates = sorted (
53- set (r .get ("modification_date" ) for r in matching_records if r .get ("modification_date" ))
54- )
55- logger .warning (
56- f"No record found for instrument_id '{ instrument_id } ' with modification_date '{ modification_date } '. "
57- f"Available dates: { available_dates } "
58- )
55+ # No matching date found - log available dates (unless suppressed)
56+ if not suppress_warning :
57+ available_dates = sorted (
58+ set (r .get ("modification_date" ) for r in matching_records if r .get ("modification_date" ))
59+ )
60+ logger .warning (
61+ f"No record found for instrument_id '{ instrument_id } ' with modification_date '{ modification_date } '. "
62+ f"Available dates: { available_dates } "
63+ )
5964 return None
6065 else :
6166 # Return latest record
6267 return sorted (matching_records , key = lambda record : record ["modification_date" ])[- 1 ]
6368
6469
65- def save_instrument (instrument_model : instrument .Instrument ) -> None : # pragma: no cover
70+ def save_instrument (instrument_model : instrument .Instrument , replace : bool = False ) -> None : # pragma: no cover
6671 """Save instrument and validate round-trip.
6772
6873 Saves the instrument, then retrieves it back and verifies that what we get back
@@ -72,6 +77,8 @@ def save_instrument(instrument_model: instrument.Instrument) -> None: # pragma:
7277 ----------
7378 instrument_model : instrument.Instrument
7479 Instrument to POST.
80+ replace : bool
81+ If True, overwrite existing record with same instrument_id and modification_date.
7582
7683 Raises
7784 ------
@@ -83,7 +90,8 @@ def save_instrument(instrument_model: instrument.Instrument) -> None: # pragma:
8390 # Use model_dump_json() and parse to ensure dates are properly serialized
8491 source_dict = json .loads (instrument_model .model_dump_json ())
8592 logger .info (f"POSTing instrument to { API_BASE_URL } " )
86- response = requests .post (API_BASE_URL , json = source_dict )
93+ params = {"replace" : "true" } if replace else {}
94+ response = requests .post (API_BASE_URL , json = source_dict , params = params )
8795 # POST 400 is always an error (e.g., "Record already exists")
8896 if response .status_code == 400 :
8997 error_msg = response .json ().get ("message" , response .text )
@@ -108,6 +116,28 @@ def save_instrument(instrument_model: instrument.Instrument) -> None: # pragma:
108116 raise ValueError ("Round-trip test failed: Source and read-back instruments differ" )
109117
110118
119+ def check_existing_instrument (
120+ instrument_id : str ,
121+ modification_date : str ,
122+ ) -> bool : # pragma: no cover
123+ """Check if an instrument with the same ID and modification_date already exists.
124+
125+ Parameters
126+ ----------
127+ instrument_id : str
128+ Instrument identifier.
129+ modification_date : str
130+ Modification date (YYYY-MM-DD format).
131+
132+ Returns
133+ -------
134+ bool
135+ True if a record with the same instrument_id and modification_date exists.
136+ """
137+ existing = get_instrument (instrument_id , modification_date = modification_date , suppress_warning = True )
138+ return existing is not None
139+
140+
111141def check_instrument_id (
112142 instrument_id : str ,
113143 skip_confirmation : bool = False ,
0 commit comments