9
9
import Foundation
10
10
import Alamofire
11
11
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 {
13
31
14
32
/// Authenticate to the Deluge API
15
- case login( ClientConfig )
33
+ case login( _ password : String )
16
34
17
- case checkAuth( ClientConfig )
35
+ case checkAuth
18
36
19
- case isConnected( ClientConfig )
37
+ case isConnected
20
38
21
39
/// Get `TorrentMetadata`
22
- case getMetadata( ClientConfig , hash: String )
40
+ case getMetadata( _ hash: String )
23
41
24
42
/// Get `TorrentFileStructure`
25
- case getFiles( ClientConfig , hash: String )
43
+ case getFiles( _ hash: String )
26
44
27
45
/// Get `TorrentOverview`
28
- case getOverview( ClientConfig )
46
+ case getOverview
29
47
30
48
/// Pause all torrents on the server
31
- case pauseAllTorrents( ClientConfig )
49
+ case pauseAllTorrents
32
50
33
51
/// Pause an individual torrent
34
- case pause( ClientConfig , hash: String )
52
+ case pause( _ hash: String )
35
53
36
54
/// Resume all torrents on the server
37
- case resumeAllTorrents( ClientConfig )
55
+ case resumeAllTorrents
38
56
39
57
/// Resume an individual torrent
40
- case resume( ClientConfig , hash: String )
58
+ case resume( _ hash: String )
41
59
42
60
/// Remove a torrent from the server
43
- case removeTorrent( ClientConfig , hash: String , withData: Bool )
61
+ case removeTorrent( _ hash: String , _ withData: Bool )
44
62
45
63
/// Add a magnet URL to the server
46
- case addTorrentMagnet( ClientConfig , URL , config : TorrentConfig )
64
+ case addTorrentMagnet( URL , TorrentConfig )
47
65
48
66
/// Get metadata about a magnet link
49
- case getMagnetInfo( ClientConfig , URL )
67
+ case getMagnetInfo( URL )
50
68
51
69
/// Add a torrent file to the server
52
- case addTorrentFile( ClientConfig , filename: String , data : Data , config : TorrentConfig )
70
+ case addTorrentFile( _ filename: String , Data , TorrentConfig )
53
71
54
72
/// Adds a torrent to the server from a URL
55
- case addTorrentURL( ClientConfig , URL , TorrentConfig )
73
+ case addTorrentURL( URL , TorrentConfig )
56
74
57
- case downloadTorrent( ClientConfig , from : URL )
75
+ case downloadTorrent( URL )
58
76
59
77
/// Upload a torrent to the server
60
78
///
61
79
/// **Note**: This does not add the torrent to the client
62
- case uploadTorrentFile( ClientConfig , Data )
80
+ case uploadTorrentFile( Data )
63
81
64
82
/// Get metadata about a torrent that was uploaded to the server
65
- case getUploadedTorrentInfo( ClientConfig , filename: String )
83
+ case getUploadedTorrentInfo( _ filename: String )
66
84
67
85
/// Get the default torrent configuration
68
- case getDefaultTorrentConfig( ClientConfig )
86
+ case getDefaultTorrentConfig
69
87
70
88
/// Set the options for a torrent
71
- case setTorrentOptions( ClientConfig , hash: String , JSON )
89
+ case setTorrentOptions( _ hash: String , _ params : JSON )
72
90
73
91
/// 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 )
75
95
76
96
/// Get all the hosts on the server
77
- case getHosts( ClientConfig )
97
+ case getHosts
78
98
79
99
/// Get the status of a host
80
- case getHostStatus( ClientConfig , Host )
100
+ case getHostStatus( Host )
81
101
82
102
/// Connect to a server
83
- case connect( ClientConfig , Host )
103
+ case connect( Host )
84
104
85
105
/// Get sessus status
86
- case getSessionStatus( ClientConfig )
87
-
106
+ case getSessionStatus
88
107
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
- }
102
108
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 }
156
113
}
157
114
158
115
// MARK: - Parameters
159
- private var parameters : Parameters ? {
116
+ fileprivate var parameters : Parameters ? {
160
117
161
118
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: [ [
206
151
" add_paused " ,
207
152
" compact_allocation " ,
208
153
" download_location " ,
@@ -214,18 +159,16 @@ enum DelugeRouter: URLRequestConvertible {
214
159
" max_upload_speed_per_torrent " ,
215
160
" prioritize_first_last_pieces "
216
161
] ] )
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: [ [
229
172
" has_incoming_connections " ,
230
173
" upload_rate " ,
231
174
" download_rate " ,
@@ -269,10 +212,11 @@ enum DelugeRouter: URLRequestConvertible {
269
212
private var id : UInt32 {
270
213
return arc4random ( )
271
214
}
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
+ ]
277
221
}
278
222
}
0 commit comments