# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Callable, Dict, List, Optional
from .... import oscar as mo
from ....utils import serialize_serializable, deserialize_serializable
from ...web import web_api, MarsServiceWebAPIHandler, MarsWebAPIClientMixin
from .core import AbstractMetaAPI
class MetaWebAPIHandler(MarsServiceWebAPIHandler):
_root_pattern = "/api/session/(?P<session_id>[^/]+)/meta"
async def _get_oscar_meta_api(self, session_id: str):
from .oscar import MetaAPI
return await self._get_api_by_key(MetaAPI, session_id)
@web_api("(?P<data_key>[^/]+)", method="get")
async def get_chunk_meta(self, session_id: str, data_key: str):
fields_str = self.get_argument("fields", None)
error = self.get_argument("error", "raise")
fields = fields_str.split(",") if fields_str else None
oscar_api = await self._get_oscar_meta_api(session_id)
result = await oscar_api.get_chunk_meta(data_key, fields=fields, error=error)
self.write(serialize_serializable(result))
@web_api("", method="post")
async def get_chunks_meta(self, session_id: str):
body_args = deserialize_serializable(self.request.body)
oscar_api = await self._get_oscar_meta_api(session_id)
get_metas = []
for data_key, fields, error in body_args:
get_metas.append(oscar_api.get_chunk_meta.delay(data_key, fields, error))
results = await oscar_api.get_chunk_meta.batch(*get_metas)
self.write(serialize_serializable(results))
web_handlers = {MetaWebAPIHandler.get_root_pattern(): MetaWebAPIHandler}