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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
use crate::error::ClientError;
use crate::{
    api::{self, Endpoint},
    types,
};
use log::debug;
use reqwest::Client;
use reqwest_cookie_store::{CookieStore, CookieStoreMutex};
use std::{error::Error, io::prelude::*, path::Path, sync::Arc};
use url::Url;

#[derive(Debug, Clone)]
pub struct Credential {
    pub username: String,
    pub password: String,
}

#[derive(Debug)]
pub struct QbitClient {
    pub host: Url,
    pub auth: Credential,
    pub client: Client,
    pub cookie_store: Arc<CookieStoreMutex>,
}

impl QbitClient {
    fn _try_new(host: String, username: String, password: String) -> Result<Self, ClientError> {
        let cookie_store = Arc::new(CookieStoreMutex::new(CookieStore::new(None)));
        let client = Client::builder()
            .cookie_provider(cookie_store.clone())
            .build()
            .map_err(|e| ClientError::Initialize(e.to_string()))?;
        Ok(Self {
            host: Url::parse(host.as_ref()).map_err(|e| ClientError::Initialize(e.to_string()))?,
            auth: Credential { username, password },
            client,
            cookie_store,
        })
    }
    pub fn new_with_user_pwd<U>(host: U, username: U, password: U) -> Result<Self, ClientError>
    where
        U: AsRef<str>,
    {
        Self::_try_new(
            host.as_ref().to_string(),
            username.as_ref().to_string(),
            password.as_ref().to_string(),
        )
    }

    pub fn new_from_env() -> Result<Self, ClientError> {
        use std::env::var;

        let (host, username, password) = (
            var("QBIT_HOST").map_err(|e| ClientError::Initialize(format!("`QBIT_HOST` {}", e)))?,
            var("QBIT_USERNAME")
                .map_err(|e| ClientError::Initialize(format!("`QBIT_USERNAME` {}", e)))?,
            var("QBIT_PASSWORD")
                .map_err(|e| ClientError::Initialize(format!("`QBIT_PASSWORD` {}", e)))?,
        );
        Self::_try_new(host, username, password)
    }

    pub async fn _resp<E>(&self, endpoint: &E) -> Result<E::Response, ClientError>
    where
        E: Endpoint,
    {
        let url = self.host.join(&endpoint.relative_path())?;
        let mut request = self.client.request(endpoint.method(), url);

        // build Headers
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert("Referer", self.host.to_string().parse()?);
        request = request.headers(headers);

        if let Some(query) = endpoint.query() {
            request = request.query(query);
        }
        if let Some(form) = endpoint.form() {
            request = request.form(form);
        }
        if let Some(multipart) = endpoint.multipart() {
            request = request.multipart(multipart);
        }
        debug!("request: {:?}", request);

        // send request
        let resp = request.send().await?;
        debug!("response: {:?}", resp);

        // check status code, return errors that defined in api
        if let Some(error) = endpoint.check_status(resp.status()) {
            return Err(error);
        }
        // deserialize response as string or type defined in api
        let de_resp = endpoint.de_response(resp).await?;
        Ok(de_resp)
    }

    pub async fn auth_login(&self) -> Result<String, ClientError> {
        let auth_form = types::AuthLoginForm {
            username: self.auth.username.clone(),
            password: self.auth.password.clone(),
        };
        let api_auth_login = api::AuthLogin { f: auth_form };

        {
            let mut store = self.cookie_store.lock().unwrap();
            store.clear();
        }

        let s = self._resp(&api_auth_login).await?;
        Ok(s)
    }

    pub async fn auth_logout(&self) -> Result<String, ClientError> {
        let api_auth_logout = api::AuthLogout {};
        let s = self._resp(&api_auth_logout).await?;
        Ok(s)
    }

    pub async fn app_version(&self) -> Result<String, ClientError> {
        let api_app_version = api::AppVersion {};
        let s = self._resp(&api_app_version).await?;
        Ok(s)
    }

    pub async fn app_webapi_version(&self) -> Result<String, ClientError> {
        let api_app_webapi_version = api::AppWebApiVersion {};
        let s = self._resp(&api_app_webapi_version).await?;
        Ok(s)
    }

    pub async fn app_build_info(&self) -> Result<types::AppBuildInfoResponse, ClientError> {
        let api_build_info = api::AppBuildInfo {};
        let de_resp = self._resp(&api_build_info).await?;
        Ok(de_resp)
    }

    pub async fn app_preferences(&self) -> Result<types::AppPreferences, ClientError> {
        let api_app_preferences = api::AppPreferences {};
        let de_resp = self._resp(&api_app_preferences).await?;
        Ok(de_resp)
    }

    pub async fn app_set_preferences(
        &self,
        f: types::AppSetPreferencesForm,
    ) -> Result<String, ClientError> {
        let api_set_preferences = api::AppSetPreferences { f };
        let s = self._resp(&api_set_preferences).await?;
        Ok(s)
    }

    pub async fn app_default_save_path(&self) -> Result<String, ClientError> {
        let api_default_save_path = api::AppDefaultSavePath {};
        let s = self._resp(&api_default_save_path).await?;
        Ok(s)
    }

    pub async fn log_main(
        &self,
        q: types::LogMainQuery,
    ) -> Result<Vec<types::LogMainResponseItem>, ClientError> {
        let api_logmain = api::LogMain { q };
        let de_resp = self._resp(&api_logmain).await?;
        Ok(de_resp.data)
    }

    pub async fn log_peers(
        &self,
        q: types::LogPeersQuery,
    ) -> Result<Vec<types::LogPeersResponseItem>, ClientError> {
        let api_logpeers = api::LogPeers { q };
        let de_resp = self._resp(&api_logpeers).await?;
        Ok(de_resp.data)
    }

    pub async fn sync_maindata(
        &self,
        q: types::SyncMaindataQuery,
    ) -> Result<types::SyncMaindataResponse, ClientError> {
        let api_maindata = api::Maindata { q };
        let de_resp = self._resp(&api_maindata).await?;
        Ok(de_resp)
    }

    pub async fn sync_torrent_peers(
        &self,
        q: types::SyncTorrentPeersQuery,
    ) -> Result<types::SyncTorrentPeersResponse, ClientError> {
        let api_torrent_peers = api::TorrentPeers { q };
        let de_resp = self._resp(&api_torrent_peers).await?;
        Ok(de_resp)
    }

    pub async fn transfer_info(&self) -> Result<types::TransferInfoResponse, ClientError> {
        let api_transfer_info = api::TransferInfo {};
        let de_resp = self._resp(&api_transfer_info).await?;
        Ok(de_resp)
    }

    pub async fn speed_limits_mode(&self) -> Result<types::SpeedLimitsModeResponse, ClientError> {
        let api_speed_limits_mode = api::SpeedLimitsMode {};
        let de_resp = self._resp(&api_speed_limits_mode).await?;
        Ok(de_resp)
    }

    pub async fn toggle_speed_limits_mode(&self) -> Result<String, ClientError> {
        let api_toggle_speed_limits_mode = api::ToggleSpeedLimitsMode {};
        let s = self._resp(&api_toggle_speed_limits_mode).await?;
        Ok(s)
    }

    pub async fn download_limit(&self) -> Result<String, ClientError> {
        let api_download_limit = api::DownloadLimit {};
        let s = self._resp(&api_download_limit).await?;
        Ok(s)
    }

    pub async fn set_download_limit(&self, limit: u64) -> Result<String, ClientError> {
        let api_set_download_limit = api::SetDownloadLimit {
            f: types::SetDownloadLimitForm { limit },
        };
        let s = self._resp(&api_set_download_limit).await?;
        Ok(s)
    }

    pub async fn upload_limit(&self) -> Result<String, ClientError> {
        let api_upload_limit = api::UploadLimit {};
        let s = self._resp(&api_upload_limit).await?;
        Ok(s)
    }

    pub async fn set_upload_limit(&self, limit: u64) -> Result<String, ClientError> {
        let api_set_upload_limit = api::SetUploadLimit {
            f: types::SetUploadLimitForm { limit },
        };
        let s = self._resp(&api_set_upload_limit).await?;
        Ok(s)
    }

