fixing error messages and naming functions better

This commit is contained in:
2021-02-15 23:51:59 -06:00
parent 0de5305eac
commit 9389c80cb6
4 changed files with 18 additions and 18 deletions

View File

@ -27,7 +27,7 @@ class MyHTTPClient:
self.client_session = None
def make_requests_sync(self, online_hosts: List[OnlineHost], url_suffix: str, body: str, authorization_header=None) -> List[HTTPResult]:
def do_multi_http_sync(self, online_hosts: List[OnlineHost], url_suffix: str, body: str, authorization_header=None) -> List[HTTPResult]:
future = run_coroutine(self.make_requests(online_hosts=online_hosts, url_suffix=url_suffix, body=body, authorization_header=authorization_header))
fromOtherThread = future.result()
toReturn = []
@ -38,8 +38,8 @@ class MyHTTPClient:
return toReturn
def post_json_sync(self, url: str, body: str, method="POST", authorization_header=None) -> HTTPResult:
future = run_coroutine(self.post_json(method=method, url=url, body=body, authorization_header=authorization_header))
def do_http_sync(self, url: str, body: str, method="POST", authorization_header=None) -> HTTPResult:
future = run_coroutine(self.do_http(method=method, url=url, body=body, authorization_header=authorization_header))
fromOtherThread = future.result()
if fromOtherThread.error != None and fromOtherThread.error != "":
current_app.logger.error(fromOtherThread.error)
@ -51,7 +51,7 @@ class MyHTTPClient:
return self.client_session
async def post_json(self, url: str, body: str, method="POST", authorization_header=None) -> InterThreadResult:
async def do_http(self, url: str, body: str, method="POST", authorization_header=None) -> InterThreadResult:
# TODO make a configuration option where this throws an error if the url does not start with https://
response = None
try:
@ -69,11 +69,11 @@ class MyHTTPClient:
)
except:
error_message = my_exec_info_message(sys.exc_info())
response_body = json.dumps({"error_message": f"error contacting spoke: {error_message}"})
response_body = json.dumps({"error_message": f"do_http (HTTP {method} {url}) {error_message}"})
return InterThreadResult(
HTTPResult(-1, response_body),
f"""error contacting spoke: post_json (HTTP {method} {url}) failed with: {error_message}"""
f"""do_http (HTTP {method} {url}) failed with: {error_message}"""
)
response_body = None
@ -81,20 +81,20 @@ class MyHTTPClient:
response_body = await response.text()
except:
error_message = my_exec_info_message(sys.exc_info())
response_body = json.dumps({"error_message": f"error reading response from spoke: {error_message}"})
response_body = json.dumps({"error_message": f"error reading response: HTTP {method} {url} (status {response.status}) failed with: {error_message}"})
return InterThreadResult(
HTTPResult(response.status, response_body),
f"""error reading response from spoke: HTTP {method} {url} (status {response.status}) failed with: {error_message}"""
f"""error reading response: HTTP {method} {url} (status {response.status}) failed with: {error_message}"""
)
return InterThreadResult(HTTPResult(response.status, response_body), None)
async def make_requests(self, online_hosts: List[OnlineHost], url_suffix: str, body: str, authorization_header=None) -> List[InterThreadResult]:
async def do_multi_http(self, online_hosts: List[OnlineHost], url_suffix: str, body: str, authorization_header=None) -> List[InterThreadResult]:
tasks = []
# append to tasks in the same order as online_hosts
for host in online_hosts:
tasks.append(
self.post_json(url=f"{host.url}{url_suffix}", body=body, authorization_header=authorization_header)
self.do_http(url=f"{host.url}{url_suffix}", body=body, authorization_header=authorization_header)
)
# gather is like Promise.all from javascript, it returns a future which resolves to an array of results
# in the same order as the tasks that we passed in -- which were in the same order as online_hosts