forked from amundsen-io/amundsen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_update.py
44 lines (36 loc) · 1.99 KB
/
document_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import json
from http import HTTPStatus
from typing import Any, Tuple
from amundsen_common.models.search import UpdateDocumentRequestSchema
from flasgger import swag_from
from flask_restful import Resource, request
from search_service.proxy import get_proxy_client
from search_service.proxy.es_proxy_utils import RESOURCE_STR_MAPPING
class DocumentAPI(Resource):
def __init__(self) -> None:
self.proxy = get_proxy_client()
self.request = UpdateDocumentRequestSchema().loads(json.dumps(request.get_json()))
@swag_from('swagger_doc/search/document_post.yml')
def post(self) -> Tuple[Any, int]:
try:
resp = self.proxy.update_document_by_key(resource_key=self.request.resource_key,
resource_type=RESOURCE_STR_MAPPING[self.request.resource_type],
field=self.request.field,
value=self.request.value,
operation=self.request.operation)
return {'msg': resp}, HTTPStatus.OK
except Exception as e:
err_msg = f'Failed to update the field value: {e}'
return {'message': err_msg}, HTTPStatus.INTERNAL_SERVER_ERROR
def delete(self) -> Tuple[Any, int]:
try:
resp = self.proxy.delete_document_by_key(resource_key=self.request.resource_key,
resource_type=RESOURCE_STR_MAPPING[self.request.resource_type],
field=self.request.field,
value=self.request.value)
return {'msg': resp}, HTTPStatus.OK
except Exception as e:
err_msg = f'Failed to delete the field value: {e}'
return {'message': err_msg}, HTTPStatus.INTERNAL_SERVER_ERROR