Skip to content

Commit 10f42ca

Browse files
committed
Refactor Router & Add torrent rechecking
1 parent cfe3fc4 commit 10f42ca

File tree

4 files changed

+182
-186
lines changed

4 files changed

+182
-186
lines changed

Deluge Remote.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@
724724
"@executable_path/Frameworks",
725725
);
726726
LIBRARY_SEARCH_PATHS = "";
727-
MARKETING_VERSION = 1.5.6;
727+
MARKETING_VERSION = 1.5.7;
728728
OTHER_LDFLAGS = "-ObjC";
729729
PRODUCT_BUNDLE_IDENTIFIER = "io.rudybermudez.Deluge-Remote";
730730
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -756,7 +756,7 @@
756756
"@executable_path/Frameworks",
757757
);
758758
LIBRARY_SEARCH_PATHS = "";
759-
MARKETING_VERSION = 1.5.6;
759+
MARKETING_VERSION = 1.5.7;
760760
OTHER_LDFLAGS = "-ObjC";
761761
PRODUCT_BUNDLE_IDENTIFIER = "io.rudybermudez.Deluge-Remote";
762762
PRODUCT_NAME = "$(TARGET_NAME)";

Deluge Remote/Sources/API/DelugeAPIRouter.swift

Lines changed: 99 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -9,200 +9,145 @@
99
import Foundation
1010
import Alamofire
1111

