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
//! # butlerd
//! Interfaces with itch.io's [butlerd](https://github.com/itchio/butler).
//! It provides methods for almost every API call that can be made.
//! Right now, replying to events from butler isn't implemented yet. This means that you can't log
//! in using an username/password if a captcha gets triggered or if a 2factor token is required.
//! Currently working on fixing this, but I'm still not very certain as to how this should be
//! implemented. Open to suggestions.
extern crate reqwest;
use std::process::Command;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate hyper;
extern crate rand;
extern crate regex;
extern crate serde;
use regex::Regex;
use reqwest::Method;
use serde::de::DeserializeOwned;
use std::env;
use std::io::Read;
use std::result::Result::*;
pub mod Responses;
use hyper::Client;
use serde_json::value::Map;
use std::collections::HashMap;
use std::fs;
use Responses::*;
#[cfg(target_os = "macos")]
static DB_PATH: &str = "";
#[cfg(target_os = "linux")]
static DB_PATH: &str = "~/.config/itch/db/butler.db";
#[cfg(target_os = "windows")]
static DB_PATH: &str = "";
#[cfg(target_os = "macos")]
static LOG_PATH_PRE: &str = "";
#[cfg(target_os = "linux")]
static LOG_PATH_PRE: &str = "/tmp/butlerdrs";
#[cfg(target_os = "windows")]
static LOG_PATH_PRE: &str = "";
#[cfg(target_os = "macos")]
static PRE_PATH: &str = "";
#[cfg(target_os = "linux")]
static PRE_PATH: &str = "~/.config/itch/prereqs";
#[cfg(target_os = "windows")]
static PRE_PATH: &str = "";
const POST: Method = Method::POST;
/// Represents a connection to a butlerd instance
pub struct Butler {
    secret: String,
    address: String,
    client: reqwest::Client,
    pre_dir: String,
    client_launch: reqwest::Client,
    hclient: Client<hyper::client::HttpConnector, hyper::Body>,
}
impl Butler {
    /// Initializes a new butlerd instance. It will close when your program does.
    pub fn new() -> Result<Butler, String> {
        let log_path = &(LOG_PATH_PRE.to_string() + &rand::random::<f64>().to_string() + ".log");
        let mut file: fs::File;
        if fs::remove_file(log_path).is_ok() {
            file = fs::File::create(log_path).unwrap();
        } else {

        }
        if fs::metadata(log_path).is_ok() {
            if fs::remove_file(log_path).is_err() {
                file = fs::File::create(log_path).unwrap();
            } else {
                file = fs::File::open(log_path).unwrap();
            }
        } else {
            file = fs::File::create(log_path).unwrap();
        }
        Command::new("sh")
            .arg("-c")
            .arg(
                "butler daemon --json --dbpath=".to_string() + &DB_PATH.replace("~", &get_home()) +
                    " --destiny-pid=" + &::std::process::id().to_string(),
            )
            .stdout(file)
            .spawn()
            .expect("Couldn't start butler daemon");
        //TODO: REPLACE
        let mut finish = false;
        let mut bd: String = String::new();
        let reg = Regex::new(r"\{(?:.|\s)+\}").unwrap();
        while !finish {
            bd = String::new();
            fs::File::open(log_path)
                .unwrap()
                .read_to_string(&mut bd)
                .unwrap();
            let res = reg.find(&bd);
            if res.is_some() {
                finish = true;
            } else {
                ::std::thread::sleep_ms(250);
            }
        }
        bd = reg.find(&bd).unwrap().as_str().to_string();
        bd = bd.replace("\\\"", "");
        let mut lines = bd.lines();
        let mut done = false;
        let mut pmeta = BStart {
            http: Map::new(),
            https: Map::new(),
            secret: String::new(),
        };
        while !done {
            let ltry = lines.next();
            if ltry.is_some() {
                let try = ltry.unwrap().to_string();
                let td = serde_json::from_str(&try.trim());
                if td.is_ok() {
                    pmeta = td.unwrap();
                    done = true;
                }
            } else {
                fs::remove_file(log_path).expect("Couldn't remove log file early");
                return Err("Couldn't get butler startup".to_string());
                // break;
            }
        }
        if done {
            let secret = pmeta.secret.to_string();
            let mut headers = reqwest::header::HeaderMap::new();
            headers.insert("X-Secret", secret.parse().unwrap());
            headers.insert("X-ID", "0".parse().unwrap());
            let mut client = reqwest::Client::builder();
            let mut client_launch = reqwest::Client::builder();
            client_launch = client_launch.default_headers(headers.clone());
            client = client.default_headers(headers);
            client_launch = client_launch.timeout(None);
            let built = client.build().unwrap();
            let builtl = client_launch.build().unwrap();
            fs::remove_file(log_path).expect("Couldn't remove log file");
            Ok(Butler {
                secret: secret,
                address: pmeta.http[&"address".to_string()].to_string().replace(
                    "\"",
                    "",
                ),
                client: built,
                pre_dir: PRE_PATH.to_string().replace("~", &get_home()),
                client_launch: builtl,
                hclient: Client::new(),
            })
        } else {
            Err("Couldn't start butler".to_string())
        }
    }
    ///Shuts down butler daemon.
    pub fn close(&self) {
        self.request( "/Meta.Shutdown", "{}".to_string())
            .expect("Couldn't shut down butler daemon");;
    }
    fn make_request(
        &self,
        method: Method,
        path: &str,
        params: String,
        client: String,
    ) -> Result<String, String> {
        let res: Result<reqwest::Response, reqwest::Error>;
        let url = "http://".to_string() + &self.address.clone() + path;
        if &client == "launch" {
            res = self.client_launch.request(method, &url).body(params).send();
        } else {
            res = self.client.request(method, &url).body(params).send();
        }
        if res.is_ok() {
            let mut res = res.unwrap();
            if res.status().is_success() {
                Ok(res.text().unwrap())
            } else {
                Err("No".to_string())
            }
        } else {
            Err("Timed out".to_string())
        }
    }
    fn request(&self, path: &str, params: String) -> Result<String, String> {
        self.make_request(POST, path, params, "default".to_string())
    }
    fn request_l(&self, path: &str, params: String) -> Result<String, String> {
        self.make_request(POST, path, params, "launch".to_string())
    }
    /// Fetchs all installed caves
    pub fn fetchall(&self) -> Result<Vec<Cave>, BError> {
        self.res_preq("/call/Fetch.Caves", vec![], "items")

    }
    ///Fetches specific game by id
    pub fn fetch_game(&self, game_id: i32) -> Result<Game, BError> {
        let gvs = self.request(
            "/call/Fetch.Game",
            json!({
                "gameId": game_id
            }).to_string(),
        ).expect("Couldn't fetch game by id");
        parse_r(gvs, "game")
    }
    ///Fetches specific cave by id
    pub fn fetch_cave(&self, cave_id: &str) -> Result<Cave, BError> {
        self.res_preq("/call/Fetch.Cave", vec![("caveId", cave_id)], "cave")
    }
    /// Makes a cave 'pinned' or not depending on pinned
    pub fn pin_cave(&self, cave_id: &str, pinned: bool) {
        self.request(
            "/call/Caves.SetPinned",
            json!({
            "caveId":cave_id,
            "pinned":pinned
        }).to_string(),
        ).expect("Couldn't pin cave");
    }
    /// Launches game by CaveID. Note that this will not complete until the game is closed.
    pub fn launch_game(&self, cave_id: &str) {
        self.request_l(
            "/call/Launch",
            json!({
                "caveId": cave_id,
                "prereqsDir": self.pre_dir.clone()
            }).to_string(),
        ).expect("Couldn't launch game");
    }
    /// Lists saved profiles
    pub fn profile_list(&self) -> Result<Vec<Profile>, BError> {
        self.res_preq("/call/Profile.List", vec![], "profiles")
    }
    /// Sets a specific profile info value
    pub fn profile_put(&self, profile_id: i32, key: &str, value: &str) {
        self.request(
            "/call/Profile.Data.Put",
            json!({
                "profileId":profile_id,
                "key":key,
                "value":value
            }).to_string(),
        ).expect("Couldn't put profile data");
    }
    /// Searches for folders possible to clean
    pub fn clean_search(
        &self,
        roots: Vec<String>,
        whitelist: Vec<String>,
    ) -> Result<Vec<CleanDownloadsEntry>, BError> {
        let cis = self.request(
            "/call/CleanDownloads.Search",
            json!({
            "roots":roots,
            "whitelist":whitelist
        }).to_string(),
        ).expect("Couldn't search for folders to clean");
        parse_r(cis, "entries")
    }
    /// Cleans specified CleanDownloadsEntries
    pub fn clean_apply(&self, entries: Vec<CleanDownloadsEntry>) {
        self.request(
            "/call/CleanDownloads.Apply",
            json!({
            "entries":entries
        }).to_string(),
        ).expect("Couldn't apply downloads clean");
    }
    /// Gets a specific profile info value
    pub fn profile_get(&self, profile_id: i32, key: &str) -> Result<String, BError> {
        let gis = self.request(
            "/call/Profile.Data.Get",
            json!({
            "profileId":profile_id,
            "key":key
        }).to_string(),
        ).expect("Couldn't get profile data value");
        parse_r(gis, "value")
    }
    /// Removes a profile's saved info. Also removes it from profile_list. Returns true if
    /// successful.
    pub fn profile_forget(&self, profile_id: i32) -> Result<bool, BError> {
        let sis = self.request(
            "/call/Profile.Forget",
            json!({ "profileId": profile_id }).to_string(),
        ).expect("Couldn't forget profile");
        parse_r(sis, "success")
    }
    /// Disables updates for a cave
    pub fn snooze_cave(&self, cave_id: &str) {
        self.request(
            "/call/Snooze.Cave",
            json!({
            "caveId":cave_id
        }).to_string(),
        ).expect("Couldn't snooze cave");
    }
    /// Logs into a profile using saved credentials
    pub fn login_saved(&self, profile_id: i32) -> Result<Profile, BError> {
        let pis = self.request(
            "/call/Profile.UseSavedLogin",
            json!({ "profileId": profile_id }).to_string(),
        ).expect("Couldn't login using saved credentials");
        parse_r(pis, "profile")
    }
    /// Given an API key, logs into a profile and returns profile.
    pub fn login_api_key(&self, api_key: &str) -> Result<Profile, BError> {
        self.res_preq(
            "/call/Profile.LoginWithAPIKey",
            vec![("apiKey", api_key)],
            "profile",
        )
    }
    /// Given an username and password, logs into a profile and returns profile and cookie. May
    /// fail if a captcha or 2factor is required. Working on fix.
    pub fn login_password(&self, username: &str, password: &str) -> Result<PassLogRes, BError> {
        self.res_req(
            "/call/Profile.LoginWithPassword",
            vec![("username", username), ("password", password)],
        ).unwrap()
    }
    /// Fetches all common/cached items and returns summaries
    pub fn fetch_commons(&self) -> Result<Commons, BError> {
        self.res_req("/call/Fetch.Commons", vec![])
    }
    /// Fetches a vec of games owned by a specific profile id
    pub fn fetch_profile_games(&self, profile_id: i32) -> Result<Vec<ProfileGame>, BError> {
        let pvs = self.request(
            "/call/Fetch.ProfileGames",
            json!({
                "profileId": profile_id,
            }).to_string(),
        ).expect("Couldn't fetch profile games");
        parse_r(pvs, "items")
    }
    /// Fetches download key
    pub fn fetch_download_key(
        &self,
        profile_id: i32,
        download_key_id: i32,
        fresh: bool,
    ) -> Result<DownloadKey, BError> {
        let dis = self.request(
            "/call/Fetch.DownloadKey",
            json!({
            "profileId": profile_id,
            "downloadKeyId": download_key_id,
            "fresh":fresh
        }).to_string(),
        ).expect("Couldn't fetch download key");
        parse_r(dis, "downloadKey")
    }
    /// Fetches collection info. Does not include games
    pub fn fetch_collection(
        &self,
        profile_id: i32,
        collection_id: i32,
        fresh: bool,
    ) -> Result<Collection, BError> {
        let cis = self.request(
            "/call/Fetch.Collection",
            json!({
            "profileId":profile_id,
            "collectionId":collection_id,
            "fresh":fresh
        }).to_string(),
        ).expect("Couldn't fetch collection");
        parse_r(cis, "collection")
    }
    /// Fetches all collections for a profile. Does not include games
    pub fn fetch_profile_collections(
        &self,
        profile_id: i32,
        fresh: bool,
    ) -> Result<Vec<Collection>, BError> {
        let cis = self.request(
            "/call/Fetch.Collection",
            json!({
            "profileId": profile_id,
            "fresh": fresh
        }).to_string(),
        ).expect("Couldn't fetch profile collections");
        parse_r(cis, "items")
    }
    /// Fetches games in a collection
    pub fn fetch_collection_games(
        &self,
        profile_id: i32,
        collection_id: i32,
        fresh: bool,
    ) -> Result<Vec<CollectionGame>, BError> {
        let cis = self.request(
            "/call/Fetch.Collection.Games",
            json!({
            "profileId": profile_id,
            "collectionId": collection_id,
            "fresh":fresh
        }).to_string(),
        ).expect("Couldn't fetch collection games");
        parse_r(cis, "items")
    }
    /// Fetches owned download keys for a profile. Pass fresh as true to force butler to refresh
    /// cache
    pub fn fetch_profile_keys(&self, profile_id: i32, fresh: bool) -> Result<Vec<DownloadKey>, BError> {
        let dis = self.request(
            "/call/Fetch.ProfileOwnedKeys",
            json!({
            "profileId": profile_id,
            "fresh":fresh
        }).to_string(),
        ).expect("Couldn't fetch profile keys");
        parse_r(dis, "items")
      }
    /// Marks all local data as 'stale' and outdated
    pub fn expireall(&self) {
        self.req_h("Fetch.ExpireAll");
    }
    /// Searches users
    pub fn search_users(&self, profile_id: i32, query: &str) -> Result<Vec<User>, BError> {
        let uis = self.request(
            "/call/Search.Users",
            json!({
            "profileId":profile_id,
            "query":query
        }).to_string(),
        ).expect("Couldn't search users");
        parse_r(uis, "users")
    }
    fn req_h(&self, path: &str) {
        let uri = "http://".to_string() + &self.address + "/call/" + path;
        let mut builder = hyper::Request::builder();
        builder.method("POST");
        builder.header("X-Secret", self.secret.as_str());
        builder.header("X-ID", "0");
        builder.uri(uri);
        let request = builder.body(hyper::Body::empty()).unwrap();
        let _res = self.hclient.request(request);
    }
    /// Sets a throttle for how much bandwith butler can use. If enabled is false, disables any
    /// previous set throttles. Rate is measured in kbps
    pub fn set_throttle(&self, enabled: bool, rate: i64) {
        self.request(
            "/call/Network.SetBandwidthThrottle",
            json!({
            "enabled":enabled,
            "rate":rate
        }).to_string(),
        ).expect("Couldn't set throttle");
    }
    /// Fetches the best available sale for a game(if such a sale exists)
    pub fn fetch_sale(&self, game_id: i32) -> Result<Sale, BError> {
        let sls = self.request(
            "/call/Fetch.Sale",
            json!({
                "gameId": game_id,
            }).to_string(),
        ).expect("Couldn't fetch sale");
        parse_r(sls, "sale")
    }
    /// Gets all configured butler install locations in a vec
    pub fn get_install_locations(&self) -> Result<Vec<InstallLocationSummary>, BError> {
        self.res_preq("/call/Install.Locations.List", vec![], "installLocations")
    }
    /// Gets info on a filesystem
    pub fn statfs(&self, path: &str) -> Result<FsInfo, BError> {
        self.res_req("/call/System.StatFS", vec![("path", path)])
    }
    /// Checks if an update is available for a vec of Caves. If you pass an empty vec, all caves
    /// will be checked.
    pub fn check_update(&self, cave_ids: Vec<String>) -> Result<CheckUpdate, BError> {
        let cuis = self.request(
            "/call/CheckUpdate",
            json!({
            "caveIds":cave_ids,
            "verbose":false
        }).to_string(),
        ).expect("Couldn't check updates");
        pres(cuis)
    }
    /// Cancels an install. Needs an install id. Result is true if cancel succeeded
    pub fn install_cancel(&self, id: &str) -> Result<bool, BError> {
        self.res_preq("/call/Install.Cancel", vec![("id", id)], "didCancel")
    }
    /// Queues up a game installation
    pub fn install_queue(
        &self,
        game: Game,
        install_location_id: &str,
        upload: Upload,
        reason: DownloadReason,
    ) -> Result<QueueResponse, BError> {
        let req = InstallQueueReq {
            install_location_id: install_location_id.to_string(),
            reason: dr_str(reason),
            game: game,
            upload: upload,
        };
        let rstr = serde_json::to_string(&req).unwrap();
        let qis = self.request( "/call/Install.Queue", rstr).expect(
            "Couldn't queue game for download",
        );
        pres(qis)
    }
    /// Performs an Install. Download must be completed beforehand
    pub fn install_perform(&self, queue_id: &str, staging_folder: &str) {
        self.request(
            "/call/Install.Perform",
            json!({
                "id":queue_id,
                "stagingFolder": staging_folder}).to_string(),
        ).expect("Couldn't perform install");
    }
    /// Fetches all uploads for a game
    pub fn fetch_uploads(&self, game_id: i32, compatible: bool) -> Result<Vec<Upload>, BError> {
        let uis = self.request(
            "/call/Fetch.GameUploads",
            json!({
                "gameId": game_id,
                "compatible": compatible,
                "fresh": true
            }).to_string(),
        ).expect("Couldn't fetch game uploads");
        parse_r(uis, "uploads")
    }
    /// Queues a download to later be downloaded by downloads_drive
    pub fn download_queue(&self, i_queue: QueueResponse) {
        self.request(
            "/call/Downloads.Queue",
            json!({
                "item": serde_json::to_string(&i_queue).unwrap()
            }).to_string(),
        ).expect("Couldn't queue download");
    }
    /// Downloads all games in the queue. Completes when they are all done
    pub fn downloads_drive(&self) {
        let uri = "http://".to_string() + &self.address + "/call/Downloads.Drive";
        let mut builder = hyper::Request::builder();
        builder.method("POST");
        builder.header("X-Secret", self.secret.as_str());
        builder.header("X-ID", "0");
        builder.uri(uri);
        let request = builder.body(hyper::Body::empty()).unwrap();
        self.hclient.request(request);
        let mut done = false;
        while !done {
            ::std::thread::sleep_ms(1000);
            self.clear_completed();
            let ds = self.downloads_list();
            if ds.is_err() {
                done = true;
            }
        }
    }
    /// Cancels driving downloads. Returns bool indicating success.
    pub fn cancel_download_drive(&self) -> Result<bool, BError> {
        self.res_preq("/call/Downloads.Drive.Cancel", vec![], "didCancel")
    }
    /// Forces butler's online/offline state. True is offline, False is online
    pub fn set_offline(&self, online: bool) {
        self.request(
            "/call/Network.SetSimulateOffline",
            json!({
            "enabled":online
        }).to_string(),
        ).expect("Couldn't change butler's network status");
    }
    /// Discards one download
    pub fn discard_download(&self, download_id: &str) {
        self.request(
            "/call/Downloads.Discard",
            json!({ "downloadId": download_id }).to_string(),
        ).expect("Couldn't discard download");
    }
    /// Prioritizes by download id
    pub fn prioritize_download(&self, download_id: &str) {
        self.request(
            "/call/Downloads.Prioritize",
            json!({ "downloadId": download_id }).to_string(),
        ).expect("Couldn't prioritize download");
    }
    /// Retries an errored download id
    pub fn download_retry(&self, download_id: &str) {
        self.request(
            "/call/Downloads.Retry",
            json!({ "downloadId": download_id }).to_string(),
        ).expect("Couldn't retry download");
    }
    /// Gets butler version strings
    pub fn get_version(&self) -> Result<VersionInfo, BError> {
        self.res_req("/call/Version.Get", vec![])
    }
    /// Clears all completed downloads from the queue
    pub fn clear_completed(&self) {
        self.request( "/call/Downloads.ClearFinished", "{}".to_string())
            .expect("Couldn't clear completed donwloads");
    }
    /// A helper function that performs all of the game installation/download steps for you.
    /// Recommended over doing installation yourself.
    pub fn install_game(&self, game: Game, install_location_id: &str, upload: Upload) {
        let inf = self.install_queue(game, install_location_id, upload, DownloadReason::Install)
            .unwrap();
        let id = inf.id.clone();
        let stf = inf.staging_folder.clone();
        self.download_queue(inf);
        self.downloads_drive();
        self.install_perform(&id, &stf);
    }
    /// Fetches a vec of Downloads from the queue, returning a BError if none are available
    pub fn downloads_list(&self) -> Result<Vec<Download>, BError> {
        self.res_preq("/call/Downloads.List", vec![], "downloads")
    }
    /// Searches games for string. Requires profileid.
    pub fn search_games(&self, profile_id: i32, query: &str) -> Result<Vec<Game>, BError> {
        let gis = self.request(
            "/call/Seach.Games",
            json!({
            "profileId":profile_id,
            "query":query
        }).to_string(),
        ).unwrap();
        parse_r(gis, "games")
    }
    /// Adds a new install location
    pub fn install_location_add(&self, path: &str) {
        self.request(
            "/call/Install.Locations.Add",
            json!({ "path": path }).to_string(),
        ).expect("Couldn't add new install location");
    }
    /// Removes an install location
    pub fn install_location_remove(&self, id: &str) {
        self.request(
            "/call/Install.Locations.Remove",
            json!({ "id": id }).to_string(),
        ).expect("Couldn't remove install location");
    }
    /// Gets an install location from a previously fetched id
    pub fn install_location_get_by_id(&self, id: &str) -> Result<InstallLocationSummary, BError> {
        self.res_req("/call/Install.Locations.GetByID", vec![("id", id)])
    }
    /// Uninstalls a cave
    pub fn uninstall(&self, cave_id: &str) {
        self.request(
            "/call/Uninstall.Perform",
            json!({ "caveId": cave_id }).to_string(),
        ).expect("Couldn't uninstall cave");
    }
    fn res_req<T>(&self, url: &str, body: Vec<(&str, &str)>) -> Result<T, BError>
    where
        T: DeserializeOwned,
    {
        let mut b = String::new();
        if body.len() < 1 {
            b = "{}".to_string();
        } else {
            b = serde_json::to_string(&mp(body)).unwrap();
        }
        let ris = self.request( url, b).unwrap();
        let res = pres(ris);
        res
    }
    fn res_preq<T>(&self, url: &str, body: Vec<(&str, &str)>, field: &str) -> Result<T, BError>
    where
        T: DeserializeOwned,
    {
        let mut b = String::new();
        if body.len() < 1 {
            b = "{}".to_string();
        } else {
            b = serde_json::to_string(&mp(body)).unwrap();
        }
        let ris = self.request(url, b).unwrap();
        let res = parse_r(ris, field);
        res
    }
}
fn get_home() -> String {
    return String::from(env::home_dir().unwrap().to_str().unwrap());
}
/// Translates a DownloadReason into a string to be used by the butler API
fn dr_str(r: DownloadReason) -> String {
    match r {
        DownloadReason::Install => "install",
        DownloadReason::Reinstall => "reinstall",
        DownloadReason::Update => "update",
        DownloadReason::VersionSwitch => "version-switch",
    }.to_string()
}
/// A helper function to interpet a common result response from butler. Took far too long to write.
fn pres<T>(st: String) -> Result<T, BError>
where
    T: DeserializeOwned,
{
    let res: Result<ResponseRes, serde_json::Error> = serde_json::from_str(&st);
    if res.is_ok() {
        return Ok(
            serde_json::from_str(&serde_json::to_string(&res.unwrap().result).unwrap()).unwrap(),
        );
    } else {
        let err: Result<ResponseErr, serde_json::Error> = serde_json::from_str(&st);
        if err.is_ok() {
            return Err(err.unwrap().error);
        } else {
            return Err(BError {
                message: st,
                code: -1,
            });
        }
    }
}
fn parse_r<T>(st: String, prop: &str) -> Result<T, BError>
where
    T: DeserializeOwned,
{
    let response: Result<ResponseRes, serde_json::Error> = serde_json::from_str(&st);
    if response.is_ok() {
        let result = response.unwrap().result;
        if result.contains_key(prop) {
            let finish = serde_json::from_str(&serde_json::to_string(&result[prop]).unwrap())
                .unwrap();
            return Ok(finish);
        } else {
            return Err(BError {
                message: "Not Found".to_string(),
                code: -2,
            });
        }
    } else {
        let err: Result<ResponseErr, serde_json::Error> = serde_json::from_str(&st);
        if err.is_ok() {
            return Err(err.unwrap().error);
        } else {
            return Err(BError {
                message: st,
                code: -1,
            });
        }
    }

}
/// A helper function to create a map easily for use with res_req
fn mp(data: Vec<(&str, &str)>) -> HashMap<String, String> {
    data.into_iter()
        .map(|x| (x.0.to_string(), x.1.to_string()))
        .collect::<HashMap<String, _>>()
}