1
- """ rest api handlers
1
+ """rest api handlers
2
2
3
3
- Take into account that part of the API is also needed in the public API so logic
4
4
should live in the catalog service in his final version
7
7
8
8
import asyncio
9
9
import logging
10
- import urllib .parse
11
10
from typing import Final
12
11
13
12
from aiohttp import web
26
25
ServiceResourcesDict ,
27
26
ServiceResourcesDictHelpers ,
28
27
)
29
- from pydantic import BaseModel , ConfigDict , Field , field_validator
28
+ from pydantic import BaseModel , Field
30
29
from servicelib .aiohttp .requests_validation import (
31
30
parse_request_body_as ,
32
31
parse_request_path_parameters_as ,
39
38
from ..resource_usage .service import get_default_service_pricing_plan
40
39
from ..security .decorators import permission_required
41
40
from ..utils_aiohttp import envelope_json_response
42
- from . import _api , _handlers_errors , client
43
- from ._api import CatalogRequestContext
44
- from .exceptions import DefaultPricingUnitForServiceNotFoundError
41
+ from . import _catalog_rest_client_service , _service
42
+ from ._exceptions import (
43
+ DefaultPricingUnitForServiceNotFoundError ,
44
+ handle_plugin_requests_exceptions ,
45
+ )
46
+ from .controller_rest_schemas import (
47
+ CatalogRequestContext ,
48
+ ListServiceParams ,
49
+ ServicePathParams ,
50
+ )
45
51
46
52
_logger = logging .getLogger (__name__ )
47
53
51
57
routes = RouteTableDef ()
52
58
53
59
54
- class ServicePathParams (BaseModel ):
55
- service_key : ServiceKey
56
- service_version : ServiceVersion
57
- model_config = ConfigDict (
58
- populate_by_name = True ,
59
- extra = "forbid" ,
60
- )
61
-
62
- @field_validator ("service_key" , mode = "before" )
63
- @classmethod
64
- def ensure_unquoted (cls , v ):
65
- # NOTE: this is needed as in pytest mode, the aiohttp server does not seem to unquote automatically
66
- if v is not None :
67
- return urllib .parse .unquote (v )
68
- return v
69
-
70
-
71
- class ListServiceParams (PageQueryParameters ):
72
- ...
73
-
74
-
75
60
@routes .get (
76
61
f"{ VTAG } /catalog/services/-/latest" ,
77
62
name = "list_services_latest" ,
78
63
)
79
64
@login_required
80
65
@permission_required ("services.catalog.*" )
81
- @_handlers_errors . reraise_catalog_exceptions_as_http_errors
66
+ @handle_plugin_requests_exceptions
82
67
async def list_services_latest (request : Request ):
83
68
request_ctx = CatalogRequestContext .create (request )
84
69
query_params : ListServiceParams = parse_request_query_parameters_as (
85
70
ListServiceParams , request
86
71
)
87
72
88
- page_items , page_meta = await _api .list_latest_services (
73
+ page_items , page_meta = await _service .list_latest_services (
89
74
request .app ,
90
75
user_id = request_ctx .user_id ,
91
76
product_name = request_ctx .product_name ,
@@ -116,15 +101,15 @@ async def list_services_latest(request: Request):
116
101
)
117
102
@login_required
118
103
@permission_required ("services.catalog.*" )
119
- @_handlers_errors . reraise_catalog_exceptions_as_http_errors
104
+ @handle_plugin_requests_exceptions
120
105
async def get_service (request : Request ):
121
106
request_ctx = CatalogRequestContext .create (request )
122
107
path_params = parse_request_path_parameters_as (ServicePathParams , request )
123
108
124
109
assert request_ctx # nosec
125
110
assert path_params # nosec
126
111
127
- service = await _api .get_service_v2 (
112
+ service = await _service .get_service_v2 (
128
113
request .app ,
129
114
user_id = request_ctx .user_id ,
130
115
product_name = request_ctx .product_name ,
@@ -142,7 +127,7 @@ async def get_service(request: Request):
142
127
)
143
128
@login_required
144
129
@permission_required ("services.catalog.*" )
145
- @_handlers_errors . reraise_catalog_exceptions_as_http_errors
130
+ @handle_plugin_requests_exceptions
146
131
async def update_service (request : Request ):
147
132
request_ctx = CatalogRequestContext .create (request )
148
133
path_params = parse_request_path_parameters_as (ServicePathParams , request )
@@ -154,7 +139,7 @@ async def update_service(request: Request):
154
139
assert path_params # nosec
155
140
assert update # nosec
156
141
157
- updated = await _api .update_service_v2 (
142
+ updated = await _service .update_service_v2 (
158
143
request .app ,
159
144
user_id = request_ctx .user_id ,
160
145
product_name = request_ctx .product_name ,
@@ -178,7 +163,7 @@ async def list_service_inputs(request: Request):
178
163
path_params = parse_request_path_parameters_as (ServicePathParams , request )
179
164
180
165
# Evaluate and return validated model
181
- response_model = await _api .list_service_inputs (
166
+ response_model = await _service .list_service_inputs (
182
167
path_params .service_key , path_params .service_version , ctx
183
168
)
184
169
@@ -203,7 +188,7 @@ async def get_service_input(request: Request):
203
188
path_params = parse_request_path_parameters_as (_ServiceInputsPathParams , request )
204
189
205
190
# Evaluate and return validated model
206
- response_model = await _api .get_service_input (
191
+ response_model = await _service .get_service_input (
207
192
path_params .service_key ,
208
193
path_params .service_version ,
209
194
path_params .input_key ,
@@ -236,7 +221,7 @@ async def get_compatible_inputs_given_source_output(request: Request):
236
221
)
237
222
238
223
# Evaluate and return validated model
239
- data = await _api .get_compatible_inputs_given_source_output (
224
+ data = await _service .get_compatible_inputs_given_source_output (
240
225
path_params .service_key ,
241
226
path_params .service_version ,
242
227
query_params .from_service_key ,
@@ -261,7 +246,7 @@ async def list_service_outputs(request: Request):
261
246
path_params = parse_request_path_parameters_as (ServicePathParams , request )
262
247
263
248
# Evaluate and return validated model
264
- response_model = await _api .list_service_outputs (
249
+ response_model = await _service .list_service_outputs (
265
250
path_params .service_key , path_params .service_version , ctx
266
251
)
267
252
@@ -286,7 +271,7 @@ async def get_service_output(request: Request):
286
271
path_params = parse_request_path_parameters_as (_ServiceOutputsPathParams , request )
287
272
288
273
# Evaluate and return validated model
289
- response_model = await _api .get_service_output (
274
+ response_model = await _service .get_service_output (
290
275
path_params .service_key ,
291
276
path_params .service_version ,
292
277
path_params .output_key ,
@@ -323,7 +308,7 @@ async def get_compatible_outputs_given_target_input(request: Request):
323
308
_ToServiceInputsParams , request
324
309
)
325
310
326
- data = await _api .get_compatible_outputs_given_target_input (
311
+ data = await _service .get_compatible_outputs_given_target_input (
327
312
path_params .service_key ,
328
313
path_params .service_version ,
329
314
query_params .to_service_key ,
@@ -351,11 +336,13 @@ async def get_service_resources(request: Request):
351
336
"""
352
337
ctx = CatalogRequestContext .create (request )
353
338
path_params = parse_request_path_parameters_as (ServicePathParams , request )
354
- service_resources : ServiceResourcesDict = await client .get_service_resources (
355
- request .app ,
356
- user_id = ctx .user_id ,
357
- service_key = path_params .service_key ,
358
- service_version = path_params .service_version ,
339
+ service_resources : ServiceResourcesDict = (
340
+ await _catalog_rest_client_service .get_service_resources (
341
+ request .app ,
342
+ user_id = ctx .user_id ,
343
+ service_key = path_params .service_key ,
344
+ service_version = path_params .service_version ,
345
+ )
359
346
)
360
347
361
348
data = ServiceResourcesDictHelpers .create_jsonable (service_resources )
@@ -370,7 +357,7 @@ async def get_service_resources(request: Request):
370
357
)
371
358
@login_required
372
359
@permission_required ("services.catalog.*" )
373
- @_handlers_errors . reraise_catalog_exceptions_as_http_errors
360
+ @handle_plugin_requests_exceptions
374
361
async def get_service_pricing_plan (request : Request ):
375
362
ctx = CatalogRequestContext .create (request )
376
363
path_params = parse_request_path_parameters_as (ServicePathParams , request )
0 commit comments