Fix bugs when dispatch the RPC method

This commit is contained in:
Namhyeon Go 2024-10-25 08:59:49 +09:00
parent 08212459eb
commit 44425dbb8b
4 changed files with 24 additions and 9 deletions

10
base.py
View File

@ -166,9 +166,13 @@ class Extension:
@classmethod @classmethod
def get_rpcmethod(cls, method): def get_rpcmethod(cls, method):
for extension in cls.extensions: for extension in cls.extensions:
is_exported_method = (method == extension.method) or ( is_exported_method = False
method in extension.exported_methods try:
) is_exported_method = (method == extension.method) or (
method in extension.exported_methods
)
except:
pass
if extension.type == "rpcmethod" and is_exported_method: if extension.type == "rpcmethod" and is_exported_method:
return extension return extension
return None return None

View File

@ -296,7 +296,7 @@
if (env.method == "analyze_sequence") { if (env.method == "analyze_sequence") {
var _this = this; var _this = this;
this.read("Enter the sequence:\r\n", function(message) { this.read("Enter the sequence:\r\n", function(message) {
jsonrpc2_request(this, env.method, { jsonrpc2_request(_this, env.method, {
"sequence": message "sequence": message
}); });
}); });
@ -307,7 +307,7 @@
if (env.method == "gc_content_calculation") { if (env.method == "gc_content_calculation") {
var _this = this; var _this = this;
this.read("Enter the sequence:\r\n", function(message) { this.read("Enter the sequence:\r\n", function(message) {
jsonrpc2_request(this, env.method, { jsonrpc2_request(_this, env.method, {
"sequence": message "sequence": message
}); });
}); });

View File

@ -2,3 +2,5 @@ python-decouple
requests requests
aiosmtpd aiosmtpd
ruff ruff
flask
flask_cors

17
web.py
View File

@ -7,18 +7,20 @@
# Namyheon Go (Catswords Research) <gnh1201@gmail.com> # Namyheon Go (Catswords Research) <gnh1201@gmail.com>
# https://github.com/gnh1201/caterpillar # https://github.com/gnh1201/caterpillar
# Created at: 2024-05-20 # Created at: 2024-05-20
# Updated at: 2024-07-10 # Updated at: 2024-10-25
# #
import os import os
import sys import sys
from decouple import config from decouple import config
from flask import Flask, request, render_template from flask import Flask, request, render_template
from flask_cors import CORS
from base import Extension, jsonrpc2_error_encode, Logger from base import Extension, jsonrpc2_error_encode, Logger
# TODO: 나중에 Flask 커스텀 핸들러 구현 해야 함 # TODO: 나중에 Flask 커스텀 핸들러 구현 해야 함
logger = Logger(name="web") logger = Logger(name="web")
app = Flask(__name__) app = Flask(__name__)
CORS(app)
app.config["UPLOAD_FOLDER"] = "data/" app.config["UPLOAD_FOLDER"] = "data/"
if not os.path.exists(app.config["UPLOAD_FOLDER"]): if not os.path.exists(app.config["UPLOAD_FOLDER"]):
os.makedirs(app.config["UPLOAD_FOLDER"]) os.makedirs(app.config["UPLOAD_FOLDER"])
@ -51,9 +53,16 @@ def process_jsonrpc2():
# JSON-RPC 2.0 request # JSON-RPC 2.0 request
json_data = request.get_json(silent=True) json_data = request.get_json(silent=True)
if json_data["jsonrpc"] == "2.0": if json_data["jsonrpc"] == "2.0":
return Extension.dispatch_rpcmethod( result = Extension.dispatch_rpcmethod(
json_data["method"], "call", json_data["id"], json_data["params"], conn json_data["method"], "call", json_data["id"], json_data["params"], conn)
)
return {
"jsonrpc": "2.0",
"result": {
"data": result
},
"id": None
}
# when error # when error
return jsonrpc2_error_encode({"message": "Not valid JSON-RPC 2.0 request"}) return jsonrpc2_error_encode({"message": "Not valid JSON-RPC 2.0 request"})