Skip to content

Commit 4edd89b

Browse files
committed
Initial setup based on blog
...
1 parent 0896b93 commit 4edd89b

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ let package = Package(
1111
],
1212

1313
dependencies: [
14+
.package(url: "https://github.com/Macro-swift/Macro.git",
15+
from: "0.5.9"),
1416
.package(url: "https://github.com/Macro-swift/MacroExpress.git",
1517
from: "0.5.7"),
1618
.package(url: "https://github.com/AlwaysRightInstitute/SwiftXmlRpc.git",
1719
from: "0.8.5")
1820
],
1921

2022
targets: [
21-
.target(name: "MacroXmlRpc", dependencies: [ "MacroExpress", "XmlRpc" ])
23+
.target(name: "MacroXmlRpc",
24+
dependencies: [ "Macro", "MacroExpress", "XmlRpc" ])
2225
]
2326
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Middleware.swift
3+
// MacroXmlRpc
4+
//
5+
// Created by Helge Hess.
6+
// Copyright © 2020 ZeeZide GmbH. All rights reserved.
7+
//
8+
9+
// TODO: Try to mirror the JS server in http://xmlrpc.com
10+
public enum xmlrpc {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// ReExports.swift
3+
// MacroXmlRpc
4+
//
5+
// Created by Helge Hess.
6+
// Copyright © 2020 ZeeZide GmbH. All rights reserved.
7+
//
8+
9+
@_exported import Macro
10+
@_exported import MacroExpress
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)