    pub async fn ban_peers(&self, peers: Vec<String>) -> Result<String, ClientError> {
        let f = types::BanPeersForm { peers };
        let api_ban_peers = api::BanPeers { f };
        let s = self._resp(&api_ban_peers).await?;
        Ok(s)
    }

    pub async fn torrents_info(
        &self,
        q: types::TorrentsInfoQuery,
    ) -> Result<types::TorrentsInfoResponse, ClientError> {
        let api_torrents_info = api::TorrentsInfo { q };
        let de_resp = self._resp(&api_torrents_info).await?;
        Ok(de_resp)
    }

    pub async fn torrents_properties(
        &self,
        hash: String,
    ) -> Result<types::TorrentsPropertiesResponse, ClientError> {
        let q = types::TorrentsPropertiesQuery { hash };
        let api_torrents_properties = api::TorrentsProperties { q };
        let de_resp = self._resp(&api_torrents_properties).await?;
        Ok(de_resp)
    }

    pub async fn torrents_trackers(
        &self,
        hash: String,
    ) -> Result<types::TorrentsTrackersResponse, ClientError> {
        let q = types::TorrentsTrackersQuery { hash };
        let api_torrents_trackers = api::TorrentsTrackers { q };
        let de_resp = self._resp(&api_torrents_trackers).await?;
        Ok(de_resp)
    }

    pub async fn torrents_webseeds(
        &self,
        hash: String,
    ) -> Result<types::TorrentsWebseedsResponse, ClientError> {
        let q = types::TorrentsWebseedsQuery { hash };
        let api_torrents_webseeds = api::TorrentsWebseeds { q };
        let de_resp = self._resp(&api_torrents_webseeds).await?;
        Ok(de_resp)
    }

    pub async fn torrents_files(
        &self,
        hash: String,
    ) -> Result<types::TorrentsFilesResponse, ClientError> {
        let q = types::TorrentsFilesQuery {
            hash,
            ..Default::default()
        };
        let api_torrents_files = api::TorrentsFiles { q };
        let de_resp = self._resp(&api_torrents_files).await?;
        Ok(de_resp)
    }

    pub async fn torrents_piece_states(
        &self,
        hash: String,
    ) -> Result<types::TorrentsPieceStatesResponse, ClientError> {
        let q = types::TorrentsPieceStatesQuery { hash };
        let api_torrents_piece_states = api::TorrentsPieceStates { q };
        let de_resp = self._resp(&api_torrents_piece_states).await?;
        Ok(de_resp)
    }

    pub async fn torrents_piece_hashes(
        &self,
        hash: String,
    ) -> Result<types::TorrentsPieceHashesResponse, ClientError> {
        let q = types::TorrentsPieceHashesQuery { hash };
        let api_torrents_piece_hashes = api::TorrentsPieceHashes { q };
        let de_resp = self._resp(&api_torrents_piece_hashes).await?;
        Ok(de_resp)
    }

    pub async fn torrents_pause(&self, hashes: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsPauseForm { hashes };
        let api_torrents_pause = api::TorrentsPause { f };
        let s = self._resp(&api_torrents_pause).await?;
        Ok(s)
    }

    pub async fn torrents_add_by_url<U>(&self, urls: &[U]) -> Result<String, ClientError>
    where
        U: AsRef<str>,
    {
        let urls: Vec<String> = urls.iter().map(|u| u.as_ref().to_string()).collect();
        let ta = types::TorrentsAddMultipart {
            urls,
            torrents: vec![],
            ..Default::default()
        };
        let s = self.torrents_add(ta).await?;
        Ok(s)
    }

    pub async fn torrents_add_by_file<F>(&self, files: &[F]) -> Result<String, ClientError>
    where
        F: AsRef<Path>,
    {
        type VecOfNameAndContent = Vec<(String, Vec<u8>)>;
        let fc = |x: &F| -> Result<(String, Vec<u8>), Box<dyn Error>> {
            let mut f = std::fs::File::open(x.as_ref())?;
            let mut buffer = Vec::new();
            f.read_to_end(&mut buffer)?;
            Ok((
                x.as_ref()
                    .file_name()
                    .ok_or("no file name")?
                    .to_string_lossy()
                    .to_string(),
                buffer,
            ))
        };
        let files: Result<VecOfNameAndContent, Box<dyn Error>> = files.iter().map(fc).collect();
        let files = files.map_err(|_| ClientError::Other("".into()))?;
        let ta = types::TorrentsAddMultipart {
            urls: vec![],
            torrents: files,
            ..Default::default()
        };
        let s = self.torrents_add(ta).await?;
        Ok(s)
    }

