Skip to content
Snippets Groups Projects
Commit 02f15b67 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Backported updated RESTConnection.

parent 87e6abed
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ class RestApp(App): ...@@ -15,7 +15,7 @@ class RestApp(App):
def load_locations(self): def load_locations(self):
self.root.ids.results.clear_widgets() self.root.ids.results.clear_widgets()
self.openmrs_connection.send_request('location', {}, None, self.on_locations_loaded, self.openmrs_connection.send_request('location', None, None, self.on_locations_loaded,
self.on_locations_not_loaded, self.on_locations_not_loaded) self.on_locations_not_loaded, self.on_locations_not_loaded)
def on_locations_loaded(self, _, response): def on_locations_loaded(self, _, response):
......
...@@ -8,26 +8,27 @@ except ImportError: ...@@ -8,26 +8,27 @@ except ImportError:
from urllib.parse import quote from urllib.parse import quote
class RESTConnection(object): class RESTConnection:
@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): def __init__(self, authority, port, username, password):
self.authority = authority self.authority = authority
self.port = port self.port = port
credentials = '{username}:{password}'.format(username=username, password=password) credentials = f'{username}:{password}'
credentials = base64.standard_b64encode(credentials.encode('UTF8')).decode('UTF8') credentials = base64.standard_b64encode(credentials.encode('UTF8')).decode('UTF8')
self.headers = { self.headers = {
'Authorization': 'Basic {credentials}'.format(credentials=credentials), 'Authorization': f'Basic {credentials}',
'Content-type': 'application/json', 'Content-type': 'application/json',
} }
def construct_url(self, resource, get_parameters=None):
get_parameters = '&'.join(f'{quote(str(key))}={quote(str(value))}' for key, value in get_parameters.items()) \
if get_parameters is not None else ''
return f'http://{self.authority}:{self.port}/openmrs/ws/rest/v1/{resource}?{get_parameters}'
def send_request_by_url(self, url, post_parameters, on_success, on_failure, on_error):
UrlRequest(url, req_headers=self.headers,
req_body=json.dumps(post_parameters) if post_parameters is not None else None,
on_success=on_success, on_failure=on_failure, on_error=on_error)
def send_request(self, resource, get_parameters, post_parameters, on_success, on_failure, on_error): def send_request(self, resource, get_parameters, post_parameters, on_success, on_failure, on_error):
url = RESTConnection._construct_url(self.authority, self.port, resource) url = self.construct_url(resource, get_parameters)
get_parameters = '&'.join('{key}={value}'.format(key=quote(key), value=quote(value)) self.send_request_by_url(url, post_parameters, on_success, on_failure, on_error)
for key, value in get_parameters.items()) if get_parameters is not None else ''
post_parameters = json.dumps(post_parameters) if post_parameters is not None else None
UrlRequest('{url}?{get_parameters}'.format(url=url, get_parameters=get_parameters), req_headers=self.headers,
req_body=post_parameters, on_success=on_success, on_failure=on_failure, on_error=on_error)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment