Commit 80e1f5ee authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
.idea
*.pyc
*.pyo
*.apk
from kivy.app import App
from kivy.uix.label import Label
from kivy.logger import Logger
from openmrs import RESTConnection
__app_package__ = 'edu.unl.cse.soft161.rest'
__app__ = 'Rest'
__version__ = '0.1'
__flags__ = ['--bootstrap=sdl2', '--requirements=python2,kivy', '--permission=INTERNET', '--orientation=landscape']
class RestApp(App):
def __init__(self, **kwargs):
super(RestApp, self).__init__(**kwargs)
self.openmrs_connection = RESTConnection('localhost', 8080, 'admin', 'Admin123')
def load_locations(self):
results_layout = self.root.ids.results
for moribund in tuple(results_layout.children):
results_layout.remove_widget(moribund)
self.openmrs_connection.send_request('location', None, self.on_locations_loaded,
self.on_locations_not_loaded, self.on_locations_not_loaded)
def on_locations_loaded(self, request, response):
results_layout = self.root.ids.results
for result in response['results']:
results_layout.add_widget(Label(text=result['display']))
def on_locations_not_loaded(self, request, error):
results_layout = self.root.ids.results
results_layout.add_widget(Label(text='[Failed to load locations]'))
Logger.error('RestApp: {error}'.format(error=error))
if __name__ == "__main__":
app = RestApp()
app.run()
from kivy.network.urlrequest import UrlRequest
import base64
import json
try:
from urllib import quote
except ImportError:
from urllib.parse import quote
class RESTConnection(object):
@staticmethod
def _construct_url(authority, port, resource):
return 'http://{authority}:{port}/openmrs/ws/rest/v1/{resource}' \
.format(authority=authority, port=port, resource=resource)
def __init__(self, authority, port, username, password):
self.authority = authority
self.port = port
credentials = '{username}:{password}'.format(username=username, password=password)
credentials = base64.standard_b64encode(credentials)
self.headers = {
'Authorization': 'Basic {credentials}'.format(credentials=credentials),
'Content-type': 'application/json',
}
def send_request(self, resource, parameters, on_success, on_failure, on_error):
url = RESTConnection._construct_url(self.authority, self.port, resource)
parameters = json.dumps(parameters) if parameters is not None else None
UrlRequest(url, req_headers=self.headers, req_body=parameters,
on_success=on_success, on_failure=on_failure, on_error=on_error)
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
id: results
Button:
text: 'Load Locations'
on_press: app.load_locations()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment