2021-04-09 10:17:38 +02:00
|
|
|
from typing import (
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
|
|
|
Union,
|
|
|
|
)
|
|
|
|
|
2021-04-15 16:14:54 +02:00
|
|
|
from aiogrpcclient import BaseGrpcClient
|
2021-04-09 10:17:38 +02:00
|
|
|
from grpc import StatusCode
|
2021-04-15 16:14:54 +02:00
|
|
|
from grpc.experimental.aio import AioRpcError
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.meta_api.proto.documents_service_pb2 import \
|
|
|
|
RollRequest as RollRequestPb
|
|
|
|
from nexus.meta_api.proto.documents_service_pb2 import \
|
|
|
|
RollResponse as RollResponsePb
|
2021-04-13 16:23:31 +02:00
|
|
|
from nexus.meta_api.proto.documents_service_pb2 import \
|
|
|
|
TopMissedRequest as TopMissedRequestPb
|
|
|
|
from nexus.meta_api.proto.documents_service_pb2 import \
|
|
|
|
TopMissedResponse as TopMissedResponsePb
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.meta_api.proto.documents_service_pb2 import \
|
|
|
|
TypedDocumentRequest as TypedDocumentRequestPb
|
|
|
|
from nexus.meta_api.proto.documents_service_pb2_grpc import DocumentsStub
|
2021-04-15 16:14:54 +02:00
|
|
|
from nexus.meta_api.proto.search_service_pb2 import \
|
2021-04-09 10:17:38 +02:00
|
|
|
SearchRequest as SearchRequestPb
|
2021-04-15 16:14:54 +02:00
|
|
|
from nexus.meta_api.proto.search_service_pb2 import \
|
2021-04-09 10:17:38 +02:00
|
|
|
SearchResponse as SearchResponsePb
|
2021-04-15 16:14:54 +02:00
|
|
|
from nexus.meta_api.proto.search_service_pb2_grpc import SearchStub
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.models.proto.typed_document_pb2 import \
|
|
|
|
TypedDocument as TypedDocumentPb
|
|
|
|
from tenacity import (
|
|
|
|
retry,
|
|
|
|
retry_if_exception,
|
|
|
|
stop_after_attempt,
|
|
|
|
wait_fixed,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-04-15 16:14:54 +02:00
|
|
|
class MetaApiGrpcClient(BaseGrpcClient):
|
|
|
|
stub_clses = {
|
|
|
|
'documents': DocumentsStub,
|
|
|
|
'search': SearchStub,
|
|
|
|
}
|
2021-04-09 10:17:38 +02:00
|
|
|
|
|
|
|
async def get(
|
|
|
|
self,
|
|
|
|
schema: str,
|
|
|
|
document_id: int,
|
2021-05-01 11:10:45 +02:00
|
|
|
user_id: str,
|
2021-04-09 10:17:38 +02:00
|
|
|
position: Optional[int] = None,
|
|
|
|
request_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> TypedDocumentPb:
|
2021-04-15 16:14:54 +02:00
|
|
|
return await self.stubs['documents'].get(
|
2021-04-09 10:17:38 +02:00
|
|
|
TypedDocumentRequestPb(
|
|
|
|
schema=schema,
|
|
|
|
document_id=document_id,
|
|
|
|
position=position,
|
|
|
|
),
|
|
|
|
metadata=(
|
|
|
|
('request-id', request_id),
|
2021-05-01 11:10:45 +02:00
|
|
|
('session-id', session_id),
|
|
|
|
('user-id', user_id),
|
2021-04-09 10:17:38 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
async def roll(
|
|
|
|
self,
|
2021-05-01 11:10:45 +02:00
|
|
|
user_id: str,
|
2021-04-09 10:17:38 +02:00
|
|
|
language: Optional[str] = None,
|
|
|
|
request_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> RollResponsePb:
|
2021-04-15 16:14:54 +02:00
|
|
|
return await self.stubs['documents'].roll(
|
2021-04-09 10:17:38 +02:00
|
|
|
RollRequestPb(
|
|
|
|
language=language,
|
|
|
|
),
|
|
|
|
metadata=(
|
|
|
|
('request-id', request_id),
|
2021-05-01 11:10:45 +02:00
|
|
|
('session-id', session_id),
|
|
|
|
('user-id', user_id),
|
2021-04-09 10:17:38 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
@retry(
|
|
|
|
retry=retry_if_exception(
|
|
|
|
lambda e: isinstance(e, AioRpcError) and (
|
|
|
|
e.code() == StatusCode.CANCELLED
|
|
|
|
or e.code() == StatusCode.UNAVAILABLE
|
|
|
|
)
|
|
|
|
),
|
|
|
|
reraise=True,
|
|
|
|
stop=stop_after_attempt(5),
|
|
|
|
wait=wait_fixed(2),
|
|
|
|
)
|
|
|
|
async def search(
|
|
|
|
self,
|
|
|
|
schemas: Union[List[str], Tuple[str]],
|
|
|
|
query: str,
|
2021-05-01 11:10:45 +02:00
|
|
|
user_id: str,
|
2021-04-09 10:17:38 +02:00
|
|
|
page: Optional[int] = None,
|
|
|
|
page_size: Optional[int] = None,
|
|
|
|
language: Optional[str] = None,
|
|
|
|
request_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> SearchResponsePb:
|
2021-04-15 16:14:54 +02:00
|
|
|
return await self.stubs['search'].search(
|
2021-04-09 10:17:38 +02:00
|
|
|
SearchRequestPb(
|
|
|
|
schemas=schemas,
|
|
|
|
query=query,
|
|
|
|
page=page,
|
|
|
|
page_size=page_size,
|
|
|
|
language=language,
|
|
|
|
),
|
|
|
|
metadata=(
|
|
|
|
('request-id', request_id),
|
2021-05-01 11:10:45 +02:00
|
|
|
('session-id', session_id),
|
|
|
|
('user-id', user_id),
|
2021-04-09 10:17:38 +02:00
|
|
|
),
|
|
|
|
)
|
2021-04-13 16:23:31 +02:00
|
|
|
|
|
|
|
async def top_missed(
|
|
|
|
self,
|
|
|
|
page: int,
|
|
|
|
page_size: int,
|
2021-05-01 11:10:45 +02:00
|
|
|
user_id: str,
|
2021-04-13 16:23:31 +02:00
|
|
|
request_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
) -> TopMissedResponsePb:
|
2021-04-15 16:14:54 +02:00
|
|
|
return await self.stubs['documents'].top_missed(
|
2021-04-13 16:23:31 +02:00
|
|
|
TopMissedRequestPb(
|
|
|
|
page=page,
|
|
|
|
page_size=page_size,
|
|
|
|
),
|
|
|
|
metadata=(
|
|
|
|
('request-id', request_id),
|
2021-05-01 11:10:45 +02:00
|
|
|
('session-id', session_id),
|
|
|
|
('user-id', user_id),
|
2021-04-13 16:23:31 +02:00
|
|
|
),
|
|
|
|
)
|