==================
URLServiceRouter 是一个基于 URL 用 Swift 写的一个路由分发库,由一个高自由度的 nodeTree 跟 RPC 来实现的。
pod 'URLServiceRouter'
.pod install
或 pod update
.import URLServiceRouter
github "lightank/URLServiceRouter"
.carthage update --platform ios
并把 framework 添加到你的 Target 中.import URLServiceRouter
https://github.com/lightank/URLServiceRouter.git
import URLServiceRouter
我们假设
www.realword.com
, 测试环境是:*.realword.io
http://china.realword.io/owner/<owner_id>/info
代表查询 owner_id
对应的用户信息注册 owner 服务
URLServiceRouter.shared.registerService(name: "user://info") {
URLOwnerInfoService()
}
注册将测试环境host转为生产环境host的解析器
URLServiceRouter.shared.registerNode(from: "https") {
[URLServiceRedirectTestHostParser()]
}
注册 owner URL 到 nodeTree 中
do {
URLServiceRouter.shared.registerNode(from: "https://www.realword.com/owner/info") {
[URLServiceNoramlParser(parserType: .post, parseBlock: { nodeParser, _, decision in
decision.complete(nodeParser, "user://info")
})]
}
}
do {
URLServiceRouter.shared.registerNode(from: "https://www.realword.com/owner/") {
let preParser = URLServiceNoramlParser(parserType: .pre, parseBlock: { nodeParser, request, decision in
var nodeNames = request.nodeNames
if let first = nodeNames.first, first.isPureInt {
request.merge(params: ["id": nodeNames.remove(at: 0)], from: nodeParser)
request.replace(nodeNames: nodeNames, from: nodeParser)
}
decision.next()
})
let postParser = URLServiceNoramlParser(parserType: .post, parseBlock: { _, _, decision in
decision.next()
})
return [preParser, postParser]
}
}
请求 owner 服务
if let url = URL(string: "http://china.realword.io/owner/1/info") {
URLServiceRequest(url: url).start(callback: { request in
if let data = request.response?.data {
// 正确的数据
self.showAlertMessge(title: "回调的业务数据", message: String(describing: data))
}
URLServiceRouter.shared.logInfo("\(String(describing: request.response?.data))")
})
}
直接调用服务
URLServiceRouter.shared.callService(name: "user://info", params: "1") { _, _ in
} callback: { result, _ in
self.showAlertMessge(title: "回调的业务数据", message: String(describing: result))
URLServiceRouter.shared.logInfo("\(String(describing: result))")
}
link |
Stars: 37 |
Last commit: 3 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics