|
| 1 | +// |
| 2 | +// RouteKeeper.swift |
| 3 | +// MacroXmlRpc |
| 4 | +// |
| 5 | +// Created by Helge Hess. |
| 6 | +// Copyright © 2020 ZeeZide GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import MacroExpress |
| 10 | +import XmlRpc |
| 11 | + |
| 12 | +extension RouteKeeper { |
| 13 | + |
| 14 | + @discardableResult |
| 15 | + func rpc(_ methodName: String? = nil, |
| 16 | + execute: @escaping |
| 17 | + ( XmlRpc.Call ) throws -> XmlRpcValueRepresentable) |
| 18 | + -> Self |
| 19 | + { |
| 20 | + post { req, res, next in |
| 21 | + if let methodName = methodName { |
| 22 | + let methods = (req.extra["rpc.methods"] as? [ String ]) ?? [] |
| 23 | + req.extra["rpc.methods"] = methods + [ methodName ] |
| 24 | + } |
| 25 | + |
| 26 | + guard let call = XmlRpc.parseCall(req.body.text ?? "") else { |
| 27 | + return res.sendStatus(400) |
| 28 | + } |
| 29 | + |
| 30 | + if let methodName = methodName, call.methodName != methodName { |
| 31 | + return next() |
| 32 | + } |
| 33 | + |
| 34 | + do { |
| 35 | + let value = try execute(call) |
| 36 | + res.send(XmlRpc.Response.value(value.xmlRpcValue).xmlString) |
| 37 | + } |
| 38 | + catch let error as XmlRpc.Fault { |
| 39 | + res.send(XmlRpc.Response.fault(error).xmlString) |
| 40 | + } |
| 41 | + catch { |
| 42 | + res.sendStatus(500) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @discardableResult |
| 48 | + func systemListMethods() -> Self { |
| 49 | + post { req, res, next in |
| 50 | + guard let call = XmlRpc.parseCall(req.body.text ?? ""), |
| 51 | + call.methodName == "system.listMethods" else { |
| 52 | + return next() |
| 53 | + } |
| 54 | + let methods = (req.extra["rpc.methods"] as? [ String ]) ?? [] |
| 55 | + res.send(XmlRpc.Response(methods).xmlString) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +extension RouteKeeper { |
| 61 | + |
| 62 | + @discardableResult |
| 63 | + func rpc<A1>(_ methodName: String, |
| 64 | + execute: @escaping ( A1 ) |
| 65 | + throws -> XmlRpcValueRepresentable) |
| 66 | + -> Self |
| 67 | + where A1: XmlRpcValueRepresentable |
| 68 | + { |
| 69 | + rpc(methodName) { call in |
| 70 | + guard call.parameters.count == 1, |
| 71 | + let a1 = A1(xmlRpcValue: call.parameters[0]) |
| 72 | + else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") } |
| 73 | + return try execute(a1) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @discardableResult |
| 78 | + func rpc<A1, A2>(_ methodName: String, |
| 79 | + execute: @escaping ( A1, A2 ) |
| 80 | + throws -> XmlRpcValueRepresentable) |
| 81 | + -> Self |
| 82 | + where A1: XmlRpcValueRepresentable, |
| 83 | + A2: XmlRpcValueRepresentable |
| 84 | + { |
| 85 | + rpc(methodName) { call in |
| 86 | + guard call.parameters.count == 2, |
| 87 | + let a1 = A1(xmlRpcValue: call.parameters[0]), |
| 88 | + let a2 = A2(xmlRpcValue: call.parameters[1]) |
| 89 | + else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") } |
| 90 | + return try execute(a1, a2) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments