Skip to content

Commit

Permalink
✨ Supplement the missing request generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mobyw committed Oct 14, 2024
1 parent 5f204ad commit e656de6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/app/src/blocks/nonebot_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const definitions: BlockDefinition[] = [
name: "TYPE",
options: [
["字典", "dict"],
["列表", "list"],
["文本", "string"],
["二进制", "binary"],
],
Expand Down Expand Up @@ -90,7 +91,7 @@ export const definitions: BlockDefinition[] = [
},
{
type: "input_value",
name: "HEADER",
name: "HEADERS",
align: "RIGHT",
check: "dict",
},
Expand All @@ -99,6 +100,7 @@ export const definitions: BlockDefinition[] = [
name: "TYPE",
options: [
["字典", "dict"],
["列表", "list"],
["文本", "string"],
["二进制", "binary"],
],
Expand Down
36 changes: 35 additions & 1 deletion packages/app/src/generators/nonebot_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Expand All @@ -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];
};

0 comments on commit e656de6

Please sign in to comment.