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
use crate::{
    add_backslash_if_necessary, has_valid_path_segments, http_response_to_grid,
    new_auth_token,
};
use crate::{ClientSeed, Grid};
use serde_json::json;
use thiserror::Error;
use url::Url;

/// A standalone function to call the eval API on a SkySpark server, without
/// creating a `SkySparkClient`.
///
/// # Example
/// ```rust,no_run
/// # async fn run() {
/// use raystack::ClientSeed;
/// use raystack::eval::eval;
/// let client_seed = ClientSeed::new(30).unwrap();
/// let url = "http://test.com/api/bigProject/";
/// let output = eval(&client_seed, url, "name", "p4ssw0rd", "readAll(site)", None).await.unwrap();
/// let grid = output.into_grid();
/// // Use the grid here
/// # }
/// ```
pub async fn eval(
    client_seed: &ClientSeed,
    project_api_url: &str,
    username: &str,
    password: &str,
    axon_expr: &str,
    auth_token: Option<&str>,
) -> Result<EvalOutput, EvalError> {
    let project_api_url = Url::parse(project_api_url)?;
    let project_api_url = add_backslash_if_necessary(project_api_url);

    if project_api_url.cannot_be_a_base() {
        let url_err_msg = "the project API URL must be a valid base URL";
        return Err(EvalError::UrlFormat(url_err_msg.to_owned()));
    }

    if !has_valid_path_segments(&project_api_url) {
        let url_err_msg = "URL must be formatted similarly to http://www.test.com/api/project/";
        return Err(EvalError::UrlFormat(url_err_msg.to_owned()));
    }

    let eval_url = project_api_url
        .join("eval")
        .expect("since url ends with '/' this should never fail");

    let mut was_new_token_obtained = false;

    let auth_token = match auth_token {
        Some(token) => token.to_owned(),
        None => {
            was_new_token_obtained = true;
            new_auth_token(&project_api_url, &client_seed, username, password)
                .await?
        }
    };

    let row = json!({ "expr": axon_expr });
    let req_grid = Grid::new_internal(vec![row]);

    let req_with_token = |token: &str| {
        client_seed
            .client()
            .post(eval_url.clone())
            .header("Accept", "application/json")
            .header("Authorization", format!("BEARER authToken={}", token))
            .header("Content-Type", "application/json")
            .body(req_grid.to_json_string())
    };

    let res = req_with_token(&auth_token).send().await?;

    if res.status() == reqwest::StatusCode::FORBIDDEN {
        let auth_token =
            new_auth_token(&project_api_url, &client_seed, username, password)
                .await?;
        let retry_res = req_with_token(&auth_token).send().await?;
        let grid: Result<Grid, EvalError> = http_response_to_grid(retry_res)
            .await
            .map_err(|err| err.into());
        Ok(EvalOutput::new(grid?, Some(auth_token)))
    } else {
        let grid: Result<Grid, EvalError> =
            http_response_to_grid(res).await.map_err(|err| err.into());
        if was_new_token_obtained {
            Ok(EvalOutput::new(grid?, Some(auth_token)))
        } else {
            Ok(EvalOutput::new(grid?, None))
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
/// The resulting output of a call to the SkySpark eval API.
pub struct EvalOutput {
    /// The grid returned from the server.
    grid: Grid,
    /// If a new auth token was obtained while executing the eval function,
    /// this will contain that auth token.
    new_auth_token: Option<String>,
}

impl EvalOutput {
    fn new(grid: Grid, new_auth_token: Option<String>) -> Self {
        Self {
            grid,
            new_auth_token,
        }
    }

    pub fn grid(&self) -> &Grid {
        &self.grid
    }

    pub fn into_grid(self) -> Grid {
        self.grid
    }

    /// Return true only if a new auth token was obtained.
    pub fn has_new_auth_token(&self) -> bool {
        self.new_auth_token.is_some()
    }

    /// If a new auth token was obtained, return that token.
    pub fn new_auth_token(&self) -> Option<&str> {
        match &self.new_auth_token {
            Some(token) => Some(token),
            None => None,
        }
    }
}

/// Errors that can occur when executing an eval API call on
/// a SkySpark server.
#[derive(Debug, Error)]
pub enum EvalError {
    #[error("Could not parse a URL: {0}")]
    UrlParse(#[from] url::ParseError),
    #[error("URL is not formatted for the SkySpark API: {0}")]
    UrlFormat(String),
    #[error("Authentication error: {0}")]
    Auth(#[from] crate::auth::AuthError),
    /// The grid contained error information from the server.
    #[error("Server returned an error grid")]
    Grid {
        /// The grid which caused this error.
        err_grid: Grid,
    },
    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),
    /// An error related to parsing a `Grid` from a JSON value.
    #[error("Could not parse JSON as a Haystack grid")]
    ParseJsonGrid(#[from] crate::grid::ParseJsonGridError),
    /// An error caused by an invalid time zone.
    #[error("Not a valid time zone: {err_time_zone}")]
    TimeZone {
        /// The time zone which caused this error.
        err_time_zone: String,
    },
}

impl std::convert::From<crate::Error> for EvalError {
    fn from(error: crate::Error) -> Self {
        match error {
            crate::Error::Grid { err_grid } => Self::Grid { err_grid },
            crate::Error::Http { err } => Self::Http(err),
            crate::Error::ParseJsonGrid(err) => Self::ParseJsonGrid(err),
            crate::Error::TimeZone { err_time_zone } => {
                Self::TimeZone { err_time_zone }
            }
            crate::Error::UpdateAuthToken(_) => unreachable!(), // The standalone eval function will not update auth tokens.
        }
    }
}

impl EvalError {
    /// Return true if this error encapsulates a Haystack error grid.
    pub fn is_grid(&self) -> bool {
        matches!(self, Self::Grid { .. })
    }

    /// Return a reference to the Haystack error grid encapsulated by this
    /// error, if this error was caused by a Haystack error grid.
    pub fn grid(&self) -> Option<&Grid> {
        match self {
            Self::Grid { err_grid } => Some(err_grid),
            _ => None,
        }
    }

    /// Return the Haystack error grid encapsulated by this error, if this
    /// error was caused by a Haystack error grid.
    pub fn into_grid(self) -> Option<Grid> {
        match self {
            Self::Grid { err_grid } => Some(err_grid),
            _ => None,
        }
    }
}

#[cfg(test)]
mod test {
    use super::eval;
    use super::{EvalError, EvalOutput};
    use crate::{ClientSeed, ValueExt};

    fn project_api_url() -> String {
        std::env::var("RAYSTACK_SKYSPARK_PROJECT_API_URL").unwrap()
    }

    fn username() -> String {
        std::env::var("RAYSTACK_SKYSPARK_USERNAME").unwrap()
    }

    fn password() -> String {
        std::env::var("RAYSTACK_SKYSPARK_PASSWORD").unwrap()
    }

    async fn eval_expr(
        axon_expr: &str,
        token: Option<&str>,
    ) -> Result<EvalOutput, EvalError> {
        let seed = ClientSeed::new(15).unwrap();

        eval(
            &seed,
            &project_api_url(),
            &username(),
            &password(),
            axon_expr,
            token,
        )
        .await
    }

    #[tokio::test]
    async fn eval_works_with_no_token() {
        let output = eval_expr("readAll(site)", None).await.unwrap();
        assert!(output.has_new_auth_token());
        let grid = output.into_grid();
        assert!(grid.size() > 1);
        assert!(grid.rows()[0]["site"].is_hs_marker());
    }

    #[tokio::test]
    async fn eval_works_with_bad_token() {
        let output = eval_expr("readAll(site)", Some("thistokenisnotvalid"))
            .await
            .unwrap();
        assert!(output.has_new_auth_token());
        let grid = output.into_grid();
        assert!(grid.size() > 1);
        assert!(grid.rows()[0]["site"].is_hs_marker());
    }

    #[tokio::test]
    async fn eval_works_with_good_token() {
        let output_for_token = eval_expr("readAll(site)", None).await.unwrap();
        let valid_token = output_for_token.new_auth_token().unwrap();
        let output =
            eval_expr("readAll(site)", Some(valid_token)).await.unwrap();

        // We used a valid token, so there should be no new token:
        assert_eq!(output.has_new_auth_token(), false);

        let grid = output.into_grid();
        assert!(grid.size() > 1);
        assert!(grid.rows()[0]["site"].is_hs_marker());
    }
}