Skip to content

Commit f4f844b

Browse files
committed
feat(client): fix client
1 parent b74f906 commit f4f844b

File tree

3 files changed

+76
-41
lines changed

3 files changed

+76
-41
lines changed

scaleway_qaas_client/client.py

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,75 @@
1414
import json
1515
import randomname
1616

17+
from pytimeparse.timeparse import timeparse
18+
1719
from typing import List, Optional, Dict, Union
1820

1921
from scaleway_qaas_client.quantum_as_a_service_api_client.models import (
2022
CreateJobBody,
21-
CancelJobBody,
2223
CreateJobBodyCircuit,
2324
CreateSessionBody,
2425
TerminateSessionBody,
26+
CancelJobBody,
2527
ScalewayQaasV1Alpha1Platform,
2628
ScalewayQaasV1Alpha1Job,
2729
ScalewayQaasV1Alpha1JobResult,
2830
ScalewayQaasV1Alpha1Session,
2931
)
3032

3133
from scaleway_qaas_client.quantum_as_a_service_api_client.api.sessions.create_session import (
32-
sync as _create_session_sync,
34+
sync_detailed as _create_session_sync,
3335
)
3436
from scaleway_qaas_client.quantum_as_a_service_api_client.api.sessions.get_session import (
35-
sync as _get_session_sync,
37+
sync_detailed as _get_session_sync,
3638
)
3739
from scaleway_qaas_client.quantum_as_a_service_api_client.api.sessions.list_sessions import (
38-
sync as _list_session_sync,
40+
sync_detailed as _list_session_sync,
3941
)
4042
from scaleway_qaas_client.quantum_as_a_service_api_client.api.sessions.terminate_session import (
41-
sync as _terminate_session_sync,
43+
sync_detailed as _terminate_session_sync,
4244
)
4345
from scaleway_qaas_client.quantum_as_a_service_api_client.api.sessions.delete_session import (
4446
sync_detailed as _delete_session_sync,
4547
)
4648
from scaleway_qaas_client.quantum_as_a_service_api_client.api.platforms.list_platforms import (
47-
sync as _list_platforms_sync,
49+
sync_detailed as _list_platforms_sync,
4850
)
4951
from scaleway_qaas_client.quantum_as_a_service_api_client.api.platforms.get_platform import (
50-
sync as _get_platform_sync,
52+
sync_detailed as _get_platform_sync,
5153
)
5254
from scaleway_qaas_client.quantum_as_a_service_api_client.api.jobs.create_job import (
53-
sync as _create_job_sync,
55+
sync_detailed as _create_job_sync,
5456
)
5557
from scaleway_qaas_client.quantum_as_a_service_api_client.api.jobs.get_job import (
56-
sync as _get_job_sync,
58+
sync_detailed as _get_job_sync,
5759
)
5860
from scaleway_qaas_client.quantum_as_a_service_api_client.api.jobs.cancel_job import (
59-
sync as _cancel_job_sync,
61+
sync_detailed as _cancel_job_sync,
6062
)
6163
from scaleway_qaas_client.quantum_as_a_service_api_client.api.jobs.list_job_results import (
62-
sync as _list_job_result_sync,
64+
sync_detailed as _list_job_result_sync,
6365
)
66+
from scaleway_qaas_client.quantum_as_a_service_api_client.types import Response
6467

6568
from scaleway_qaas_client.quantum_as_a_service_api_client.client import (
66-
Client,
6769
AuthenticatedClient,
6870
)
6971

7072

7173
_DEFAULT_URL = "https://api.scaleway.com"
7274

7375

76+
def _raise_on_error(response: Response):
77+
if not response:
78+
raise Exception("error: None response")
79+
80+
if response.status_code.is_server_error or response.status_code.is_client_error:
81+
raise Exception(
82+
f"error {response.status_code}: {response.content.decode("utf-8")}"
83+
)
84+
85+
7486
class QaaSClient:
7587
def __init__(self, project_id: str, secret_key: str, url: str = _DEFAULT_URL):
7688
self.__project_id = project_id
@@ -89,30 +101,38 @@ def __repr__(self) -> str:
89101
return f"<QaaSClient(url={self.__client._base_url},project_id={self.__project_id})>"
90102

91103
def get_platform(self, platform_id: str) -> ScalewayQaasV1Alpha1Platform:
92-
platform = _get_platform_sync(client=self.__client, platform_id=platform_id)
104+
response = _get_platform_sync(client=self.__client, platform_id=platform_id)
105+
106+
_raise_on_error(response)
93107

94-
return platform
108+
return response.parsed
95109

96110
def list_platforms(
97111
self, name: Optional[str] = None
98112
) -> List[ScalewayQaasV1Alpha1Platform]:
99113
response = _list_platforms_sync(client=self.__client, name=name)
100114

101-
assert response
115+
_raise_on_error(response)
102116

