This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
/
contract.py
84 lines (68 loc) · 2.94 KB
/
contract.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2017 Intel Corporation
#
# 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.
# ------------------------------------------------------------------------------
import logging
from sanic import Blueprint
from sanic import response
from rest_api.insurance_common import transaction
from rest_api.workflow import general, security_messaging
from rest_api.workflow.errors import ApiBadRequest, ApiInternalError
CONTRACT_BP = Blueprint('contract')
logging.basicConfig(level=logging.DEBUG)
LOGGER = logging.getLogger(__name__)
@CONTRACT_BP.get('contract')
async def get_all_contacts(request):
"""Fetches complete details of all Accounts in state"""
LOGGER.debug("Call 'contract' request")
client_key = general.get_request_key_header(request)
contract_list = await security_messaging.get_contracts(request.app.config.VAL_CONN, client_key)
contract_list_json = []
for address, con in contract_list.items():
contract_list_json.append({
'client_pkey': con.client_pkey,
'id': con.id,
'name': con.name,
'surname': con.surname
})
return response.json(body={'data': contract_list_json},
headers=general.get_response_headers())
@CONTRACT_BP.post('contract')
async def add_new_contract(request):
"""Updates auth information for the authorized account"""
client_key = general.get_request_key_header(request)
required_fields = ['id', 'client_pkey']
general.validate_fields(required_fields, request.json)
uid = request.json.get('id')
contractor_pkey = request.json.get('client_pkey')
client_signer = general.get_signer(request, client_key)
contract_txn = transaction.add_contract(
txn_signer=client_signer,
batch_signer=client_signer,
uid=uid,
client_pkey=contractor_pkey
)
batch, batch_id = transaction.make_batch_and_id([contract_txn], client_signer)
await security_messaging.add_contract(
request.app.config.VAL_CONN,
request.app.config.TIMEOUT,
[batch], client_key)
try:
await security_messaging.check_batch_status(
request.app.config.VAL_CONN, [batch_id])
except (ApiBadRequest, ApiInternalError) as err:
# await auth_query.remove_auth_entry(
# request.app.config.DB_CONN, request.json.get('email'))
raise err
return response.json(body={'status': general.DONE},
headers=general.get_response_headers())