From 6ef9711f23f39dbb2dd52af21275110e51cdd011 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 15 Nov 2020 11:57:14 +0000 Subject: [PATCH] JSON RPC extension --- ext/json_rpc/info.php | 13 ++++++ ext/json_rpc/main.php | 106 ++++++++++++++++++++++++++++++++++++++++++ ext/json_rpc/test.php | 12 +++++ 3 files changed, 131 insertions(+) create mode 100644 ext/json_rpc/info.php create mode 100644 ext/json_rpc/main.php create mode 100644 ext/json_rpc/test.php diff --git a/ext/json_rpc/info.php b/ext/json_rpc/info.php new file mode 100644 index 00000000..c61608b9 --- /dev/null +++ b/ext/json_rpc/info.php @@ -0,0 +1,13 @@ +method = $method; + $this->params = $params; + $this->id = $id; + $this->result = null; + } + + public static function from_array(array $req) + { + $method = $req["method"]; + $params = $req["params"] ?? []; + $id = $req["id"]; + return new ApiRequestEvent($method, $params, $id); + } +} + +class JsonRpc extends Extension +{ + public function onPageRequest(PageRequestEvent $event) + { + global $page; + if ($event->page_matches("api/json")) { + $page->set_mode(PageMode::DATA); + $in = json_decode(file_get_contents('php://input'), true); + + // $in is a request + if (array_key_exists("jsonrpc", $in)) { + $out = $this->get_response($in); + if (!is_null($out)) { + $page->set_data(json_encode($out)); + } + } + // assume $in is a list of requests + else { + $out = []; + foreach ($in as $req) { + $res = $this->get_response($req); + if (!is_null($res)) { + $out[] = $res; + } + } + $page->set_data(json_encode($out)); + } + } + } + + public function onCommand(CommandEvent $event) + { + if ($event->cmd == "help") { + print "\tjson-rpc \n"; + print "\t\teg 'json-rpc get-posts'\n\n"; + } + if ($event->cmd == "json-rpc") { + print(json_encode($this->get_response([ + "id" => 1, + "method" => $event->args[0], + "params" => json_decode($event->args[1] ?? "[]"), + ]), JSON_PRETTY_PRINT) . "\n"); + } + } + + private function get_response(array $req): ?array + { + $evt = ApiRequestEvent::from_array($req); + try { + send_event($evt); + if (is_null($evt->id)) { + return null; + } + return [ + "jsonrpc" => "2.0", + "result" => $evt->result, + "id" => $evt->id, + ]; + } catch (Throwable $e) { + return [ + "jsonrpc" => "2.0", + "error" => [ + "code" => -1, + "message" => $e->getMessage(), + "data" => null, + ], + "id" => $evt->id, + ]; + } + } + + public function onApiRequest(ApiRequestEvent $event) + { + if ($event->method == "echo") { + $event->result = $event->params; + } + } +} diff --git a/ext/json_rpc/test.php b/ext/json_rpc/test.php new file mode 100644 index 00000000..9749eeea --- /dev/null +++ b/ext/json_rpc/test.php @@ -0,0 +1,12 @@ +"bar"], 1); + send_event($evt); + $this->assertEquals(1, $evt->id); + $this->assertEquals(["foo"=>"bar"], $evt->result); + } +}