Browse Source

plugin: Allow both array as well as object params in example plugin

Signed-off-by: Christian Decker <decker.christian@gmail.com>
plugin-3
Christian Decker 6 years ago
parent
commit
ec592e05b9
No known key found for this signature in database GPG Key ID: 1416D83DC4F0E86D
  1. 25
      contrib/plugins/helloworld.py

25
contrib/plugins/helloworld.py

@ -1,10 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Simple plugin to show how to build new plugins for c-lightning """Simple plugin to show how to build new plugins for c-lightning
It demonstrates how a plugin communicates with c-lightning, how it registers It demonstrates how a plugin communicates with c-lightning, how it
command line arguments that should be passed through and how it can register registers command line arguments that should be passed through and how
JSON-RPC commands. We communicate with the main daemon through STDIN and STDOUT, it can register JSON-RPC commands. We communicate with the main daemon
reading and writing JSON-RPC requests. through STDIN and STDOUT, reading and writing JSON-RPC requests.
""" """
import json import json
@ -14,8 +14,8 @@ import sys
greeting = "World" greeting = "World"
def json_hello(request): def json_hello(request, name):
greeting = "Hello {}".format(request['params']['name']) greeting = "Hello {}".format(name)
return greeting return greeting
@ -37,7 +37,7 @@ def json_getmanifest(request):
} }
def json_init(request): def json_init(request, options):
"""The main daemon is telling us the relevant cli options """The main daemon is telling us the relevant cli options
""" """
global greeting global greeting
@ -52,14 +52,23 @@ methods = {
'init': json_init, 'init': json_init,
} }
partial = "" partial = ""
for l in sys.stdin: for l in sys.stdin:
partial += l partial += l
try: try:
request = json.loads(partial) request = json.loads(partial)
result = None
method = methods[request['method']]
params = request['params']
if isinstance(params, dict):
result = method(request, **params)
else:
result = method(request, *params)
result = { result = {
"jsonrpc": "2.0", "jsonrpc": "2.0",
"result": methods[request['method']](request), "result": result,
"id": request['id'] "id": request['id']
} }
json.dump(result, fp=sys.stdout) json.dump(result, fp=sys.stdout)

Loading…
Cancel
Save