    async fn torrents_add(&self, ta: types::TorrentsAddMultipart) -> Result<String, ClientError> {
        let api_torrents_add = api::TorrentsAdd { mp: ta };
        if api_torrents_add.multipart().is_none() {
            return Err(ClientError::InvalidMultipart("no valid multipart".into()));
        }
        let s = self._resp(&api_torrents_add).await?;
        Ok(s)
    }

    pub async fn torrents_add_trackers(
        &self,
        hash: String,
        urls: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsAddTrackersForm { hash, urls };
        let api_torrents_add_trackers = api::TorrentsAddTrackers { f };
        let s = self._resp(&api_torrents_add_trackers).await?;
        Ok(s)
    }

    pub async fn torrents_edit_tracker(
        &self,
        hash: String,
        orig_url: String,
        new_url: String,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsEditTrackerForm {
            hash,
            orig_url,
            new_url,
        };
        let api_torrents_edit_tracker = api::TorrentsEditTracker { f };
        let s = self._resp(&api_torrents_edit_tracker).await?;
        Ok(s)
    }

    pub async fn torrents_remove_trackers(
        &self,
        hash: String,
        urls: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsRemoveTrackersForm { hash, urls };
        let api_torrents_remove_trackers = api::TorrentsRemoveTrackers { f };
        let s = self._resp(&api_torrents_remove_trackers).await?;
        Ok(s)
    }

