From e656de6d9da6204bd3a639c298a7b3b81bedd9d7 Mon Sep 17 00:00:00 2001 From: mobyw Date: Mon, 14 Oct 2024 23:40:31 +0800 Subject: [PATCH] :sparkles: Supplement the missing request generator --- packages/app/src/blocks/nonebot_request.ts | 4 ++- .../app/src/generators/nonebot_request.ts | 36 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/app/src/blocks/nonebot_request.ts b/packages/app/src/blocks/nonebot_request.ts index 5e17b65..893c0ab 100644 --- a/packages/app/src/blocks/nonebot_request.ts +++ b/packages/app/src/blocks/nonebot_request.ts @@ -34,6 +34,7 @@ export const definitions: BlockDefinition[] = [ name: "TYPE", options: [ ["字典", "dict"], + ["列表", "list"], ["文本", "string"], ["二进制", "binary"], ], @@ -90,7 +91,7 @@ export const definitions: BlockDefinition[] = [ }, { type: "input_value", - name: "HEADER", + name: "HEADERS", align: "RIGHT", check: "dict", }, @@ -99,6 +100,7 @@ export const definitions: BlockDefinition[] = [ name: "TYPE", options: [ ["字典", "dict"], + ["列表", "list"], ["文本", "string"], ["二进制", "binary"], ], diff --git a/packages/app/src/generators/nonebot_request.ts b/packages/app/src/generators/nonebot_request.ts index 3db0bb6..a136410 100644 --- a/packages/app/src/generators/nonebot_request.ts +++ b/packages/app/src/generators/nonebot_request.ts @@ -34,6 +34,9 @@ if not isinstance(driver, HTTPClientMixin): \n\ if (type === "dict") { generator["definitions_"]["import json"] = "import json"; code = `json.loads(${content} or "{}")`; + } else if (type === "list") { + generator["definitions_"]["import json"] = "import json"; + code = `json.loads(${content} or "[]")`; } return [code, Order.ATOMIC]; }; @@ -42,6 +45,37 @@ forBlock["request_post"] = function ( block: Blockly.Block, generator: PythonGenerator, ) { - const code = `\n`; + const url = generator.valueToCode(block, "URL", Order.ATOMIC); + const json = generator.valueToCode(block, "JSON", Order.ATOMIC) || "None"; + const params = generator.valueToCode(block, "PARAMS", Order.ATOMIC) || "None"; + const headers = + generator.valueToCode(block, "HEADERS", Order.ATOMIC) || "None"; + const type = block.getFieldValue("TYPE"); + const timeout = block.getFieldValue("TIMEOUT"); + if (url === "" || url === "None") { + return ["None", Order.ATOMIC]; + } + generator["definitions_"]["from nonebot import get_driver"] = + "from nonebot import get_driver"; + generator["definitions_"]["from nonebot.drivers import Request"] = + "from nonebot.drivers import Request"; + generator["definitions_"]["from nonebot.drivers import HTTPClientMixin"] = + "from nonebot.drivers import HTTPClientMixin"; + generator["definitions_"]["nonebot_request_driver"] = + 'driver = get_driver() \n\ + if not isinstance(driver, HTTPClientMixin): \n\ + raise RuntimeError( \n\ + f"Current driver {driver} does not support http client requests!" \n\ + )'; + const request = `Request("POST", ${url}, json=${json}, params=${params}, headers=${headers}, timeout=${timeout})`; + const content = `(await driver.request(${request})).content`; + let code = content; + if (type === "dict") { + generator["definitions_"]["import json"] = "import json"; + code = `json.loads(${content} or "{}")`; + } else if (type === "list") { + generator["definitions_"]["import json"] = "import json"; + code = `json.loads(${content} or "[]")`; + } return [code, Order.ATOMIC]; };