1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use async_trait::async_trait;

use serde::Serialize;

use super::api::Api;
use super::data::*;
use super::error::Error;
use super::utils::QueryConcat;

#[async_trait]
pub trait TorrentData<T> {
    async fn properties(&self, other: &'_ T) -> Result<TorrentProperties, Error>;
    async fn trackers(&self, other: &'_ T) -> Result<Vec<Tracker>, Error>;
    // TOOD: when impl'd on Api, self is 'a when it does not need to be
    // when impl'd on Torrent, other is &Api which also does not need to be 'a
    async fn contents<'a>(&'a self, other: &'a T) -> Result<Vec<TorrentInfo<'a>>, Error>;
}

// Resume a torrent
#[async_trait]
pub trait Resume<T> {
    async fn resume(&self, other: &'_ T) -> Result<(), Error>;
}
#[async_trait]
pub trait Pause<T> {
    async fn pause(&self, other: &'_ T) -> Result<(), Error>;
}

#[async_trait]
pub trait Tags<T, V: ?Sized> {
    async fn add_tag(&self, other: &'_ T, tags: &'_ V) -> Result<(), Error>;
}

#[async_trait]
impl TorrentData<Api> for Torrent {
    async fn properties(&self, api: &'_ Api) -> Result<TorrentProperties, Error> {
        // let _hash = &self.hash;
        // let addr = push_own! {api.address, "/api/v2/torrents/properties?hash=", _hash};

        // let res = api
        //     .client
        //     .get(&addr)
        //     .headers(api.make_headers()?)
        //     .send()
        //     .await?
        //     .bytes()
        //     .await?;

        // let props = serde_json::from_slice(&res)?;
        // Ok(props)
        self.hash.properties(&api).await
    }

    async fn trackers(&self, api: &'_ Api) -> Result<Vec<Tracker>, Error> {
        // let _hash = &self.hash;
        // let addr = push_own! {api.address, "/api/v2/torrents/trackers?hash=", _hash};

        // let res = api
        //     .client
        //     .get(&addr)
        //     .headers(api.make_headers()?)
        //     .send()
        //     .await?
        //     .bytes()
        //     .await?;

        // let trackers = serde_json::from_slice(&res)?;
        // Ok(trackers)

        self.hash.trackers(api).await
    }

    async fn contents<'a>(&'a self, api: &'a Api) -> Result<Vec<TorrentInfo<'a>>, Error> {
        self.hash.contents(api).await
        // let _hash = &self.hash;
        // let addr = push_own! {api.address, "/api/v2/torrents/files?hash=", _hash};

        // let res = api
        //     .client
        //     .get(&addr)
        //     .headers(api.make_headers()?)
        //     .send()
        //     .await?
        //     .bytes()
        //     .await?;

        // let info = serde_json::from_slice::<Vec<TorrentInfoSerde>>(&res)?
        //     .into_iter()
        //     .map(|x| x.into_info(&self.hash))
        //     .collect();

        // Ok(info)
    }
}

#[async_trait]
impl TorrentData<Api> for Hash {
    async fn properties(&self, api: &'_ Api) -> Result<TorrentProperties, Error> {
        let _hash = &self.hash;
        let addr = push_own! {api.address, "/api/v2/torrents/properties?hash=", self};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?
            .bytes()
            .await?;

        let props = serde_json::from_slice(&res)?;
        Ok(props)
    }

    async fn trackers(&self, api: &'_ Api) -> Result<Vec<Tracker>, Error> {
        let addr = push_own! {api.address, "/api/v2/torrents/trackers?hash=", self};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?
            .bytes()
            .await?;

        let trackers = serde_json::from_slice(&res)?;
        Ok(trackers)
    }

    async fn contents<'a>(&'a self, api: &'a Api) -> Result<Vec<TorrentInfo<'a>>, Error> {
        // let _hash = &self.hash;
        let addr = push_own! {api.address, "/api/v2/torrents/files?hash=", self};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?
            .bytes()
            .await?;

        let info = serde_json::from_slice::<Vec<TorrentInfoSerde>>(&res)?
            .into_iter()
            .map(|x| x.into_info(self))
            .collect();

        Ok(info)
    }
}

#[async_trait]
impl TorrentData<Torrent> for Api {
    async fn properties(&self, torrent: &'_ Torrent) -> Result<TorrentProperties, Error> {
        torrent.hash.properties(&self).await
    }

    async fn trackers(&self, torrent: &'_ Torrent) -> Result<Vec<Tracker>, Error> {
        torrent.hash.trackers(&self).await
    }

    async fn contents<'a>(&'a self, torrent: &'a Torrent) -> Result<Vec<TorrentInfo<'a>>, Error> {
        torrent.hash.contents(&self).await
    }
}

#[async_trait]
impl Resume<Api> for Torrent {
    async fn resume(&self, api: &'_ Api) -> Result<(), Error> {
        self.hash.resume(&api).await
    }
}

#[async_trait]
impl Resume<Api> for Hash {
    async fn resume(&self, api: &'_ Api) -> Result<(), Error> {
        let _hash = &self.hash;
        let addr = push_own! {api.address, "/api/v2/torrents/resume?hashes=", _hash};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?;

        match res.error_for_status() {
            Ok(_) => Ok(()),
            Err(e) => Err(Error::from(e)),
        }
    }
}

#[async_trait]
impl Resume<Api> for Vec<Hash> {
    async fn resume(&self, api: &'_ Api) -> Result<(), Error> {
        let hash_url = QueryConcat::query_concat(&self.as_slice(), '|');

        let addr = push_own! {api.address, "/api/v2/torrents/resume?hashes=", &hash_url};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?;

        match res.error_for_status() {
            Ok(_) => Ok(()),
            Err(e) => Err(Error::from(e)),
        }
    }
}

#[async_trait]
impl Pause<Api> for Torrent {
    async fn pause(&self, api: &'_ Api) -> Result<(), Error> {
        self.hash.pause(&api).await
    }
}

#[async_trait]
impl Pause<Api> for Hash {
    async fn pause(&self, api: &'_ Api) -> Result<(), Error> {
        let _hash = &self.hash;
        let addr = push_own! {api.address, "/api/v2/torrents/pause?hashes=", _hash};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?;

        match res.error_for_status() {
            Ok(_) => Ok(()),
            Err(e) => Err(Error::from(e)),
        }
    }
}

#[async_trait]
impl Pause<Api> for Vec<Hash> {
    async fn pause(&self, api: &'_ Api) -> Result<(), Error> {
        // concat all hashes together with "|" character separation
        let mut hash_url = self
            .iter()
            .map(|x| {
                let mut cln = x.hash.clone();
                cln.push_str("|");
                cln
            })
            .collect::<String>();

        // remove the final | from the string
        hash_url.remove(hash_url.len() - 1);

        let addr = push_own! {api.address, "/api/v2/torrents/pause?hashes=", &hash_url};

        let res = api
            .client
            .get(&addr)
            .headers(api.make_headers()?)
            .send()
            .await?;

        match res.error_for_status() {
            Ok(_) => Ok(()),
            Err(e) => Err(Error::from(e)),
        }
    }
}

#[derive(Serialize)]
struct TagsUrlHelper<'hash, 'tag> {
    hashes: &'hash [&'hash Hash],
    tags: &'tag [String],
}
impl<'hash, 'tag> TagsUrlHelper<'hash, 'tag> {
    fn url(self) -> Result<String, Error> {
        let mut url = String::with_capacity(25);
        let hashes = QueryConcat::query_concat(self.hashes, '|');
        let tags = QueryConcat::query_concat(&self.tags, ',');

        push_own!(prealloc; url, "hashes=", &hashes,"&tags=", &tags);

        Ok(url)
    }
}

#[async_trait]
impl Tags<Api, [String]> for Torrent {
    async fn add_tag(&self, api: &'_ Api, tags: &'_ [String]) -> Result<(), Error> {
        self.hash.add_tag(&api, tags).await
    }
}

#[async_trait]
impl Tags<Api, [String]> for Hash {
    async fn add_tag(&self, api: &'_ Api, tags: &'_ [String]) -> Result<(), Error> {
        let helper = TagsUrlHelper {
            hashes: &[self],
            tags,
        };
        let addr = push_own! {api.address, "/api/v2/torrents/addTags?", &helper.url()?};

        let res = api
            .client
            .post(&addr)
            .headers(api.make_headers()?)
            .send()
            .await;

        match res?.error_for_status() {
            Ok(_) => Ok(()),
            Err(e) => Err(Error::from(e)),
        }
    }
}

#[async_trait]
impl Tags<Api, [String]> for Vec<Hash> {
    async fn add_tag(&self, api: &'_ Api, tags: &'_ [String]) -> Result<(), Error> {
        // let addr = push_own! {api.address, "/api/v2/torrents/addTags"};

        // let _tags = QueryConcat::query_concat(&tags);
        // let form = serde_json::json! {
        //     {
        //         "hashes" : QueryConcat::query_concat(&self.as_slice()),
        //         "tags" : _tags
        //     }
        // };

        // dbg!{&form};

        // let res = api
        //     .client
        //     .post(&addr)
        //     .headers(api.make_headers()?)
        //     .form(&form)
        //     .send()
        //     .await?;

        // match res.error_for_status() {
        //     Ok(_) => Ok(()),
        //     Err(e) => Err(Error::from(e)),
        // }

        unimplemented!()
    }
}
#[async_trait]
impl Tags<Api, [String]> for Vec<Torrent> {
    async fn add_tag(&self, api: &'_ Api, tags: &'_ [String]) -> Result<(), Error> {
        // let addr = push_own! {api.address, "/api/v2/torrents/addTags"};

        // // let _tags = QueryConcat::query_concat(&tags);
        // // pull hashes from the torrents
        // let hashes = self
        //     .iter()
        //     .map(|x| x.hash.to_string())
        //     .collect::<Vec<String>>();

        // let form = serde_json::json! {
        //     {
        //         "hashes" : QueryConcat::query_concat(&hashes.as_slice()),
        //         "tags" : _tags
        //     }
        // };

        // let res = api
        //     .client
        //     .post(&addr)
        //     .headers(api.make_headers()?)
        //     .form(&form)
        //     .send()
        //     .await?;

        // match res.error_for_status() {
        //     Ok(_) => Ok(()),
        //     Err(e) => Err(Error::from(e)),
        // }

        unimplemented!()
    }
}