Commit 6c149cd7 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Backported updated RESTConnection.

parent fa7c4c06
......@@ -13,7 +13,7 @@ class RestApp(App):
def load_locations(self):
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)
def on_locations_loaded(self, _, response):
......
......@@ -8,11 +8,7 @@ except ImportError:
from urllib.parse import quote
class RESTConnection(object):
@staticmethod
def _construct_url(authority, port, resource):
return f'http://{authority}:{port}/openmrs/ws/rest/v1/{resource}'
class RESTConnection:
def __init__(self, authority, port, username, password):
self.authority = authority
self.port = port
......@@ -23,8 +19,16 @@ class RESTConnection(object):
'Content-type': 'application/json',
}
def send_request(self, resource, post_parameters, on_success, on_failure, on_error):
url = RESTConnection._construct_url(self.authority, self.port, resource)
post_parameters = json.dumps(post_parameters) if post_parameters is not None else None
UrlRequest(url, req_headers=self.headers, req_body=post_parameters,
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):
url = self.construct_url(resource, get_parameters)
self.send_request_by_url(url, post_parameters, on_success, on_failure, on_error)
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