Skip to content

Instantly share code, notes, and snippets.

@djazayeri
Created March 17, 2015 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djazayeri/d3dc89988486fe6f988d to your computer and use it in GitHub Desktop.
Save djazayeri/d3dc89988486fe6f988d to your computer and use it in GitHub Desktop.
Example of using OpenMRS REST to create a patient and an encounter (in OpenMRS 2.1)
// go to http://demo.openmrs.org, log in as admin/Admin123, open the JavaScript console, and do the following:
var URL_BASE = "http://demo.openmrs.org/openmrs/ws/rest/v1/";
var resources = {};
function andLog(data) { console.log(data); }
function andSaveAs(variable) { return function(data) { resources[variable] = data; console.log(variable + " => " + JSON.stringify(data)); } }
function andSaveFirstResultAs(variable) { return function(data) { data = data.results[0]; resources[variable] = data; console.log(variable + " => " + JSON.stringify(data)); } }
function random() { return Math.floor(Math.random() * 10000); }
function post(resource, data, success) {
$.ajax({ type: "POST", url: URL_BASE + resource, contentType: "application/json", data: JSON.stringify(data), success: success });
}
$.getJSON(URL_BASE + "patientidentifiertype", andLog);
// response will be something like this: Object {results: Array[3]} and at least in chrome you can inspect this
$.getJSON(URL_BASE + "patientidentifiertype/05a29f94-c0ed-11e2-94be-8c13b969e334", andSaveAs("openmrsId"));
// Until IDGEN-42 is fixed we can't fetch an autogenerated identifier via proper web service
var identifierToUse;
$.getJSON("http://demo.openmrs.org/openmrs/module/idgen/generateIdentifier.form?source=1", function(data) {
identifierToUse = data.identifiers[0];
});
$.getJSON(URL_BASE + "location", andLog);
$.getJSON(URL_BASE + "location/aff27d58-a15c-49a6-9beb-d30dcfc0c66e", andSaveAs("hospital"));
// wait for any ajax calls to finish
post("patient", {
identifiers: [
{ identifierType: resources.openmrsId.uuid, identifier: identifierToUse, location: resources.hospital.uuid }
],
person: {
gender: "F",
age: 36,
names: [
{ givenName: "Restful", familyName: "" + random() }
]
}
}, andSaveAs("patient"));
// once the AJAX call finishes, we'll have a patient
console.log(resources.patient);
$.getJSON(URL_BASE + "concept?q=WEIGHT+(KG)", andSaveFirstResultAs("weightConcept"));
$.getJSON(URL_BASE + "encountertype?q=vitals", andSaveFirstResultAs("vitalsEncounterType"));
// wait for any ajax calls to finish
// Starting with REST Web Services 2.7 (included in OpenMRS 2.2) you can create a real-time encounter
// by not specifying an encounterDatetime parameter. (RESTWS-463)
// Note that just doing "new Date()" only works as you intend if the client and server are set to the
// same time zone.
var when = '2015-03-14';
post("encounter", {
patient: resources.patient.uuid, // note that you need to post UUIDs here: RESTWS-459
encounterType: resources.vitalsEncounterType.uuid,
location: resources.hospital.uuid,
encounterDatetime: when,
obs: [
{ concept: resources.weightConcept.uuid, value: 71.2 }
]
}, andSaveAs("encounter"));
// once the AJAX call finishes we'll have created an encounter
console.log(resources.encounter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment