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
"""Simple plugin to show how to build new plugins for c-lightning
It demonstrates how a plugin communicates with c-lightning, how it registers
command line arguments that should be passed through and how it can register
JSON-RPC commands. We communicate with the main daemon through STDIN and STDOUT,
reading and writing JSON-RPC requests.
It demonstrates how a plugin communicates with c-lightning, how it
registers command line arguments that should be passed through and how
it can register JSON-RPC commands. We communicate with the main daemon
through STDIN and STDOUT, reading and writing JSON-RPC requests.
"""
import json
@ -14,8 +14,8 @@ import sys
greeting = "World"
def json_hello(request):
greeting = "Hello {}".format(request['params']['name'])
def json_hello(request, name):
greeting = "Hello {}".format(name)
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
"""
global greeting
@ -52,14 +52,23 @@ methods = {
'init': json_init,
}
partial = ""
for l in sys.stdin:
partial += l
try:
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 = {
"jsonrpc": "2.0",
"result": methods[request['method']](request),
"result": result,
"id": request['id']
}
json.dump(result, fp=sys.stdout)

Loading…
Cancel
Save