Select Git revision
Forked from
Brady James Garvin / rest_with_post
1 commit behind the upstream repository.
Brady James Garvin authored
main.py 2.58 KiB
from kivy.app import App
from kivy.logger import Logger
from openmrs import RESTConnection, RESTCommunication
class RestApp(App):
def __init__(self, **kwargs):
super(RestApp, self).__init__(**kwargs)
self.openmrs_connection = RESTConnection('localhost', 8080, 'admin', 'Admin123')
def create_lincoln_hospital(self):
self.openmrs_connection.send_request('location', {
'q': 'Lincoln Hospital',
}, None, self.on_lincoln_hospital_queried, self.on_failure, self.on_failure)
def on_lincoln_hospital_queried(self, _, response):
matches = [result for result in response['results'] if result['display'] == 'Lincoln Hospital']
if len(matches) > 0:
print(f'Location "Lincoln Hospital" already exists: {matches[0]["uuid"]}')
else:
self.openmrs_connection.send_request('location', None, {
'name': 'Lincoln Hospital'
}, self.on_lincoln_hospital_created, self.on_failure, self.on_failure)
# noinspection PyMethodMayBeStatic
def on_lincoln_hospital_created(self, _, response): # pylint: disable=no-self-use
print(f'Location "Lincoln Hospital" created: {response["uuid"]}')
def create_health_concept(self):
def coroutine(communication):
datatypes = yield from communication.get_uuids('conceptdatatype')
classes = yield from communication.get_uuids('conceptclass')
concepts = yield from communication.get_uuids('concept')
try:
numeric = datatypes['Numeric']
finding = classes['Finding']
except KeyError as exception:
communication.on_failure(None, exception)
return
health_concept_uuid = concepts.get('Health')
if health_concept_uuid is None:
health_concept = yield from communication.send_request('concept', post_parameters={
'names': [{'name': 'Health', 'locale': 'en'}],
'datatype': numeric,
'conceptClass': finding,
})
health_concept_uuid = health_concept['uuid']
print(f'Concept "Health" created: {health_concept_uuid}')
else:
print(f'Concept "Health" already exists: {health_concept_uuid}')
RESTCommunication(coroutine, self.openmrs_connection, self.on_failure).run()
# noinspection PyMethodMayBeStatic
def on_failure(self, _, error): # pylint: disable=no-self-use
Logger.error(f'RestApp: {error}')
if __name__ == '__main__':
app = RestApp()
app.run()