14
14
import json
15
15
import randomname
16
16
17
+ from pytimeparse .timeparse import timeparse
18
+
17
19
from typing import List , Optional , Dict , Union
18
20
19
21
from scaleway_qaas_client .quantum_as_a_service_api_client .models import (
20
22
CreateJobBody ,
21
- CancelJobBody ,
22
23
CreateJobBodyCircuit ,
23
24
CreateSessionBody ,
24
25
TerminateSessionBody ,
26
+ CancelJobBody ,
25
27
ScalewayQaasV1Alpha1Platform ,
26
28
ScalewayQaasV1Alpha1Job ,
27
29
ScalewayQaasV1Alpha1JobResult ,
28
30
ScalewayQaasV1Alpha1Session ,
29
31
)
30
32
31
33
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 ,
33
35
)
34
36
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 ,
36
38
)
37
39
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 ,
39
41
)
40
42
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 ,
42
44
)
43
45
from scaleway_qaas_client .quantum_as_a_service_api_client .api .sessions .delete_session import (
44
46
sync_detailed as _delete_session_sync ,
45
47
)
46
48
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 ,
48
50
)
49
51
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 ,
51
53
)
52
54
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 ,
54
56
)
55
57
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 ,
57
59
)
58
60
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 ,
60
62
)
61
63
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 ,
63
65
)
66
+ from scaleway_qaas_client .quantum_as_a_service_api_client .types import Response
64
67
65
68
from scaleway_qaas_client .quantum_as_a_service_api_client .client import (
66
- Client ,
67
69
AuthenticatedClient ,
68
70
)
69
71
70
72
71
73
_DEFAULT_URL = "https://api.scaleway.com"
72
74
73
75
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
+
74
86
class QaaSClient :
75
87
def __init__ (self , project_id : str , secret_key : str , url : str = _DEFAULT_URL ):
76
88
self .__project_id = project_id
@@ -89,30 +101,38 @@ def __repr__(self) -> str:
89
101
return f"<QaaSClient(url={ self .__client ._base_url } ,project_id={ self .__project_id } )>"
90
102
91
103
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 )
93
107
94
- return platform
108
+ return response . parsed
95
109
96
110
def list_platforms (
97
111
self , name : Optional [str ] = None
98
112
) -> List [ScalewayQaasV1Alpha1Platform ]:
99
113
response = _list_platforms_sync (client = self .__client , name = name )
100
114
101
- assert response
115
+ _raise_on_error ( response )
102
116
103
- return response .platforms
117
+ return response .parsed . platforms
104
118
105
119
def create_session (
106
120
self ,
107
121
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 ] ,
110
124
deduplication_id : Optional [str ] = None ,
111
125
name : Optional [str ] = None ,
112
126
) -> ScalewayQaasV1Alpha1Session :
113
127
name = name if name else f"qs-{ randomname .get_name ()} "
114
128
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 (
116
136
client = self .__client ,
117
137
body = CreateSessionBody (
118
138
project_id = self .__project_id ,
@@ -124,35 +144,38 @@ def create_session(
124
144
),
125
145
)
126
146
127
- return session
147
+ _raise_on_error (response )
148
+
149
+ return response .parsed
128
150
129
151
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 )
131
153
132
- return session
154
+ _raise_on_error (response )
155
+
156
+ return response .parsed
133
157
134
158
def list_session (
135
159
self , platform_id : Optional [str ] = None
136
160
) -> List [ScalewayQaasV1Alpha1Session ]:
137
161
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
141
163
)
142
164
143
- assert response
165
+ _raise_on_error ( response )
144
166
145
- return response .sessions
167
+ return response .parsed . sessions
146
168
147
169
def terminate_session (self , session_id : str ) -> ScalewayQaasV1Alpha1Session :
148
- session = _terminate_session_sync (
170
+ response = _terminate_session_sync (
149
171
client = self .__client ,
150
- body = TerminateSessionBody (
151
- session_id = session_id ,
152
- ),
172
+ session_id = session_id ,
173
+ body = TerminateSessionBody (),
153
174
)
154
175
155
- return session
176
+ _raise_on_error (response )
177
+
178
+ return response .parsed
156
179
157
180
def delete_session (self , session_id : str ):
158
181
_delete_session_sync (client = self .__client , session_id = session_id )
@@ -166,7 +189,7 @@ def create_job(
166
189
payload = payload if isinstance (payload , str ) else json .dumps (payload )
167
190
name = name if name else f"qj-{ randomname .get_name ()} "
168
191
169
- job = _create_job_sync (
192
+ response = _create_job_sync (
170
193
client = self .__client ,
171
194
body = CreateJobBody (
172
195
name = name ,
@@ -175,19 +198,31 @@ def create_job(
175
198
),
176
199
)
177
200
178
- return job
201
+ _raise_on_error (response )
202
+
203
+ return response .parsed
179
204
180
205
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 )
182
207
183
- return job
208
+ _raise_on_error (response )
209
+
210
+ return response .parsed
184
211
185
212
def list_job_results (self , job_id : str ) -> List [ScalewayQaasV1Alpha1JobResult ]:
186
213
response = _list_job_result_sync (client = self .__client , job_id = job_id )
187
214
188
- return response .job_results
215
+ _raise_on_error (response )
216
+
217
+ return response .parsed .job_results
189
218
190
219
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 )
192
227
193
- return job
228
+ return response . parsed
0 commit comments