diff --git a/src/zepben/eas/client/eas_client.py b/src/zepben/eas/client/eas_client.py index 8293bf9..d900a04 100644 --- a/src/zepben/eas/client/eas_client.py +++ b/src/zepben/eas/client/eas_client.py @@ -727,18 +727,22 @@ async def async_upload_study(self, study: Study): response = await response.text() return response - def run_hosting_capacity_calibration(self, calibration_name: str): + def run_hosting_capacity_calibration(self, calibration_name: str, local_calibration_time: Optional[str] = None): """ Send request to run hosting capacity calibration :param calibration_name: A string representation of the calibration name + :param local_calibration_time: A string representation of the calibration time, in model time. :return: The HTTP response received from the Evolve App Server after attempting to run the calibration """ - return get_event_loop().run_until_complete(self.async_run_hosting_capacity_calibration(calibration_name)) + return get_event_loop().run_until_complete( + self.async_run_hosting_capacity_calibration(calibration_name, local_calibration_time)) - async def async_run_hosting_capacity_calibration(self, calibration_name: str): + async def async_run_hosting_capacity_calibration(self, calibration_name: str, + calibration_time_local: Optional[str] = None): """ Send asynchronous request to run hosting capacity calibration :param calibration_name: A string representation of the calibration name + :param calibration_time_local: A string representation of the calibration time, in model time. :return: The HTTP response received from the Evolve App Server after attempting to run the calibration """ with warnings.catch_warnings(): @@ -746,14 +750,16 @@ async def async_run_hosting_capacity_calibration(self, calibration_name: str): warnings.filterwarnings("ignore", category=InsecureRequestWarning) json = { "query": """ - mutation runCalibration($calibrationName: String!) { - runCalibration(calibrationName: $calibrationName) + mutation runCalibration($calibrationName: String!, $calibrationTimeLocal: LocalDateTime) { + runCalibration(calibrationName: $calibrationName, calibrationTimeLocal: $calibrationTimeLocal) } """, "variables": { - "calibrationName": calibration_name + "calibrationName": calibration_name, + "calibrationTimeLocal": calibration_time_local } } + if self._verify_certificate: sslcontext = ssl.create_default_context(cafile=self._ca_filename) @@ -772,7 +778,7 @@ async def async_run_hosting_capacity_calibration(self, calibration_name: str): def get_hosting_capacity_calibration_run(self, id: str): """ Retrieve information of a hosting capacity calibration run - :param id: The calibration run ID + :param id: The calibration ID :return: The HTTP response received from the Evolve App Server after requesting calibration run info """ return get_event_loop().run_until_complete(self.async_get_hosting_capacity_calibration_run(id)) @@ -780,7 +786,7 @@ def get_hosting_capacity_calibration_run(self, id: str): async def async_get_hosting_capacity_calibration_run(self, id: str): """ Retrieve information of a hosting capacity calibration run - :param id: The calibration run ID + :param id: The calibration ID :return: The HTTP response received from the Evolve App Server after requesting calibration run info """ with warnings.catch_warnings(): @@ -789,11 +795,12 @@ async def async_get_hosting_capacity_calibration_run(self, id: str): json = { "query": """ query getCalibrationRun($id: ID!) { - getCalibrationRun(calibrationRunId: $id) { + getCalibrationRun(id: $id) { id name workflowId runId + calibrationTimeLocal startAt completedAt status @@ -818,3 +825,40 @@ async def async_get_hosting_capacity_calibration_run(self, id: str): else: response = await response.text() return response + + def get_hosting_capacity_calibration_sets(self): + """ + Retrieve a list of all completed calibration runs initiated through Evolve App Server + :return: The HTTP response received from the Evolve App Server after requesting completed calibration runs + """ + return get_event_loop().run_until_complete(self.async_get_hosting_capacity_calibration_sets()) + + async def async_get_hosting_capacity_calibration_sets(self): + """ + Retrieve a list of all completed calibration runs initiated through Evolve App Server + :return: The HTTP response received from the Evolve App Server after requesting completed calibration runs + """ + with warnings.catch_warnings(): + if not self._verify_certificate: + warnings.filterwarnings("ignore", category=InsecureRequestWarning) + json = { + "query": """ + query { + getCalibrationSets + } + """ + } + if self._verify_certificate: + sslcontext = ssl.create_default_context(cafile=self._ca_filename) + + async with self.session.post( + construct_url(protocol=self._protocol, host=self._host, port=self._port, path="/api/graphql"), + headers=self._get_request_headers(), + json=json, + ssl=sslcontext if self._verify_certificate else False + ) as response: + if response.ok: + response = await response.json() + else: + response = await response.text() + return response diff --git a/test/test_eas_client.py b/test/test_eas_client.py index 10a405a..7796bc7 100644 --- a/test/test_eas_client.py +++ b/test/test_eas_client.py @@ -174,7 +174,8 @@ def test_get_work_package_cost_estimation_no_verify_success(httpserver: HTTPServ verify_certificate=False ) - httpserver.expect_oneshot_request("/api/graphql").respond_with_json({"data": {"getWorkPackageCostEstimation": "123.45"}}) + httpserver.expect_oneshot_request("/api/graphql").respond_with_json( + {"data": {"getWorkPackageCostEstimation": "123.45"}}) res = eas_client.get_work_package_cost_estimation( WorkPackageConfig( "wp_name", @@ -569,15 +570,17 @@ def test_raises_error_if_access_token_and_client_secret_configured(httpserver: H assert "Incompatible arguments passed to connect to secured Evolve App Server. You cannot provide multiple types of authentication. When using an access_token, do not provide client_id, client_secret, username, password, or token_fetcher." in str( error_message_for_username.value) + def hosting_capacity_run_calibration_request_handler(request): actual_body = json.loads(request.data.decode()) query = " ".join(actual_body['query'].split()) - assert query == "mutation runCalibration($calibrationName: String!) { runCalibration(calibrationName: $calibrationName) }" - assert actual_body['variables'] == {"calibrationName": "TEST CALIBRATION"} + assert query == "mutation runCalibration($calibrationName: String!, $calibrationTimeLocal: LocalDateTime) { runCalibration(calibrationName: $calibrationName, calibrationTimeLocal: $calibrationTimeLocal) }" + assert actual_body['variables'] == {"calibrationName": "TEST CALIBRATION", "calibrationTimeLocal": None} return Response(json.dumps({"result": "success"}), status=200, content_type="application/json") + def test_run_hosting_capacity_calibration_no_verify_success(httpserver: HTTPServer): eas_client = EasClient( LOCALHOST, @@ -585,7 +588,8 @@ def test_run_hosting_capacity_calibration_no_verify_success(httpserver: HTTPServ verify_certificate=False ) - httpserver.expect_oneshot_request("/api/graphql").respond_with_handler(hosting_capacity_run_calibration_request_handler) + httpserver.expect_oneshot_request("/api/graphql").respond_with_handler( + hosting_capacity_run_calibration_request_handler) res = eas_client.run_hosting_capacity_calibration("TEST CALIBRATION") httpserver.check_assertions() assert res == {"result": "success"} @@ -614,20 +618,23 @@ def test_run_hosting_capacity_calibration_valid_certificate_success(ca: trustme. ca_filename=ca_filename ) - httpserver.expect_oneshot_request("/api/graphql").respond_with_handler(hosting_capacity_run_calibration_request_handler) + httpserver.expect_oneshot_request("/api/graphql").respond_with_handler( + hosting_capacity_run_calibration_request_handler) res = eas_client.run_hosting_capacity_calibration("TEST CALIBRATION") httpserver.check_assertions() assert res == {"result": "success"} + def get_hosting_capacity_run_calibration_request_handler(request): actual_body = json.loads(request.data.decode()) query = " ".join(actual_body['query'].split()) - assert query == "query getCalibrationRun($id: ID!) { getCalibrationRun(calibrationRunId: $id) { id name workflowId runId startAt completedAt status } }" + assert query == "query getCalibrationRun($id: ID!) { getCalibrationRun(id: $id) { id name workflowId runId calibrationTimeLocal startAt completedAt status } }" assert actual_body['variables'] == {"id": "calibration-id"} return Response(json.dumps({"result": "success"}), status=200, content_type="application/json") + def test_get_hosting_capacity_calibration_run_no_verify_success(httpserver: HTTPServer): eas_client = EasClient( LOCALHOST, @@ -635,7 +642,8 @@ def test_get_hosting_capacity_calibration_run_no_verify_success(httpserver: HTTP verify_certificate=False ) - httpserver.expect_oneshot_request("/api/graphql").respond_with_handler(get_hosting_capacity_run_calibration_request_handler) + httpserver.expect_oneshot_request("/api/graphql").respond_with_handler( + get_hosting_capacity_run_calibration_request_handler) res = eas_client.get_hosting_capacity_calibration_run("calibration-id") httpserver.check_assertions() assert res == {"result": "success"} @@ -664,7 +672,57 @@ def test_get_hosting_capacity_calibration_run_valid_certificate_success(ca: trus ca_filename=ca_filename ) - httpserver.expect_oneshot_request("/api/graphql").respond_with_handler(get_hosting_capacity_run_calibration_request_handler) + httpserver.expect_oneshot_request("/api/graphql").respond_with_handler( + get_hosting_capacity_run_calibration_request_handler) res = eas_client.get_hosting_capacity_calibration_run("calibration-id") httpserver.check_assertions() - assert res == {"result": "success"} \ No newline at end of file + assert res == {"result": "success"} + + +def hosting_capacity_run_calibration_with_calibration_time_request_handler(request): + actual_body = json.loads(request.data.decode()) + query = " ".join(actual_body['query'].split()) + + assert query == "mutation runCalibration($calibrationName: String!, $calibrationTimeLocal: LocalDateTime) { runCalibration(calibrationName: $calibrationName, calibrationTimeLocal: $calibrationTimeLocal) }" + assert actual_body['variables'] == {"calibrationName": "TEST CALIBRATION", "calibrationTimeLocal": "1992-01-28T00:00:20"} + + return Response(json.dumps({"result": "success"}), status=200, content_type="application/json") + + +def test_run_hosting_capacity_calibration_with_calibration_time_no_verify_success(httpserver: HTTPServer): + eas_client = EasClient( + LOCALHOST, + httpserver.port, + verify_certificate=False + ) + + httpserver.expect_oneshot_request("/api/graphql").respond_with_handler( + hosting_capacity_run_calibration_with_calibration_time_request_handler) + res = eas_client.run_hosting_capacity_calibration("TEST CALIBRATION", "1992-01-28T00:00:20") + httpserver.check_assertions() + assert res == {"result": "success"} + + +def get_hosting_capacity_calibration_sets_request_handler(request): + actual_body = json.loads(request.data.decode()) + query = " ".join(actual_body['query'].split()) + + assert query == "query { getCalibrationSets }" + + assert "variables" not in actual_body + + return Response(json.dumps(["one", "two", "three"]), status=200, content_type="application/json") + + +def test_get_hosting_capacity_calibration_sets_no_verify_success(httpserver: HTTPServer): + eas_client = EasClient( + LOCALHOST, + httpserver.port, + verify_certificate=False + ) + + httpserver.expect_oneshot_request("/api/graphql").respond_with_handler( + get_hosting_capacity_calibration_sets_request_handler) + res = eas_client.get_hosting_capacity_calibration_sets() + httpserver.check_assertions() + assert res == ["one", "two", "three"] \ No newline at end of file