def test_keyfiles_get_users(mock_pims_session): mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.GET_USERS_FOR_KEYFILE_RESPONSE) entrypoint = KeyFiles(mock_pims_session) users = entrypoint.get_users(key_file=KeyFileFactory()) assert len(users) == 2 assert users[1].key == 22
def test_keyfiles_reidentify_chunked(mock_pims_session): mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.KEYFILES_PSEUDONYMS_REIDENTIFY_RESPONSE ) entrypoint = KeyFiles(mock_pims_session) _ = entrypoint.reidentify( key_file=KeyFileFactory(), pseudonyms=[PseudoPatientIDFactory() for _ in range(2000)] ) reidentified_items = mock_pims_session.session.requests_mock.post.mock_calls[0][2]['params']['items'] assert len(reidentified_items) <= 501
def __init__(self, session): """A logged in session to a PIMS server. Main way in client lib of interacting with PIMS Parameters ---------- session: PIMSSession session to use for communicating with PIMS """ self.session = session self.key_files = KeyFiles(session=self.session) self.users = Users(session=self.session)
def test_keyfiles_pseudonymize(mock_pims_session): """Get a pseudonym, mock server response""" mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE) entrypoint = KeyFiles(mock_pims_session) keys = entrypoint.pseudonymize( key_file=KeyFileFactory(), identifiers=[ Identifier(value="Jack de Boer", source="Test"), Identifier(value="Chris van Os", source="Test"), ], ) assert len(keys) == 2 assert keys[0].identifier.value == "Jack de Boer"
def test_keyfiles_pseudonymize_chunk_size(mock_pims_session): """In live testing the PIMS server was found not to be able to handle API requests with more then 1000 items Check the chunking system to work around this""" mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE ) entrypoint = KeyFiles(mock_pims_session) _ = entrypoint.pseudonymize( key_file=KeyFileFactory(), identifiers=[PatientIDFactory() for _ in range(2000)], ) values_posted = mock_pims_session.session.requests_mock.post.mock_calls[0][2]['json'][0]['values'] assert len(values_posted) <= 1001
def test_keyfiles_pseudonymize_different_sources(mock_pims_session): """Get a pseudonym, mock server response. Two different sources should yield separate calls for each source""" mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE ) entrypoint = KeyFiles(mock_pims_session) keys = entrypoint.pseudonymize( key_file=KeyFileFactory(), identifiers=[ Identifier(value="Jack de Boer", source="Test"), Identifier(value="Sarah Toh", source="Test_2"), ], ) assert mock_pims_session.session.requests_mock.post.call_count == 2
def test_keyfiles_reidentify(mock_pims_session): mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.KEYFILES_PSEUDONYMS_REIDENTIFY_RESPONSE ) entrypoint = KeyFiles(mock_pims_session) keys = entrypoint.reidentify( key_file=KeyFileFactory(), pseudonyms=[ PseudoPatientID(value='test'), PseudoPatientID(value='test2')] ) assert len(keys) == 2 assert keys[0].identifier.source == "PatientID"
def test_keyfiles_entrypoint(mock_pims_session): """Get some object from entrypoint, server is mocked and returning mocked responses """ mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.KEYFILES_FORUSER_RESPONSE) entrypoint = KeyFiles(mock_pims_session) keyfiles = entrypoint.get_all(user=UserFactory()) assert len(keyfiles) == 1 mock_pims_session.session.set_response_tuple( RequestsMockResponseExamples.KEYFILES_RESPONSE) keyfile = entrypoint.get(key=0) assert keyfile.name == "string"
"""Connect to a PIMS server, anonymize some identifyers, then re-identify the pseudonyms you get back To run this example, make sure you have a PIMS keyfile and have set the environment variables PIMS_CLIENT_USER PIMS_CLIENT_PASSWORD """ from pimsclient.server import PIMSServer from pimsclient.swagger import KeyFiles, Identifier api = PIMSServer("https://pims.radboudumc.nl/api") session = api.get_session() keyfiles = KeyFiles(session=session) key_file = keyfiles.get(key=26) print(f"= Pseudonymizing some identifyers in {key_file} ==================") keys = keyfiles.pseudonymize( key_file=key_file, identifiers=[ Identifier("kees2", "PatientID"), Identifier("henk1", "PatientID") ], ) [print(y) for y in [f"{x.pseudonym} -> {x.identifier}" for x in keys]] print(f"= Reidentifying in {key_file} ================") keyfiles.reidentify(key_file=key_file, pseudonyms=[x.pseudonym for x in keys]) [print(y) for y in [f"{x.identifier} -> {x.pseudonym}" for x in keys]]
class PIMSConnection: def __init__(self, session): """A logged in session to a PIMS server. Main way in client lib of interacting with PIMS Parameters ---------- session: PIMSSession session to use for communicating with PIMS """ self.session = session self.key_files = KeyFiles(session=self.session) self.users = Users(session=self.session) def get_key_file(self, key): """Get specific key file Parameters ---------- key: int or str key for the key_file to get Raises ------ PIMSServerException When key file cannot be got for some reason Returns ------- KeyFile """ return self.key_files.get(key) def pseudonymize(self, key_file, identifiers): """get a pseudonym for each identifier. If identifier is known in PIMS, return this. Otherwise, have PIMS generate a new pseudonym and return that. Parameters ---------- key_file: KeyFile The key_file to use identifiers: List[Identifier] The identifiers to get pseudonyms for Returns ------- List[Key] The PIMS pseudonym for each identifier """ return self.key_files.pseudonymize(key_file=key_file, identifiers=identifiers) def reidentify(self, key_file, pseudonyms): """Find the identifiers linked to the given pseudonyms. Parameters ---------- key_file: KeyFile The key_file to use pseudonyms: List[Pseudonym] The pseudonyms to get identifyers for Notes ----- Returned list might be shorter than input list. For unknown pseudonyms no keys are returned Returns ------- List[Key] A list of pseudonym-identifier keys """ return self.key_files.reidentify(key_file=key_file, pseudonyms=pseudonyms)
"""Just some basic interactions with a PIMS server To run this example, make sure you have a PIMS keyfile and have set the environment variables PIMS_CLIENT_USER PIMS_CLIENT_PASSWORD """ from pimsclient.server import PIMSServer from pimsclient.swagger import KeyFiles, Users api = PIMSServer("https://pims.radboudumc.nl/api") session = api.get_session() keyfiles = KeyFiles(session=session) keyfile = keyfiles.get(key=26) print(keyfile) users = Users(session=session) user = users.get(key=26) print(user) all_mine = keyfiles.get_all(user=user) print(all_mine)
class PIMSConnection: def __init__(self, session): """A logged in session to a PIMS server. Main way in client lib of interacting with PIMS Parameters ---------- session: PIMSSession session to use for communicating with PIMS """ self.session = session self.key_files = KeyFiles(session=self.session) self.users = Users(session=self.session) def get_key_file(self, key): """Get specific key file Parameters ---------- key: int or str key for the key_file to get Raises ------ PIMSServerException When key file cannot be got for some reason Returns ------- KeyFile """ return self.key_files.get(key) def pseudonymize(self, key_file: KeyFile, identifiers: List[Identifier]) -> List[Key]: """Get a pseudonym for each identifier. If identifier is known in PIMS, return this. Otherwise, have PIMS generate a new pseudonym and return that. Parameters ---------- key_file: KeyFile The key_file to use identifiers: List[Identifier] The identifiers to get pseudonyms for Returns ------- List[Key] The PIMS pseudonym for each identifier """ return self.key_files.pseudonymize(key_file=key_file, identifiers=identifiers) def reidentify(self, key_file: KeyFile, pseudonyms: List[Pseudonym]) -> List[Key]: """Find the identifiers linked to the given pseudonyms. Parameters ---------- key_file: KeyFile The key_file to use pseudonyms: List[Pseudonym] The pseudonyms to get identifiers for Notes ----- Returned list might be shorter than input list. For unknown pseudonyms no keys are returned Returns ------- List[Key] A list of pseudonym-identifier keys """ return self.key_files.reidentify(key_file=key_file, pseudonyms=pseudonyms) def set_keys(self, key_file: KeyFile, keys: List[Key]): """Manually set the given pseudonym-identifier keys Raises ------ PIMSServerException If any pseudonym or identifier already exists in keyfile """ # PIMS silently skips setting a pseudonym if the identity exists already. # We want to avoid silent skiping, so manually check existing reidentified = self.reidentify(key_file=key_file, pseudonyms=[x.pseudonym for x in keys]) if reidentified: raise PIMSServerException( f"One or more identifiers already exist in keyfile: " f"{[x.describe() for x in reidentified]}. Overwriting would make " f"this keyfile inconsistent") # No identities exist. Start setting return self.key_files.set_keys(key_file=key_file, keys=keys)
"""Connect to a PIMS server, anonymize some identifyers, then re-identify the pseudonyms you get back """ from pimsclient.server import PIMSServer from pimsclient.swagger import KeyFiles, Identifier api = PIMSServer('https://pims.radboudumc.nl/api') session = api.get_session() keyfiles = KeyFiles(session=session) key_file = keyfiles.get(key=26) print(f"= Pseudonymizing some identifyers in {key_file} ==================") keys = keyfiles.pseudonymize(key_file=key_file, identifiers=[Identifier('kees2', 'PatientID'), Identifier('henk1', 'PatientID')]) [print(y) for y in [f"{x.pseudonym} -> {x.identifier}" for x in keys]] print(f"= Reidentifying in {key_file} ================") keyfiles.reidentify(key_file=key_file, pseudonyms=[x.pseudonym for x in keys]) [print(y) for y in [f"{x.identifier} -> {x.pseudonym}" for x in keys]]