views.py
1.66 KB
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
import random
import json
import traceback
from io import StringIO
from logging import getLogger
from pyramid.view import view_config
from pyramid.response import Response
from cryptography.exceptions import InvalidSignature
from pyramid_snap.view import Base
from pyramid_snap.structure import (
SERVICE_VA_CREATE,
RC_UNKNOWN_ERROR,
VA_FIELDS,
)
from pyramid_snap.decorator import (
json_method,
create_response,
)
from pyramid_snap.validation import BaseError
def exception_message():
f = StringIO()
traceback.print_exc(file=f)
s = f.getvalue()
f.close()
return s
@view_config(context=Exception)
def view_exception(exc, request):
log = getLogger('view_exception')
log.error(exception_message())
if isinstance(exc, BaseError):
msg = exc[0]
elif isinstance(exc, InvalidSignature):
msg = 'Signature tidak valid'
else:
msg = 'Ada kesalahan yang belum dipahami'
resp = create_response('00', RC_UNKNOWN_ERROR, msg)
try:
d = resp.json
except json.decoder.JSONDecodeError:
d = [resp.text] # Agar tetap 1 baris
log.error(
'to %s headers %s response %s', request.client_addr,
dict(resp.headers), d)
return resp
def nomor_acak(jml: int) -> str:
return '08899' + ''.join(
str(random.randint(0, 9)) for _ in range(jml))
class API(Base):
@json_method(
route_name='create-va', service_code=SERVICE_VA_CREATE,
fields=VA_FIELDS)
def create_va(self, params: dict) -> dict:
vacc = nomor_acak(10)
vacc_data = params.copy()
vacc_data['virtualAccountNo'] = vacc
return dict(virtualAccountData=vacc_data)