Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions src/zepben/eas/client/eas_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,33 +727,39 @@ 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():
if not self._verify_certificate:
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)

Expand All @@ -772,15 +778,15 @@ 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))

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():
Expand All @@ -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
Expand All @@ -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
76 changes: 67 additions & 9 deletions test/test_eas_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -569,23 +570,26 @@ 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,
httpserver.port,
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"}
Expand Down Expand Up @@ -614,28 +618,32 @@ 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,
httpserver.port,
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"}
Expand Down Expand Up @@ -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"}
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"]