def _get_status(self, batch_id, wait):
     try:
         result = self._send_request('batch_statuses?id={}&wait={}'.format(
             batch_id, wait))
         return yaml.safe_load(result)['data'][0]['status']
     except BaseException as err:
         raise HealthCareException(err)
def main(prog_name=os.path.basename(sys.argv[0]), args=None):
    if args is None:
        args = sys.argv[1:]
    parser = create_parser(prog_name)
    args = parser.parse_args(args)

    if args.verbose is None:
        verbose_level = 0
    else:
        verbose_level = args.verbose

    setup_loggers(verbose_level=verbose_level)

    if args.command == 'create_clinic':
        do_create_clinic(args)
    # elif args.command == 'create_claim':
    #     do_create_claim(args)
    elif args.command == 'create_doctor':
        do_create_doctor(args)
    elif args.command == 'create_patient':
        do_create_patient(args)
    elif args.command == 'add_lab_test':
        do_add_lab_test(args)
    elif args.command == 'add_pulse':
        do_add_pulse(args)
    # elif args.command == 'assign_doctor':
    #     do_assign_doctor(args)
    # elif args.command == 'pass_tests':
    #     do_pass_tests(args)
    # elif args.command == 'attend_procedures':
    #     do_attend_procedures(args)
    # elif args.command == 'eat_pills':
    #     do_eat_pills(args)
    # elif args.command == 'next_visit':
    #     do_next_visit(args)
    # elif args.command == 'first_visit':
    #     do_first_visit(args)
    elif args.command == 'list_clinics':
        do_list_clinics(args)
    elif args.command == 'list_doctors':
        do_list_doctors(args)
    elif args.command == 'list_patients':
        do_list_patients(args)
    elif args.command == 'list_lab_test':
        do_list_lab_test(args)
    elif args.command == 'list_pulse':
        do_list_pulse(args)

    # elif args.command == 'list_claims':
    #     do_list_claims(args)
    # elif args.command == 'list_claim_details':
    #     do_list_claim_details(args)
    else:
        raise HealthCareException("invalid command: {}".format(args.command))
    def __init__(self, base_url, keyfile=None):

        self._base_url = base_url

        if keyfile is None:
            self._signer = None
            return

        try:
            with open(keyfile) as fd:
                private_key_str = fd.read().strip()
        except OSError as err:
            raise HealthCareException(
                'Failed to read private key {}: {}'.format(keyfile, str(err)))

        try:
            private_key = Secp256k1PrivateKey.from_hex(private_key_str)
        except ParseError as e:
            raise HealthCareException('Unable to load private key: {}'.format(
                str(e)))

        self._signer = CryptoFactory(create_context('secp256k1')) \
            .new_signer(private_key)
    def _send_request(self, suffix, data=None, content_type=None, name=None):
        if self._base_url.startswith("http://"):
            url = "{}/{}".format(self._base_url, suffix)
        else:
            url = "http://{}/{}".format(self._base_url, suffix)

        headers = {}
        # if auth_user is not None:
        #     auth_string = "{}:{}".format(auth_user, auth_password)
        #     b64_string = base64.b64encode(auth_string.encode()).decode()
        #     auth_header = 'Basic {}'.format(b64_string)
        #     headers['Authorization'] = auth_header

        if content_type is not None:
            headers['Content-Type'] = content_type

        try:
            if data is not None:
                result = requests.post(url, headers=headers, data=data)
            else:
                result = requests.get(url, headers=headers)

            if result.status_code == 404:
                raise HealthCareException("No such operator: {}".format(name))

            elif not result.ok:
                raise HealthCareException("Error {}: {}".format(
                    result.status_code, result.reason))

        except requests.ConnectionError as err:
            raise HealthCareException('Failed to connect to {}: {}'.format(
                url, str(err)))

        except BaseException as err:
            raise HealthCareException(err)

        return result.text
def do_list_pulse(args):
    url = _get_url(args)
    # auth_user, auth_password = _get_auth_info(args)

    client = HealthCareClient(base_url=url, keyfile=None)

    pulse_list = client.list_pulse()

    if pulse_list is not None:
        fmt = "%-15s %-15s %-15s %-15s"
        print(fmt % ('PATIENT HEX', 'PATIENT PKEY', 'PULSE', 'TIMESTAMP'))
        for key, value in pulse_list.items():
            print(fmt % (key, value.public_key, value.pulse, value.timestamp))
    else:
        raise HealthCareException("Could not retrieve pulse listing.")
def do_list_patients(args):
    url = _get_url(args)
    # auth_user, auth_password = _get_auth_info(args)

    client = HealthCareClient(base_url=url, keyfile=None)

    patient_list = client.list_patients()

    if patient_list is not None:
        fmt = "%-15s %-15s %-15s %-15s"
        print(fmt % ('PATIENT HEX', 'NAME', 'SURNAME', 'PUBLIC_KEY'))
        for key, value in patient_list.items():
            print(fmt % (key, value.name, value.surname, value.public_key))
    else:
        raise HealthCareException("Could not retrieve patient listing.")
def do_list_doctors(args):
    url = _get_url(args)
    # auth_user, auth_password = _get_auth_info(args)

    client = HealthCareClient(base_url=url, keyfile=None)

    doctors_list = client.list_doctors()

    if doctors_list is not None:
        fmt = "%-15s %-15s %-15s"
        print(fmt % ('NAME', 'SURNAME', 'PUBLIC_KEY'))
        for doctor in doctors_list:
            print(fmt % (doctor.name, doctor.surname, doctor.public_key))
    else:
        raise HealthCareException("Could not retrieve doctors listing.")
def do_list_lab_test(args):
    url = _get_url(args)
    # auth_user, auth_password = _get_auth_info(args)

    client = HealthCareClient(base_url=url, keyfile=None)

    lab_test_list = client.list_lab_test()

    if lab_test_list is not None:
        fmt = "%-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s"
        print(fmt % ('CLINIC HEX', 'HEIGHT', 'WEIGHT', 'GENDER', 'A/G RATIO',
                     'ALBUMIN', 'ALKALINE PHOSPHATASE', 'APPEARANCE',
                     'BILIRUBIN', 'CASTS', 'COLOR'))
        for key, value in lab_test_list.items():
            print(
                fmt %
                (key, value.height, value.weight, value.gender,
                 value.a_g_ratio, value.albumin, value.alkaline_phosphatase,
                 value.appearance, value.bilirubin, value.casts, value.color))
    else:
        raise HealthCareException("Could not retrieve lab test listing.")