web-client.py
4.06 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import sys
import requests
import json
from datetime import datetime
from time import (
sleep,
time,
)
from threading import Thread
from argparse import ArgumentParser
headers = {'content-type': 'application/json'}
threads = {}
end_threads = []
durations = {}
json_responses = {}
def error(s):
print('ERROR: {}'.format(s))
sys.exit()
def required(name, default=None):
value = getattr(option, name)
if not value and not default:
error('--{} harus diisi'.format(name))
p[name] = value or default
def create_thread(func, args=[]):
thread = Thread(target=func, args=args)
# Exit the server thread when the main thread terminates
thread.daemon = True
return thread
def send(p):
key = p['id']
log_info('Request: {}'.format(p))
start = time()
try:
resp = requests.post(url, data=json.dumps(p), headers=headers)
durations[key] = time() - start
json_resp = resp.json()
log_info('Response: {}'.format(json_resp))
json_responses[key] = json_resp
finally:
end_threads.append(key)
def log_info(s):
t = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
t = t[:-3]
msg = '{} {}'.format(t, s)
print(msg)
default_url = 'http://localhost:7000/rpc'
default_host = 'pemda'
default_count = 1
default_method = 'echo'
help_url = 'default ' + default_url
help_host = 'default ' + default_host
help_count = 'default {}'.format(default_count)
help_method = 'default ' + default_method
help_invoice_id = 'dibutuhkan saat --method inquiry, payment, atau reversal'
help_amount = 'dibutuhkan saat --method payment dan reversal'
help_ntb = 'dibutuhkan saat --method payment dan reversal'
help_stan = 'dibutuhkan saat --method payment dan reversal'
help_bit = 'bit tambahan, contoh: --bit=42:TOKOPEDIA'
help_conf = 'konfigurasi tambahan, contoh untuk multi: --conf=pajak:bphtb'
parser = ArgumentParser()
parser.add_argument('--url', default=default_url, help=help_url)
parser.add_argument('--host', default=default_host, help=help_host)
parser.add_argument(
'--count', type=int, default=default_count, help=help_count)
parser.add_argument('--method', default=default_method, help=help_method)
parser.add_argument('--invoice-id', help=help_invoice_id)
parser.add_argument('--amount', type=int, help=help_amount)
parser.add_argument('--ntb', help=help_ntb)
parser.add_argument('--stan', help=help_stan)
parser.add_argument('--bit', help=help_bit)
parser.add_argument('--conf', help=help_conf)
option = parser.parse_args(sys.argv[1:])
url = option.url
count = option.count
p = dict(host=option.host)
if option.method != 'echo':
p['invoice_id'] = option.invoice_id
if option.method in ('payment', 'reversal'):
required('amount')
if option.method == 'payment':
required('ntb', datetime.now().strftime('%y%m%d%H%m%s'))
required('stan', datetime.now().strftime('%H%M%S'))
else:
required('ntb')
required('stan')
if option.bit:
bits = dict()
for t in option.bit.split(','):
bit, value = t.split(':')
bits[bit] = value
p['bits'] = bits
if option.conf:
conf = dict()
for t in option.conf.split(','):
key, val = t.split(':')
conf[key] = val
p['conf'] = conf
data = dict(method=option.method, params=[p], jsonrpc='2.0')
for i in range(count):
data['id'] = i
thread = create_thread(send, [dict(data)])
threads[i] = thread
for key in threads:
thread = threads[key]
thread.start()
sleep(0.2)
while threads:
sleep(1)
if not end_threads:
continue
i = end_threads[0]
if i in threads:
thread = threads[i]
thread.join()
del threads[i]
index = end_threads.index(i)
del end_threads[index]
for key in durations:
val = durations[key]
resp = json_responses[key]
if 'error' in resp:
break
result = resp['result']
if result['code'] == 0:
stan = result['data']['11']
else:
stan = '-'
print('thread {} stan {} {} detik'.format(key, stan, val))