12-
enum DelugeRouter: URLRequestConvertible {
12+
13+
struct DelugeRouter: URLRequestConvertible {
14+
15+
private var route: DelugeRoute
16+
private var config: ClientConfig
17+
18+
init(_ route: DelugeRoute, _ config: ClientConfig) {
19+
self.route = route
20+
self.config = config
21+
}
22+
23+
func asURLRequest() throws -> URLRequest {
24+
let request = try URLRequest(url: config.url, method: route.method)
25+
let encoding = Alamofire.JSONEncoding.default
26+
return try encoding.encode(request, with: route.parameters)
27+
}
28+
}
29+
30+
enum DelugeRoute {
1331

1432
/// Authenticate to the Deluge API
15-
case login(ClientConfig)
33+
case login(_ password: String)
1634

17-
case checkAuth(ClientConfig)
35+
case checkAuth
1836

19-
case isConnected(ClientConfig)
37+
case isConnected
2038

2139
/// Get `TorrentMetadata`
22-
case getMetadata(ClientConfig,hash: String)
40+
case getMetadata(_ hash: String)
2341

2442
/// Get `TorrentFileStructure`
25-
case getFiles(ClientConfig, hash: String)
43+
case getFiles(_ hash: String)
2644

2745
/// Get `TorrentOverview`
28-
case getOverview(ClientConfig)
46+
case getOverview
2947

3048
/// Pause all torrents on the server
31-
case pauseAllTorrents(ClientConfig)
49+
case pauseAllTorrents
3250

3351
/// Pause an individual torrent
34-
case pause(ClientConfig, hash: String)
52+
case pause(_ hash: String)
3553

3654
/// Resume all torrents on the server
37-
case resumeAllTorrents(ClientConfig)
55+
case resumeAllTorrents
3856

3957
/// Resume an individual torrent
40-
case resume(ClientConfig, hash: String)
58+
case resume(_ hash: String)
4159

4260
/// Remove a torrent from the server
43-
case removeTorrent(ClientConfig, hash: String, withData: Bool)
61+
case removeTorrent(_ hash: String, _ withData: Bool)
4462

4563
/// Add a magnet URL to the server
46-
case addTorrentMagnet(ClientConfig, URL, config: TorrentConfig)
64+
case addTorrentMagnet(URL, TorrentConfig)
4765

4866
/// Get metadata about a magnet link
49-
case getMagnetInfo(ClientConfig, URL)
67+
case getMagnetInfo(URL)
5068

5169
/// Add a torrent file to the server
52-
case addTorrentFile(ClientConfig, filename: String, data: Data, config: TorrentConfig)
70+
case addTorrentFile(_ filename: String, Data, TorrentConfig)
5371

5472
/// Adds a torrent to the server from a URL
55-
case addTorrentURL(ClientConfig, URL, TorrentConfig)
73+
case addTorrentURL(URL, TorrentConfig)
5674

57-
case downloadTorrent(ClientConfig, from: URL)
75+
case downloadTorrent(URL)
5876

5977
/// Upload a torrent to the server
6078
///
6179
/// **Note**: This does not add the torrent to the client
62-
case uploadTorrentFile(ClientConfig, Data)
80+
case uploadTorrentFile(Data)
6381

6482
/// Get metadata about a torrent that was uploaded to the server
65-
case getUploadedTorrentInfo(ClientConfig, filename: String)
83+
case getUploadedTorrentInfo(_ filename: String)
6684

6785
/// Get the default torrent configuration
68-
case getDefaultTorrentConfig(ClientConfig)
86+
case getDefaultTorrentConfig
6987

7088
/// Set the options for a torrent
71-
case setTorrentOptions(ClientConfig, hash: String, JSON)
89+
case setTorrentOptions(_ hash: String, _ params: JSON)
7290

7391
/// Move a torrents filepath
74-
case moveTorrent(ClientConfig, hash: String, filePath: String)
92+
case moveTorrent(_ hash: String, _ filePath: String)
93+
94+
case forceRecheck(_ hash: String)
7595

7696
/// Get all the hosts on the server
77-
case getHosts(ClientConfig)
97+
case getHosts
7898

7999
/// Get the status of a host
80-
case getHostStatus(ClientConfig,Host)
100+
case getHostStatus(Host)
81101

82102
/// Connect to a server
83-
case connect(ClientConfig,Host)
103+
case connect(Host)
84104

85105
/// Get sessus status
86-
case getSessionStatus(ClientConfig)
87-
106+
case getSessionStatus
88107

89-
// MARK: - HTTPMethod
90-
private var method: HTTPMethod {
91-
/// Every method is a post method
92-
return .post
93-
}
94-
95-
private func paramsFor(method: String, with params: Any) -> Parameters {
96-
return [
97-
"id": id,
98-
"method": method,
99-
"params": params
100-
]
101-
}
102108

103-
private var baseURL: URL {
104-
switch self {
105-
case .login(let config):
106-
return config.url
107-
case .checkAuth(let config):
108-
return config.url
109-
case .isConnected(let config):
110-
return config.url
111-
case .getMetadata(let config, hash: _):
112-
return config.url
113-
case .getFiles(let config, hash: _):
114-
return config.url
115-
case .getOverview(let config):
116-
return config.url
117-
case .pauseAllTorrents(let config):
118-
return config.url
119-
case .pause(let config, hash: _):
120-
return config.url
121-
case .resumeAllTorrents(let config):
122-
return config.url
123-
case .resume(let config, hash: _):
124-
return config.url
125-
case .removeTorrent(let config, hash: _, withData: _):
126-
return config.url
127-
case .addTorrentMagnet(let config, _, config: _):
128-
return config.url
129-
case .getMagnetInfo(let config, _):
130-
return config.url
131-
case .addTorrentFile(let config, filename: _, data: _, config: _):
132-
return config.url
133-
case .addTorrentURL(let config, _, _):
134-
return config.url
135-
case .downloadTorrent(let config, from: _):
136-
return config.url
137-
case .uploadTorrentFile(let config, _):
138-
return config.url
139-
case .getUploadedTorrentInfo(let config, filename: _):
140-
return config.url
141-
case .getDefaultTorrentConfig(let config):
142-
return config.url
143-
case .setTorrentOptions(let config, hash: _, _):
144-
return config.url
145-
case .moveTorrent(let config, hash: _, filePath: _):
146-
return config.url
147-
case .getHosts(let config):
148-
return config.url
149-
case .getHostStatus(let config, _):
150-
return config.url
151-
case .connect(let config, _):
152-
return config.url
153-
case .getSessionStatus(let config):
154-
return config.url
155-
}
109+
// MARK:- Helpers
110+
fileprivate var method: HTTPMethod {
111+
// Every method is a post method
112+
switch self { default: return .post }
156113
}
157114

158115
// MARK: - Parameters
159-
private var parameters: Parameters? {
116+
fileprivate var parameters: Parameters? {
160117

161118
switch self {
162-
case .login(let config):
163-
return paramsFor( method: "auth.login", with: [config.password])
164-
case .checkAuth(_):
165-
return paramsFor(method: "auth.check_session", with: [])
166-
case .isConnected(_):
167-
return paramsFor(method: "web.connected", with: [])
168-
case .getMetadata(_, hash: let hash):
169-
return paramsFor(method: "core.get_torrent_status", with: [hash, []])
170-
case .getFiles(_, hash: let hash):
171-
return paramsFor(method: "web.get_torrent_files", with: [hash])
172-
case .getOverview(_):
173-
return paramsFor(method: "core.get_torrents_status",
174-
with: [[], ["name", "hash", "upload_payload_rate",
175-
"download_payload_rate", "ratio",
176-
"progress", "total_wanted", "state",
177-
"tracker_host", "label", "eta",
178-
"total_size", "all_time_download",
179-
"total_uploaded", "time_added", "paused"]])
180-
case .pause(_, hash: let hash):
181-
return paramsFor(method: "core.pause_torrent", with: [[hash]])
182-
case .pauseAllTorrents(_):
183-
return paramsFor(method: "core.pause_all_torrents", with: [])
184-
case .resumeAllTorrents(_):
185-
return paramsFor(method: "core.resume_all_torrents", with: [])
186-
case .resume(_, hash: let hash):
187-
return paramsFor(method: "core.resume_torrent", with: [[hash]])
188-
case .removeTorrent(_, hash: let hash, withData: let withData):
189-
return paramsFor(method: "core.remove_torrent", with: [hash, withData])
190-
case .addTorrentMagnet(_, let url, config: let config):
191-
return paramsFor(method: "core.add_torrent_magnet", with: [url.absoluteString, config.toParams()])
192-
case .downloadTorrent(_, from: let url):
193-
return paramsFor(method: "web.download_torrent_from_url", with: [url.absoluteString, []])
194-
case .addTorrentURL(_, let url, let config):
195-
return paramsFor(method: "core.add_torrent_url", with: [url.absoluteString, config.toParams()])
196-
case .getMagnetInfo(_, let url):
197-
return paramsFor(method: "web.get_magnet_info", with: [url.absoluteString])
198-
case .addTorrentFile(_, filename: let filename, data: let data, config: let config):
199-
return paramsFor(method: "core.add_torrent_file", with: [filename, data.base64EncodedString(), config.toParams()])
200-
case .uploadTorrentFile(_,_):
201-
return nil
202-
case .getUploadedTorrentInfo(_, let filename):
203-
return paramsFor(method: "web.get_torrent_info", with: [filename])
204-
case .getDefaultTorrentConfig(_):
205-
return paramsFor(method: "core.get_config_values", with: [[
119+
case .login(let password): return params(for: "auth.login", with: [password])
120+
case .checkAuth: return params(for: "auth.check_session", with: [])
121+
case .isConnected: return params(for: "web.connected", with: [])
122+
case .getMetadata(let hash): return params(for: "core.get_torrent_status", with: [hash, []])
123+
case .getFiles(let hash): return params(for: "web.get_torrent_files", with: [hash])
124+
case .pause(let hash): return params(for: "core.pause_torrent", with: [[hash]])
125+
case .pauseAllTorrents: return params(for: "core.pause_all_torrents", with: [])
126+
case .resumeAllTorrents: return params(for: "core.resume_all_torrents", with: [])
127+
case .resume(let hash): return params(for: "core.resume_torrent", with: [[hash]])
128+
case .downloadTorrent(let url): return params(for: "web.download_torrent_from_url", with: [url.absoluteString, []])
129+
case .getMagnetInfo(let url): return params(for: "web.get_magnet_info", with: [url.absoluteString])
130+
case .forceRecheck(let hash): return params(for: "core.force_recheck", with: [[hash]]);
131+
case .getHosts: return params(for: "web.get_hosts", with: [])
132+
case .getHostStatus(let host): return params(for:"web.get_host_status", with: [host.id])
133+
case .connect( let host): return params(for: "web.connect", with: [host.id])
134+
case .uploadTorrentFile(_): return nil
135+
case .setTorrentOptions(let hash, let options):
136+
return params(for: "core.set_torrent_options", with: [[hash], options])
137+
case .moveTorrent( let hash, let filePath):
138+
return params(for:"core.move_storage", with: [[hash], filePath])
139+
case .removeTorrent( let hash, let withData):
140+
return params(for: "core.remove_torrent", with: [hash, withData])
141+
case .addTorrentMagnet(let url, let config):
142+
return params(for: "core.add_torrent_magnet", with: [url.absoluteString, config.toParams()])
143+
case .addTorrentURL(let url, let config):
144+
return params(for: "core.add_torrent_url", with: [url.absoluteString, config.toParams()])
145+
case .addTorrentFile( let filename, let data, let config):
146+
return params(for: "core.add_torrent_file", with: [filename, data.base64EncodedString(), config.toParams()])
147+
case .getUploadedTorrentInfo(let filename):
148+
return params(for: "web.get_torrent_info", with: [filename])
149+
case .getDefaultTorrentConfig:
150+
return params(for: "core.get_config_values", with: [[
206151
"add_paused",
207152
"compact_allocation",
208153
"download_location",
@@ -214,18 +159,16 @@ enum DelugeRouter: URLRequestConvertible {
214159
"max_upload_speed_per_torrent",
215160
"prioritize_first_last_pieces"
216161
]])
217-
case .setTorrentOptions(_,hash: let hash, let options):
218-
return paramsFor(method: "core.set_torrent_options", with: [[hash], options])
219-
case .moveTorrent(_,hash: let hash, filePath: let filePath):
220-
return paramsFor(method: "core.move_storage", with: [[hash], filePath])
221-
case .getHosts(_):
222-
return paramsFor(method: "web.get_hosts", with: [])
223-
case .getHostStatus(_, let host):
224-
return paramsFor(method: "web.get_host_status", with: [host.id])
225-
case .connect(_, let host):
226-
return paramsFor(method: "web.connect", with: [host.id])
227-
case .getSessionStatus(_):
228-
return paramsFor(method: "core.get_session_status", with: [[
162+
case .getOverview:
163+
return params(for: "core.get_torrents_status",
164+
with: [[], ["name", "hash", "upload_payload_rate",
165+
"download_payload_rate", "ratio",
166+
"progress", "total_wanted", "state",
167+
"tracker_host", "label", "eta",
168+
"total_size", "all_time_download",
169+
"total_uploaded", "time_added", "paused"]])
170+
case .getSessionStatus:
171+
return params(for: "core.get_session_status", with: [[
229172
"has_incoming_connections",
230173
"upload_rate",
231174
"download_rate",
@@ -269,10 +212,11 @@ enum DelugeRouter: URLRequestConvertible {
269212
private var id: UInt32 {
270213
return arc4random()
271214
}
272-
273-
func asURLRequest() throws -> URLRequest {
274-
let request = try URLRequest(url: baseURL, method: method)
275-
let encoding = Alamofire.JSONEncoding.default
276-
return try encoding.encode(request, with: parameters)
215+
private func params(for method: String, with options: Any) -> Parameters {
216+
return [
217+
"id": id,
218+
"method": method,
219+
"params": options
220+
]
277221
}
278222
}

0 commit comments

Comments
 (0)