-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayutcli.py
executable file
·305 lines (261 loc) · 10.4 KB
/
payutcli.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import logging
import multiprocessing.dummy
import requests
import threading
import types
try:
import urlparse
from urllib import urlencode
except ImportError:
import urllib.parse as urlparse
from urllib.parse import urlencode
import webbrowser
from wsgiref.simple_server import make_server
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
def append_query_parameter(url, name, value):
"""Add the query string parameter 'name=value' to the given url.
Ex:
- ('/return', 'id', 42) -> '/return?id=42'
- ('/return?foo=bar', 'id', 42) -> '/return?foo=bar&id=42'
"""
scheme, netloc, path, query_string, fragment = urlparse.urlsplit(url)
query_params = urlparse.parse_qsl(query_string)
query_params.append((name, value))
new_query_string = urlencode(query_params, doseq=True)
return urlparse.urlunsplit((scheme, netloc, path, new_query_string, fragment))
class PayutcError(Exception):
def __init__(self, message, code, type, data=None):
super(PayutcError, self).__init__(message, code, type, data)
self.message = message
self.code = code
self.type = type
self.data = data
class Client(object):
def __init__(self, location, insecure=False, timeout=None, ssl_certificate=None, send_json=True,
app_key=None, system_id=None, proxies=None):
"""
:param location: Server location
:param insecure: Do not check ssl certificate (default: False, meaning secure mode enabled)
:param timeout: Http timeout (default: no-timeout)
:param ssl_certificate: Path to ssl certificate
:send_json: Send json instead of form-urlencoded (default: False)
:app_key: Send tha app_key to avoid use loginApp
:system_id: Add a parameter with system_id (for nemopay api)
:proxies: To set a proxy, put a dict like {'https': 'foo.bar:3128'}
"""
self.location = location.strip('/')
self.insecure = insecure
self.ssl_certificate = ssl_certificate
self.session = requests.Session()
self.session.proxies = proxies
self.timeout = None if timeout is None else float(timeout)
self.send_json = send_json
self.app_key = app_key
self.system_id = system_id
def call(self, service__, method, **kw):
"""service will be present in the kwargs, so we should call the service argument service__.
Try to remove it and call loginCas to see the bug :)
"""
url = '/'.join((self.location, service__, method))
if self.insecure:
verify = False
elif self.ssl_certificate:
verify = self.ssl_certificate
else:
verify = True
if self.app_key:
url = append_query_parameter(url, 'app_key', self.app_key)
if self.system_id:
url = append_query_parameter(url, 'system_id', self.system_id)
if self.send_json:
headers = {'content-type': 'application/json'}
data = json.dumps(kw)
else:
headers = {}
data = kw
try:
r = self.session.post(url, data=data, verify=verify, timeout=self.timeout, headers=headers)
except requests.exceptions.SSLError as e:
if 'certificate' in str(e):
print(e)
print("Use -k or --insecure to skip ssl certificate check")
raise
try:
r = r.json()
except Exception as e:
logger.exception("Error when parsing result for %s.%s" % (service__, method))
r = {
'error': {
'type': 'JsonDecodeError',
'message': "Error during parsing : %r" % r.text,
'code': -1,
}
}
if isinstance(r, dict) and 'error' in r:
if isinstance(r['error'], dict):
raise PayutcError(**r['error'])
else:
raise PayutcError(r['error'], 600, 'WeirdError')
return r
def clean_default_arg(arg):
if arg is False or arg is True or arg is None:
return arg
try:
float(arg)
return arg
except ValueError:
pass
return '"%s"' % arg
def prompt():
try:
from IPython import embed
embed()
except ImportError:
## this doesn't quite work right, in that it doesn't go to the right env
## so we just fail.
import code
import rlcompleter # NOQA
import readline
readline.parse_and_bind("tab: complete")
# calling this with globals ensures we can see the environment
shell = code.InteractiveConsole(globals())
shell.interact()
SERVICES = [
'AUTH',
'POSS3',
'STATS',
'KEY',
'ADMINRIGHT',
'BLOCKED',
'GESARTICLE',
'RELOAD',
'MYACCOUNT',
'MYACCOUNTEXT',
'TRANSFER',
'WEBSALE',
'WEBSALECONFIRM',
'MESSAGES',
'SELFPOS',
'TRESO',
]
class Service:
def __init__(self, name, client):
self.name = name
self.client = client
def reload(self):
methods = self.call('getMethods')
for method in methods:
self._add_method(method)
def call(self, method, **kw):
return self.client.call(self.name, method, **kw)
def _add_method(self, method_definition):
parameters = method_definition['parameters']
parameters.sort(key=lambda p: 'default' in p)
func_parameters = (p['name'] if 'default' not in p else '%s=%s' % (p['name'], clean_default_arg(p['default']))
for p in parameters)
call_parameters = ('{0}={0}'.format(p['name']) for p in parameters)
code = 'def f(self, {func_parameters}): return self.call("{method}", {call_parameters})'
code = code.format(
func_parameters=','.join(func_parameters),
method=method_definition['name'],
call_parameters=','.join(call_parameters))
logger.debug('%s => %s' % (method_definition['name'], code))
d = self.__exec(code)
d['f'].__doc__ = method_definition['comment']
f = types.MethodType(d['f'], self)
setattr(self, method_definition['name'], f)
def __exec(self, code):
d = {}
exec(code, d)
return d
class CliClient(Client):
def __init__(self, location, services=None, insecure=False, ssl_certificate=None, send_json=False,
system_id=None, app_key=None):
super(CliClient, self).__init__(location, insecure=insecure, ssl_certificate=ssl_certificate,
send_json=send_json, system_id=system_id, app_key=app_key)
if services is None:
services = SERVICES
def add_service(service):
try:
self.add_service(service)
except (ValueError, PayutcError) as ex:
logger.exception(ex)
p = multiprocessing.dummy.Pool(len(services))
p.map(add_service, services)
self.services = services
self.cas_ticket = None
self.wsgi_port = 9175
self.httpd = None
for _ in range(10000):
try:
self.httpd = make_server('', self.wsgi_port, self.wsgi_app)
break
except OSError as ex:
if ex.errno in (48, 98): # address already in use
self.wsgi_port += 1
else:
raise
else:
raise Exception('Cannot launch wsgi server')
self.wsgi_thread = threading.Thread(target=self.httpd.handle_request)
self.wsgi_thread.daemon = True
self.wsgi_thread.start()
self.wsgi_event = threading.Event()
#self.reload()
def reload(self):
for service in self.services:
getattr(self, service).reload()
def add_service(self, service):
setattr(self, service, Service(service, self))
logger.info("%s is ready", service)
def wsgi_app(self, environ, start_response):
if environ['PATH_INFO'] != '/cas':
start_response('404 NOT FOUND', [('Content-type', 'text/plain')])
return [b'']
parameters = urlparse.parse_qs(environ['QUERY_STRING'])
ticket = parameters['ticket'][0]
self.cas_ticket = ticket
self.wsgi_event.set()
r = ('Got ticket %s, you can go back to the cli' % ticket)
start_response('200 OK', [('Content-type', 'text/plain')])
return [r.encode('utf8')]
def get_cas_ticket(self, cas_url, timeout=30):
self.wsgi_event.clear()
webbrowser.open(cas_url + "login?service=http://localhost:%s/cas" % self.wsgi_port)
self.wsgi_event.wait(timeout=timeout)
return self.cas_ticket
def loginCas(self, service=None, cas_url=None):
if service is None:
service = self.services[0]
service = getattr(self, service)
if cas_url is None:
cas_url = service.call('getCasUrl')
ticket = self.get_cas_ticket(cas_url)
return service.call('loginCas', ticket=ticket, service="http://localhost:%s/cas" % self.wsgi_port)
def main():
global client
import argparse
parser = argparse.ArgumentParser(description='Connect to payutc server.')
parser.add_argument('-l', '--location', help='the server url', default='http://localhost/payutc/server/web')
parser.add_argument('-v', '--verbose', help='Increase verbosity', action="store_true")
parser.add_argument('-vv', '--verbose_plus', help='Increase verbosity', action="store_true")
parser.add_argument('-k', '--insecure', help='deactivate ssl check', action="store_true")
parser.add_argument('-c', '--cert', help='path to the ssl certificate')
parser.add_argument('-S', '--system-id', help='System id')
parser.add_argument('-A', '--app-key', help='Application key')
parser.add_argument('--form-urlencoded', help='use form-urlencoded content-type', action="store_true")
args = parser.parse_args()
if args.verbose_plus:
logger.setLevel(logging.DEBUG)
elif args.verbose:
logger.setLevel(logging.INFO)
client = CliClient(args.location, insecure=args.insecure, ssl_certificate=args.cert,
send_json=not args.form_urlencoded, system_id=args.system_id,
app_key=args.app_key)
prompt()
if __name__ == '__main__':
main()