import json

import requests

from tests.mgnt_pre_checkout_routine import PreCheckoutRoutine


class GetArchivedPVs(PreCheckoutRoutine):
    def __init__(self):
        super(GetArchivedPVs, self).__init__(
            test_id='all_archived_pvs',
            description='Get all archived PVs using archivedPVs.py',
            read_only=True
        )
        self.bpl_url = 'http://lcls-archapp.slac.stanford.edu/retrieval/bpl/getMatchingPVs'

    def execute(self):
        self.data = []
        for magnet in self.magnets:
            pattern = magnet['device'] + ':*'
            params = {'pv': pattern}
            response = requests.get(self.bpl_url, params=params)
            self.data.extend(json.loads(response.text))
