medic / chis-interoperability

5 stars 1 forks source link

Script to create dummy ServiceRequests #35

Closed kaapstorm closed 3 years ago

kaapstorm commented 3 years ago

[edit] Added patient_name

#!/usr/bin/env python3
from datetime import date, timedelta
from random import choice
import requests

BASE_URL = 'http://localhost:8425/hapi-fhir-jpaserver/fhir/'  # ends in '/'

GIVEN_NAMES = 'Alice Bethany Claire Deborah Eilidh Francesca'.split()
FAMILY_NAMES = 'Apple Barker Carter Davenport Erridge Franks'.split()
NOTE = 'Patient missed appt. Pls follow up.'

def add_patient():
    given_name = choice(GIVEN_NAMES)
    family_name = choice(FAMILY_NAMES)
    full_name = f'{given_name} {family_name}'
    date_of_birth = date.today() - timedelta(days=365 * 30)
    patient = {
        'resourceType': 'Patient',
        'name': [{
            'given': [given_name],
            'family': family_name,
            'text': full_name,
        }],
        'birthDate': date_of_birth.isoformat(),
    }
    response = requests.post(
        f'{BASE_URL}Patient/',
        json=patient,
        headers={'Accept': 'application/json'},
    )
    assert 200 <= response.status_code < 300, response.text
    return response.json()['id'], full_name

def add_service_request(patient_id, patient_name):
    service_request = {
        'resourceType': 'ServiceRequest',
        'status': 'active',
        'intent': 'directive',
        'subject': {
            'reference': f'Patient/{patient_id}',
            'display': patient_name,
        },
        'note': [{
            'text': NOTE,
        }]
    }
    response = requests.post(
        f'{BASE_URL}ServiceRequest/',
        json=service_request,
        headers={'Accept': 'application/json'},
    )
    assert 200 <= response.status_code < 300, response.text

if __name__ == '__main__':
    patient_id, patient_name = add_patient()
    add_service_request(patient_id, patient_name)
kaapstorm commented 3 years ago

@mrjones-plip ... in case it's useful, this is what I'm using to create dummy data.

mrjones-plip commented 3 years ago

This is really helpful - thanks @kaapstorm !