    pub async fn torrents_add_peers(
        &self,
        hashes: Vec<String>,
        peers: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsAddPeersForm { hashes, peers };
        let api_torrents_add_peers = api::TorrentsAddPeers { f };
        let s = self._resp(&api_torrents_add_peers).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_increase_prio(&self, hashes: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsIncreasePrioForm { hashes };
        let api_torrents_increase_prio = api::TorrentsIncreasePrio { f };
        let s = self._resp(&api_torrents_increase_prio).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_decrease_prio(&self, hashes: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsDecreasePrioForm { hashes };
        let api_torrents_decrease_prio = api::TorrentsDecreasePrio { f };
        let s = self._resp(&api_torrents_decrease_prio).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_top_prio(&self, hashes: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsTopPrioForm { hashes };
        let api_torrents_top_prio = api::TorrentsTopPrio { f };
        let s = self._resp(&api_torrents_top_prio).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_bottom_prio(&self, hashes: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsBottomPrioForm { hashes };
        let api_torrents_bottom_prio = api::TorrentsBottomPrio { f };
        let s = self._resp(&api_torrents_bottom_prio).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_download_limit(
        &self,
        hashes: Vec<String>,
    ) -> Result<types::TorrentsDownloadLimitResponse, ClientError> {
        let f = types::TorrentsDownloadLimitForm { hashes };
        let api_torrents_download_limit = api::TorrentsDownloadLimit { f };
        let de_resp = self._resp(&api_torrents_download_limit).await.unwrap();
        Ok(de_resp)
    }

    pub async fn torrents_set_download_limit(
        &self,
        hashes: Vec<String>,
        limit: u64,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetDownloadLimitForm { hashes, limit };
        let api_torrents_set_download_limit = api::TorrentsSetDownloadLimit { f };
        let s = self._resp(&api_torrents_set_download_limit).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_set_share_limits(
        &self,
        hashes: Vec<String>,
        ratio_limit: types::RatioLimit,
        seeding_time_limit: i64,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetShareLimitsForm {
            hashes,
            ratio_limit,
            seeding_time_limit,
        };
        let api_torrents_set_share_limits = api::TorrentsSetShareLimits { f };
        let s = self._resp(&api_torrents_set_share_limits).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_upload_limit(
        &self,
        hashes: Vec<String>,
    ) -> Result<types::TorrentsUploadLimitResponse, ClientError> {
        let f = types::TorrentsUploadLimitForm { hashes };
        let api_torrents_upload_limit = api::TorrentsUploadLimit { f };
        let de_resp = self._resp(&api_torrents_upload_limit).await.unwrap();
        Ok(de_resp)
    }

    pub async fn torrents_set_upload_limit(
        &self,
        hashes: Vec<String>,
        limit: u64,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetUploadLimitForm { hashes, limit };
        let api_torrents_set_upload_limit = api::TorrentsSetUploadLimit { f };
        let s = self._resp(&api_torrents_set_upload_limit).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_set_location<T>(
        &self,
        hashes: Vec<String>,
        location: T,
    ) -> Result<String, ClientError>
    where
        T: AsRef<Path>,
    {
        let f = types::TorrentsSetLocationForm {
            hashes,
            location: location.as_ref().to_string_lossy().to_string(),
        };
        let api_torrents_set_location = api::TorrentsSetLocation { f };
        let s = self._resp(&api_torrents_set_location).await.unwrap();
        Ok(s)
    }

    pub async fn torernts_rename(&self, hash: String, name: String) -> Result<String, ClientError> {
        let f = types::TorrentsRenameForm { hash, name };
        let api_torrents_rename = api::TorrentsRename { f };
        let s = self._resp(&api_torrents_rename).await.unwrap();
        Ok(s)
    }

    pub async fn torernts_set_category(
        &self,
        hashes: Vec<String>,
        category: String,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetCategoryForm { hashes, category };
        let api_torrents_set_category = api::TorrentsSetCategory { f };
        let s = self._resp(&api_torrents_set_category).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_categories(
        &self,
    ) -> Result<types::TorrentsCategoriesResponse, ClientError> {
        let api_torrents_categories = api::TorrentsCategories {};
        let de_resp = self._resp(&api_torrents_categories).await.unwrap();
        Ok(de_resp)
    }

    pub async fn torrents_create_category<T>(
        &self,
        category: String,
        save_path: T,
    ) -> Result<String, ClientError>
    where
        T: AsRef<Path>,
    {
        let f = types::TorrentsCreateCategoryForm {
            category,
            save_path: save_path.as_ref().to_string_lossy().to_string(),
        };
        let api_torrents_create_category = api::TorrentsCreateCategory { f };
        let s = self._resp(&api_torrents_create_category).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_edit_category<T>(
        &self,
        category: String,
        save_path: T,
    ) -> Result<String, ClientError>
    where
        T: AsRef<Path>,
    {
        let f = types::TorrentsEditCategoryForm {
            category,
            save_path: save_path.as_ref().to_string_lossy().to_string(),
        };
        let api_torrents_edit_category = api::TorrentsEditCategory { f };
        let s = self._resp(&api_torrents_edit_category).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_remove_categories(
        &self,
        categories: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsRemoveCategoriesForm { categories };
        let api_torrents_remove_categories = api::TorrentsRemoveCategories { f };
        let s = self._resp(&api_torrents_remove_categories).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_add_tags(
        &self,
        hashes: Vec<String>,
        tags: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsAddTagsForm { hashes, tags };
        let api_torrents_add_tags = api::TorrentsAddTags { f };
        let s = self._resp(&api_torrents_add_tags).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_remove_tags(
        &self,
        hashes: Vec<String>,
        tags: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsRemoveTagsForm { hashes, tags };
        let api_torrents_remove_tags = api::TorrentsRemoveTags { f };
        let s = self._resp(&api_torrents_remove_tags).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_tags(&self) -> Result<types::TorrentsTagsResponse, ClientError> {
        let api_torrents_tags = api::TorrentsTags {};
        let de_resp = self._resp(&api_torrents_tags).await.unwrap();
        Ok(de_resp)
    }

    pub async fn torrens_create_tags(&self, tags: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsCreateTagsForm { tags };
        let api_torrents_create_tags = api::TorrentsCreateTags { f };
        let s = self._resp(&api_torrents_create_tags).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_delete_tags(&self, tags: Vec<String>) -> Result<String, ClientError> {
        let f = types::TorrentsDeleteTagsForm { tags };
        let api_torrents_delete_tags = api::TorrentsDeleteTags { f };
        let s = self._resp(&api_torrents_delete_tags).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_set_auto_management(
        &self,
        hashes: Vec<String>,
        enable: bool,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetAutoManagementForm { hashes, enable };
        let api_torrents_set_automanagement = api::TorrentsSetAutoManagement { f };
        let s = self._resp(&api_torrents_set_automanagement).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_toggle_sequential_download(
        &self,
        hashes: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsToggleSequentialDownloadForm { hashes };
        let api_torrents_toggle_sequential_download = api::TorrentsToggleSequentialDownload { f };
        let s = self
            ._resp(&api_torrents_toggle_sequential_download)
            .await
            .unwrap();
        Ok(s)
    }

    pub async fn torrents_toggle_first_last_piece_prio(
        &self,
        hashes: Vec<String>,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsToggleFirstLastPiecePrioForm { hashes };
        let api_torrents_toggle_first_last_piece_prio = api::TorrentsToggleFirstLastPiecePrio { f };
        let s = self
            ._resp(&api_torrents_toggle_first_last_piece_prio)
            .await
            .unwrap();
        Ok(s)
    }

    pub async fn torrents_set_force_start(
        &self,
        hashes: Vec<String>,
        value: bool,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetForceStartForm { hashes, value };
        let api_torrents_set_force_start = api::TorrentsSetForceStart { f };
        let s = self._resp(&api_torrents_set_force_start).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_set_super_seeding(
        &self,
        hashes: Vec<String>,
        value: bool,
    ) -> Result<String, ClientError> {
        let f = types::TorrentsSetSuperSeedingForm { hashes, value };
        let api_torrents_set_super_seeding = api::TorrentsSetSuperSeeding { f };
        let s = self._resp(&api_torrents_set_super_seeding).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_rename_file<T>(
        &self,
        hash: String,
        old_path: T,
        new_path: T,
    ) -> Result<String, ClientError>
    where
        T: AsRef<Path>,
    {
        let f = types::TorrentsRenameFileForm {
            hash,
            old_path: old_path.as_ref().to_string_lossy().to_string(),
            new_path: new_path.as_ref().to_string_lossy().to_string(),
        };
        let api_torrents_rename_file = api::TorrentsRenameFile { f };
        let s = self._resp(&api_torrents_rename_file).await.unwrap();
        Ok(s)
    }

    pub async fn torrents_rename_folder<T>(
        &self,
        hash: String,
        old_path: T,
        new_path: T,
    ) -> Result<String, ClientError>
    where
        T: AsRef<Path>,
    {
        let f = types::TorrentsRenameFolderForm {
            hash,
            old_path: old_path.as_ref().to_string_lossy().to_string(),
            new_path: new_path.as_ref().to_string_lossy().to_string(),
        };
        let api_torrents_rename_folder = api::TorrentsRenameFolder { f };
        let s = self._resp(&api_torrents_rename_folder).await.unwrap();
        Ok(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::sync::OnceCell;

    async fn login() -> QbitClient {
        std::env::set_var("RUST_LOG", "debug");
        env_logger::init();
        let qbit = QbitClient::new_with_user_pwd("http://192.168.0.11:8080", "admin", "adminadmin")
            .unwrap();
        qbit.auth_login().await.unwrap();
        qbit
    }

    static LOGIN: OnceCell<QbitClient> = OnceCell::const_new();

    #[tokio::test]
    pub async fn test_version() {
        let client = LOGIN.get_or_init(login).await;
        let v = client.app_version().await.unwrap();
        debug!("version: {}", v);
    }

    #[tokio::test]
    pub async fn test_webapi_version() {
        let client = LOGIN.get_or_init(login).await;
        let v = client.app_webapi_version().await.unwrap();
        debug!("webapi_version: {}", v);
    }

    #[tokio::test]
    pub async fn test_build_info() {
        let client = LOGIN.get_or_init(login).await;
        let buildinfo = client.app_build_info().await.unwrap();
        debug!("buildinfo: {:?}", buildinfo);
    }
    #[tokio::test]
    pub async fn test_preferences() {
        let client = LOGIN.get_or_init(login).await;
        let p = client.app_preferences().await.unwrap();
        debug!("preferences: {:?}", p);
    }
}