appium_client/commands/
files.rs

1//! Files management
2use async_trait::async_trait;
3use base64::Engine;
4use base64::engine::general_purpose;
5use fantoccini::error::CmdError;
6use http::Method;
7use serde_json::json;
8use crate::{AndroidClient, AppiumClientTrait, IOSClient};
9use crate::commands::AppiumCommand;
10
11/// Download files and folders from the device (to your computer)
12#[async_trait]
13pub trait PullsFiles : AppiumClientTrait {
14
15    /// Pulls a single file from device
16    async fn pull_file(&self, path: &str) -> Result<Vec<u8>, CmdError> {
17        let value = self.issue_cmd(AppiumCommand::Custom(
18            Method::POST,
19            "appium/device/pull_file".to_string(),
20            Some(json!({
21                "path": path
22            }))
23        )).await?;
24
25        let value: String = serde_json::from_value(value)?;
26
27        Ok(general_purpose::STANDARD.decode(value)
28            .map_err(|e| CmdError::NotJson(format!("{e}")))?)
29    }
30
31    /// Pulls folder and returns zip file containing the content
32    async fn pull_folder(&self, path: &str) -> Result<Vec<u8>, CmdError> {
33        let value = self.issue_cmd(AppiumCommand::Custom(
34            Method::POST,
35            "appium/device/pull_folder".to_string(),
36            Some(json!({
37                "path": path
38            }))
39        )).await?;
40
41        let value: String = serde_json::from_value(value)?;
42
43        Ok(general_purpose::STANDARD.decode(value)
44            .map_err(|e| CmdError::NotJson(format!("{e}")))?)
45    }
46}
47
48#[async_trait]
49impl PullsFiles for AndroidClient {}
50
51#[async_trait]
52impl PullsFiles for IOSClient {}
53
54/// Upload files and folders onto the device
55#[async_trait]
56pub trait PushesFiles : AppiumClientTrait {
57    async fn push_file(&self, path: &str, data: &[u8]) -> Result<(), CmdError> {
58        let data = general_purpose::STANDARD.encode(data);
59
60        self.issue_cmd(AppiumCommand::Custom(
61            Method::POST,
62            "appium/device/push_file".to_string(),
63            Some(json!({
64                "path": path,
65                "data": data
66            }))
67        )).await?;
68
69        Ok(())
70    }
71}
72
73
74#[async_trait]
75impl PushesFiles for AndroidClient {}
76
77#[async_trait]
78impl PushesFiles for IOSClient {}