Skip to content

Commit

Permalink
chore: #310 更换xmlrpc解析库-通用同一套请求逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jan 6, 2023
1 parent 975ccfd commit d49f535
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions libs/simple-xmlrpc/impl/chromeXmlrpc.ts
Expand Up @@ -26,6 +26,7 @@
import { sendChromeMessage } from "~/utils/otherlib/ChromeUtil"
import { LogFactory } from "~/utils/logUtil"
import { Deserializer, serializeMethodCall } from "~/libs/simple-xmlrpc/xmlrpc"
import { XmlrpcUtil } from "~/libs/simple-xmlrpc/custom/xmlrpcUtil"

const logger = LogFactory.getLogger("libs/simple-xmlrpc/impl/chromeXmlrpc.ts")

Expand All @@ -51,24 +52,24 @@ async function doChromeFetch(
body: methodBodyXml,
}

const resXml = await sendChromeMessage({
let resText = await sendChromeMessage({
// 里面的值应该可以自定义,用于判断哪个请求之类的
type: "fetchChromeXmlrpc",
apiUrl, // 需要请求的url
fetchCORSParams,
})
logger.debug("fetchChromeXmlrpc resXml=>", resXml)
logger.debug("fetchChromeXmlrpc开始,resText=>", resText)

let data
if (resText) {
resText = XmlrpcUtil.removeXmlHeader(resText)

let resJson
if (resXml) {
const deserializer = new Deserializer("utf-8")
const resJson = await deserializer.deserializeMethodResponse(resXml)
logger.debug("resJson=>", JSON.stringify(resJson))
} else {
resJson = {}
data = await deserializer.deserializeMethodResponse(resText)
logger.debug("fetchChromeXmlrpc=结束,data=>", data)
}

return resJson
return data
} catch (e: any) {
throw new Error(e)
}
Expand Down
8 changes: 4 additions & 4 deletions libs/simple-xmlrpc/impl/nodeXmlrpc.ts
Expand Up @@ -25,24 +25,24 @@

import { LogFactory } from "~/utils/logUtil"
import { SimpleXmlRpcClient } from "~/libs/simple-xmlrpc/custom/SimpleXmlRpcClient"
import { XmlRpcValue } from "~/libs/simple-xmlrpc/xmlrpc"

const logger = LogFactory.getLogger("libs/simple-xmlrpc/impl/nodeXmlrpc.ts")

export async function fetchNode(
apiUrl: string,
reqMethod: string,
reqParams: string[]
): Promise<string> {
): Promise<XmlRpcValue> {
try {
logger.debug("SimpleXmlRpcClient开始")
logger.debug("xmlrpcNodeParams.reqMethod=>", reqMethod)
logger.debug("xmlrpcNodeParams.reqParams=>", reqParams)

const client = new SimpleXmlRpcClient(apiUrl)
const data = await client.methodCall(reqMethod, reqParams)
const dataJson = JSON.stringify(data)
logger.debug("dataJson=>", dataJson)
return dataJson
logger.debug("SimpleXmlRpcClient结束,data=>", data)
return data
} catch (e) {
logger.error(e)
throw new Error("请求处理异常")
Expand Down

0 comments on commit d49f535

Please sign in to comment.