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
//! Files management
use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose;
use fantoccini::error::CmdError;
use http::Method;
use serde_json::json;
use crate::{AndroidClient, AppiumClientTrait, IOSClient};
use crate::commands::AppiumCommand;

/// Download files and folders from the device (to your computer)
#[async_trait]
pub trait PullsFiles : AppiumClientTrait {

    /// Pulls a single file from device
    async fn pull_file(&self, path: &str) -> Result<Vec<u8>, CmdError> {
        let value = self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/pull_file".to_string(),
            Some(json!({
                "path": path
            }))
        )).await?;

        let value: String = serde_json::from_value(value)?;

        Ok(general_purpose::STANDARD.decode(value)
            .map_err(|e| CmdError::NotJson(format!("{e}")))?)
    }

    /// Pulls folder and returns zip file containing the content
    async fn pull_folder(&self, path: &str) -> Result<Vec<u8>, CmdError> {
        let value = self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/pull_folder".to_string(),
            Some(json!({
                "path": path
            }))
        )).await?;

        let value: String = serde_json::from_value(value)?;

        Ok(general_purpose::STANDARD.decode(value)
            .map_err(|e| CmdError::NotJson(format!("{e}")))?)
    }
}

#[async_trait]
impl PullsFiles for AndroidClient {}

#[async_trait]
impl PullsFiles for IOSClient {}

/// Upload files and folders onto the device
#[async_trait]
pub trait PushesFiles : AppiumClientTrait {
    async fn push_file(&self, path: &str, data: &[u8]) -> Result<(), CmdError> {
        let data = general_purpose::STANDARD.encode(data);

        self.issue_cmd(AppiumCommand::Custom(
            Method::POST,
            "appium/device/push_file".to_string(),
            Some(json!({
                "path": path,
                "data": data
            }))
        )).await?;

        Ok(())
    }
}


#[async_trait]
impl PushesFiles for AndroidClient {}

#[async_trait]
impl PushesFiles for IOSClient {}