fm_script_client/
data_api.rs

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
use crate::{Connection, Error, FileMakerError, ScriptClient};
use async_trait::async_trait;
use reqwest::{Client, Response};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use url::Url;

/// Context for Data API script execution.
///
/// Scripts within the Data API always have to be executed together with another request. While it
/// is possible to execute a script standalone through a `GET` request, it is highly advised to not
/// do that since it is both length restricted and exposing any sent data in server logs.
///
/// The script context defines a `find` on a given layout which will be used as primary request, on
/// which the actual script execution will be added on top. It is important that this `find`
/// succeeds, otherwise a FileMaker error will be thrown.
///
/// Ideally you just create simple layout with a single field and a single record.
pub struct ScriptLayoutContext {
    layout: String,
    search_field: String,
    search_value: String,
}

impl ScriptLayoutContext {
    /// Creates a new script layout context.
    ///
    /// # Examples
    ///
    /// ```
    /// use fm_script_client::data_api::ScriptLayoutContext;
    ///
    /// let context = ScriptLayoutContext::new(
    ///     "script_layout",
    ///     "id",
    ///     "1",
    /// );
    /// ```
    pub fn new(layout: &str, search_field: &str, search_value: &str) -> Self {
        Self {
            layout: layout.to_string(),
            search_field: search_field.to_string(),
            search_value: search_value.to_string(),
        }
    }
}

/// Data API script client.
///
/// The Data API script client should only be used if the OData API is not available or cannot be
/// used for other reasons. Otherwise, you should use the
/// [`crate::odata_api::ODataApiScriptClient`].
///
/// When using the Data API client, you must specify a [`ScriptLayoutContext`] in order to execute
/// script calls. See its documentation for further details.
pub struct DataApiScriptClient {
    connection: Arc<Connection>,
    context: Arc<ScriptLayoutContext>,
    client: Client,
    token: Mutex<Option<Token>>,
}

impl DataApiScriptClient {
    /// Creates a new Data API script client.
    ///
    /// # Examples
    ///
    /// ```
    /// use fm_script_client::Connection;
    /// use fm_script_client::data_api::{DataApiScriptClient, ScriptLayoutContext};
    ///
    /// let client = DataApiScriptClient::new(
    ///     "https://foo:bar@example.com/example_database".try_into().unwrap(),
    ///     ScriptLayoutContext::new("script_layout", "id", "1"),
    /// );
    /// ```
    pub fn new(connection: Connection, context: ScriptLayoutContext) -> Self {
        Self {
            connection: Arc::new(connection),
            context: Arc::new(context),
            client: Client::new(),
            token: Mutex::new(None),
        }
    }

    /// Releases the currently used token.
    ///
    /// If the client has no token registered at the moment, it will return immediately. Otherwise,
    /// it will issue a `DELETE` against the FileMaker Data API and forget the token.
    pub async fn release_token(&self) -> Result<(), Error> {
        let token = match self.token.lock().await.take() {
            Some(token) => token,
            None => return Ok(()),
        };

        let url = self.create_url(&format!("/sessions/{}", token.token))?;
        self.client.delete(url).send().await?;

        Ok(())
    }

    async fn get_token(&self) -> Result<String, Error> {
        let mut token = self.token.lock().await;
        let now = Instant::now();

        if let Some(ref mut token) = *token {
            token.expiry = now + Duration::from_secs(60 * 14);

            if token.expiry < now {
                return Ok(token.token.clone());
            }
        }

        let url = self.create_url("/sessions")?;
        let response = self
            .client
            .post(url)
            .basic_auth(&self.connection.username, Some(&self.connection.password))
            .send()
            .await?;

        if response.status().is_success() {
            let access_token = match response.headers().get("X-FM-Data-Access-Token") {
                Some(token) => match token.to_str() {
                    Ok(token) => token.to_string(),
                    Err(_) => return Err(Error::MissingAccessToken),
                },
                None => return Err(Error::MissingAccessToken),
            };

            *token = Some(Token {
                token: access_token.clone(),
                expiry: now + Duration::from_secs(60 * 14),
            });

            return Ok(access_token);
        }

        Err(self.error_from_response(response).await)
    }

    async fn error_from_response(&self, response: Response) -> Error {
        let status = response.status();

        match response.json::<ErrorResponseBody>().await {
            Ok(result) => {
                if let Some(error) = result.messages.into_iter().next() {
                    Error::FileMaker(error)
                } else {
                    Error::UnknownResponse(status)
                }
            }
            Err(_) => Error::UnknownResponse(status),
        }
    }

    fn create_url(&self, path: &str) -> Result<Url, Error> {
        let mut url = Url::parse(&format!(
            "{}://{}/fmi/data/v1/databases/{}{}",
            if self.connection.disable_tls {
                "http"
            } else {
                "https"
            },
            self.connection.hostname,
            self.connection.database,
            path
        ))?;

        if let Some(port) = self.connection.port {
            let _ = url.set_port(Some(port));
        }

        Ok(url)
    }
}

#[derive(Debug)]
struct Token {
    token: String,
    expiry: Instant,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct RequestBody<T> {
    query: HashMap<String, String>,
    limit: u8,
    script: String,
    #[serde(skip_serializing_if = "Option::is_none", rename = "script.param")]
    script_param: Option<T>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ResponseBody {
    script_result: String,
    script_error: String,
}

#[derive(Debug, Deserialize)]
struct ErrorResponseBody {
    messages: Vec<FileMakerError>,
}

#[async_trait]
impl ScriptClient for DataApiScriptClient {
    async fn execute<T: DeserializeOwned, P: Serialize + Send + Sync>(
        &self,
        script_name: impl Into<String> + Send,
        parameter: Option<P>,
    ) -> Result<T, Error> {
        let token = self.get_token().await?;
        let url = self.create_url(&format!("/layouts/{}/_find", self.context.layout))?;

        let mut query = HashMap::new();
        query.insert(
            self.context.search_field.clone(),
            self.context.search_value.clone(),
        );

        let body = RequestBody {
            query,
            limit: 1,
            script: script_name.into(),
            script_param: Some(serde_json::to_string(&parameter)?),
        };

        let response = self
            .client
            .post(url)
            .header("Authorization", format!("Bearer {}", &token))
            .header("Content-Type", "application/json")
            .header("Accept", "application/json")
            .json(&body)
            .send()
            .await?;

        let status = response.status();

        if status.is_success() {
            let result: ResponseBody = response.json().await?;

            if result.script_error != "0" {
                return Err(Error::ScriptFailure {
                    code: result.script_error.parse().unwrap_or(-1),
                    data: result.script_result,
                });
            }

            let result: T = serde_json::from_str(&result.script_result)?;
            return Ok(result);
        }

        Err(self.error_from_response(response).await)
    }
}