103-
return response.platforms
117+
return response.parsed.platforms
104118

105119
def create_session(
106120
self,
107121
platform_id: str,
108-
max_duration: str,
109-
max_idle_duration: str,
122+
max_duration: Union[str, int],
123+
max_idle_duration: Union[str, int],
110124
deduplication_id: Optional[str] = None,
111125
name: Optional[str] = None,
112126
) -> ScalewayQaasV1Alpha1Session:
113127
name = name if name else f"qs-{randomname.get_name()}"
114128

115-
session = _create_session_sync(
129+
if isinstance(max_duration, str):
130+
max_duration = f"{timeparse(max_duration)}s"
131+
132+
if isinstance(max_idle_duration, str):
133+
max_idle_duration = f"{timeparse(max_idle_duration)}s"
134+
135+
response = _create_session_sync(
116136
client=self.__client,
117137
body=CreateSessionBody(
118138
project_id=self.__project_id,
@@ -124,35 +144,38 @@ def create_session(
124144
),
125145
)
126146

127-
return session
147+
_raise_on_error(response)
148+
149+
return response.parsed
128150

129151
def get_session(self, session_id: str) -> ScalewayQaasV1Alpha1Session:
130-
session = _get_session_sync(client=self.__client, session_id=session_id)
152+
response = _get_session_sync(client=self.__client, session_id=session_id)
131153

132-
return session
154+
_raise_on_error(response)
155+
156+
return response.parsed
133157

134158
def list_session(
135159
self, platform_id: Optional[str] = None
136160
) -> List[ScalewayQaasV1Alpha1Session]:
137161
response = _list_session_sync(
138-
client=self.__client,
139-
project_id=self.__project_id,
140-
platform_id=platform_id
162+
client=self.__client, project_id=self.__project_id, platform_id=platform_id
141163
)
142164

143-
assert response
165+
_raise_on_error(response)
144166

145-
return response.sessions
167+
return response.parsed.sessions
146168

147169
def terminate_session(self, session_id: str) -> ScalewayQaasV1Alpha1Session:
148-
session = _terminate_session_sync(
170+
response = _terminate_session_sync(
149171
client=self.__client,
150-
body=TerminateSessionBody(
151-
session_id=session_id,
152-
),
172+
session_id=session_id,
173+
body=TerminateSessionBody(),
153174
)
154175

155-
return session
176+
_raise_on_error(response)
177+
178+
return response.parsed
156179

157180
def delete_session(self, session_id: str):
158181
_delete_session_sync(client=self.__client, session_id=session_id)
@@ -166,7 +189,7 @@ def create_job(
166189
payload = payload if isinstance(payload, str) else json.dumps(payload)
167190
name = name if name else f"qj-{randomname.get_name()}"
168191

169-
job = _create_job_sync(
192+
response = _create_job_sync(
170193
client=self.__client,
171194
body=CreateJobBody(
172195
name=name,
@@ -175,19 +198,31 @@ def create_job(
175198
),
176199
)
177200

178-
return job
201+
_raise_on_error(response)
202+
203+
return response.parsed
179204

180205
def get_job(self, job_id: str) -> ScalewayQaasV1Alpha1Job:
181-
job = _get_job_sync(client=self.__client, job_id=job_id)
206+
response = _get_job_sync(client=self.__client, job_id=job_id)
182207

183-
return job
208+
_raise_on_error(response)
209+
210+
return response.parsed
184211

185212
def list_job_results(self, job_id: str) -> List[ScalewayQaasV1Alpha1JobResult]:
186213
response = _list_job_result_sync(client=self.__client, job_id=job_id)
187214

188-
return response.job_results
215+
_raise_on_error(response)
216+
217+
return response.parsed.job_results
189218

190219
def cancel_job(self, job_id: str) -> ScalewayQaasV1Alpha1Job:
191-
job = _cancel_job_sync(client=self.__client, body=CancelJobBody(job_id=job_id))
220+
response = _cancel_job_sync(
221+
client=self.__client,
222+
job_id=job_id,
223+
body=CancelJobBody(),
224+
)
225+
226+
_raise_on_error(response)
192227

193-
return job
228+
return response.parsed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
setup(
2424
name="scaleway_qaas_client",
25-
version="0.1.4",
25+
version="0.1.5",
2626
project_urls={
2727
"Documentation": "https://www.scaleway.com/en/quantum-as-a-service/",
2828
"Source": "https://github.com/scaleway/scaleway-qaas-client-pythom",

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_create_delete_session():
5151
assert target_platform.id is not None
5252

5353
session = client.create_session(
54-
platform_id=target_platform.id, max_duration="2min", max_idle_duration="2min"
54+
platform_id=target_platform.id, max_duration="2m", max_idle_duration="2m"
5555
)
5656

5757
assert session is not None

0 commit comments

Comments
 (0)