xapi-rs 0.1.22

A conformant LRS implementation of xAPI 2.0.0
Documentation
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
// SPDX-License-Identifier: GPL-3.0-or-later

#![doc = include_str!("../../../doc/EXT_VERBS.md")]

use crate::{
    DataError, MyError, MyLanguageTag, Validate, Verb, config,
    db::{
        Aggregates,
        schema::TVerb,
        verb::{
            ext_compute_aggregates, ext_find_by_iri, ext_find_by_rid, ext_find_some, ext_update,
            insert_verb,
        },
    },
    eval_preconditions,
    lrs::{DB, Headers, User, etag_from_str, no_content, resources::WithETag},
};
use core::fmt;
use iri_string::types::IriStr;
use rocket::{
    Request, Responder, State,
    form::FromForm,
    get,
    http::{Header, Status, hyper::header},
    patch, post, put,
    request::{FromRequest, Outcome},
    routes,
};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use std::str::FromStr;
use tracing::{debug, error, info, warn};

const DEFAULT_START_RID: i32 = 0;
const DEFAULT_COUNT: i32 = 50;
const DEFAULT_ASC: bool = true;

#[derive(Debug, Serialize)]
pub(crate) struct VerbExt {
    pub(crate) rid: i32,
    pub(crate) verb: Verb,
}

/// Simplified Verb representation.
#[derive(Debug, Deserialize, Serialize)]
pub struct VerbUI {
    pub(crate) rid: i32,
    pub(crate) iri: String,
    pub(crate) display: String,
}

impl VerbUI {
    pub(crate) fn from(v: TVerb, language: &MyLanguageTag) -> Self {
        VerbUI {
            rid: v.id,
            iri: v.iri,
            display: match v.display {
                Some(x) => String::from(x.0.get(language).unwrap_or("")),
                None => String::from(""),
            },
        }
    }

    /// Return the row identifier.
    pub fn rid(&self) -> i32 {
        self.rid
    }

    /// Return the IRI as a `&str`.
    pub fn iri_as_str(&self) -> &str {
        &self.iri
    }

    /// Return the `display` as a `&str`.
    pub fn display(&self) -> &str {
        &self.display
    }
}

/// Rocket Responder that returns an OK HTTP Status w/ a JSON string of either
/// a _Verb_ or a _VerbExt_ along with ETag, and Content-Type HTTP headers.
#[derive(Responder)]
#[response(status = 200, content_type = "json")]
struct ETaggedResource {
    inner: String,
    etag: Header<'static>,
}

pub(crate) struct QueryParams<'a> {
    pub(crate) language: &'a str,
    pub(crate) start: i32,
    pub(crate) count: i32,
    pub(crate) asc: bool,
}

/// A structure grouping the GET multi request parameters.
#[rocket::async_trait]
impl<'r> FromRequest<'r> for QueryParams<'r> {
    type Error = MyError;

    async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
        let language = match qp::<&str>(req, "language", &config().default_language) {
            Ok(x) => x,
            Err(x) => return Outcome::Error((Status::BadRequest, x)),
        };
        // ensure `language` is a valid language tag...
        match MyLanguageTag::from_str(language) {
            Ok(_) => (),
            Err(x) => {
                error!("This ({}) is NOT a valid language tag: {}", language, x);
                return Outcome::Error((Status::BadRequest, MyError::Data(x)));
            }
        }

        let start = match qp::<i32>(req, "start", DEFAULT_START_RID) {
            Ok(x) => x,
            Err(x) => return Outcome::Error((Status::BadRequest, x)),
        };
        // must be >= 0
        if start < 0 {
            let msg = format!("Start ({start}) MUST be greater than or equal to 0");
            error!("Failed: {}", msg);
            return Outcome::Error((Status::BadRequest, MyError::Runtime(msg.into())));
        }

        let count = match qp::<i32>(req, "count", DEFAULT_COUNT) {
            Ok(x) => x,
            Err(x) => return Outcome::Error((Status::BadRequest, x)),
        };
        // must be in [10..=100]
        if !(10..=100).contains(&count) {
            let msg = format!("Count ({count}) MUST be w/in [10..101]");
            error!("Failed: {}", msg);
            return Outcome::Error((Status::BadRequest, MyError::Runtime(msg.into())));
        }

        let asc = match qp::<bool>(req, "asc", DEFAULT_ASC) {
            Ok(x) => x,
            Err(x) => return Outcome::Error((Status::BadRequest, x)),
        };

        Outcome::Success(QueryParams {
            language,
            start,
            count,
            asc,
        })
    }
}

/// Generic function to assign a value to an expected query parameter. If the
/// named parameter is missing, a provided default value will be used instead.
fn qp<'r, T: FromForm<'r>>(
    req: &'r Request<'_>,
    name: &str,
    default_value: T,
) -> Result<T, MyError> {
    match req.query_value::<T>(name) {
        Some(Ok(x)) => Ok(x),
        Some(Err(x)) => {
            let msg = format!("Failed parsing query parameter '{name}': {x}");
            error!("{}", msg);
            Err(MyError::Runtime(msg.into()))
        }
        None => {
            info!("Missing query parameter '{}'. Use default value", name);
            Ok(default_value)
        }
    }
}

impl fmt::Display for QueryParams<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "[language={}, start={}, count={}, asc? {}]",
            self.language, self.start, self.count, self.asc
        )
    }
}

#[doc(hidden)]
pub fn routes() -> Vec<rocket::Route> {
    routes![
        post,
        put,
        put_rid,
        patch,
        patch_rid,
        get_iri,
        get_rid,
        get_aggregates,
        get_some
    ]
}

fn parse_verb(s: &str) -> Result<Verb, MyError> {
    if s.is_empty() {
        return Err(MyError::HTTP {
            status: Status::BadRequest,
            info: "Body must NOT be empty".into(),
        });
    }

    let v = serde_json::from_str::<Verb>(s)
        .map_err(|x| MyError::Data(DataError::JSON(x)).with_status(Status::BadRequest))?;
    debug!("v = {}", v);
    // a valid Verb may have a None or empty `display` language map.  this is
    // not acceptable here...
    if v.is_valid() {
        match v.display_as_map() {
            Some(x) => {
                if x.is_empty() {
                    Err(MyError::HTTP {
                        status: Status::BadRequest,
                        info: "Verb's 'display' language map MUST not be empty".into(),
                    })
                } else {
                    Ok(v)
                }
            }
            None => Err(MyError::HTTP {
                status: Status::BadRequest,
                info: "Verb's 'display' language map MUST not be null".into(),
            }),
        }
    } else {
        Err(MyError::HTTP {
            status: Status::BadRequest,
            info: "Verb is invalid".into(),
        })
    }
}

/// Create new Verb resource.
#[post("/", data = "<body>")]
async fn post(body: &str, db: &State<DB>, user: User) -> Result<WithETag, MyError> {
    debug!("----- post ----- {}", user);
    user.can_use_verbs()?;

    let new_verb = parse_verb(body)?;
    let conn = db.pool();
    let rid = insert_verb(conn, &new_verb)
        .await
        .map_err(|x| x.with_status(Status::BadRequest))?;
    info!("Created Verb at #{}", rid);
    let etag = etag_from_str(body);
    Ok(WithETag {
        inner: Status::Ok,
        etag: Header::new(header::ETAG.as_str(), etag.to_string()),
    })
}

/// Update existing Verb replacing its `display` field.
#[put("/", data = "<body>")]
async fn put(c: Headers, body: &str, db: &State<DB>, user: User) -> Result<WithETag, MyError> {
    debug!("----- put ----- {}", user);
    user.can_use_verbs()?;

    let new_verb = parse_verb(body)?;
    let conn = db.pool();
    // must already exist...
    let x = ext_find_by_iri(conn, new_verb.id_as_str())
        .await
        .map_err(|x| x.with_status(Status::NotFound))?;
    update_it(c, conn, x.rid, x.verb, new_verb).await
}

#[put("/<rid>", data = "<body>")]
async fn put_rid(
    c: Headers,
    rid: i32,
    body: &str,
    db: &State<DB>,
    user: User,
) -> Result<WithETag, MyError> {
    debug!("----- put_rid ----- {}", user);
    user.can_use_verbs()?;

    let new_verb = parse_verb(body)?;
    let conn = db.pool();
    // must already exist...
    let old_verb = ext_find_by_rid(conn, rid)
        .await
        .map_err(|x| x.with_status(Status::NotFound))?;
    update_it(c, conn, rid, old_verb, new_verb).await
}

async fn update_it(
    c: Headers,
    conn: &PgPool,
    rid: i32,
    old_verb: Verb,
    new_verb: Verb,
) -> Result<WithETag, MyError> {
    // only update if pre-conditions exist + pass...
    if c.has_no_conditionals() {
        Err(MyError::HTTP {
            status: Status::Conflict,
            info: "Update existing Verb w/ no pre-conditions is NOT allowed".into(),
        })
    } else {
        debug!("old_verb = {}", old_verb);
        let x = serde_json::to_string(&old_verb).map_err(|x| MyError::Data(DataError::JSON(x)))?;
        let etag = etag_from_str(&x);
        debug!("etag (old) = {}", etag);
        match eval_preconditions!(&etag, c) {
            s if s != Status::Ok => Err(MyError::HTTP {
                status: s,
                info: "Failed pre-condition(s)".into(),
            }),
            _ => {
                if new_verb == old_verb {
                    info!("Old + new Verbs are identical. Pass");
                    Ok(no_content(&etag))
                } else {
                    ext_update(conn, rid, &new_verb).await?;
                    // etag changed.  recompute...
                    let x = serde_json::to_string(&new_verb)
                        .map_err(|x| MyError::Data(DataError::JSON(x)))?;
                    let etag = etag_from_str(&x);
                    debug!("etag (new) = {}", etag);
                    Ok(no_content(&etag))
                }
            }
        }
    }
}

/// Update existing Verb merging `display` fields.
#[patch("/", data = "<body>")]
async fn patch(c: Headers, body: &str, db: &State<DB>, user: User) -> Result<WithETag, MyError> {
    debug!("----- patch ----- {}", user);
    user.can_use_verbs()?;

    let new_verb = parse_verb(body)?;
    let conn = db.pool();
    // must already exist...
    let x = ext_find_by_iri(conn, new_verb.id_as_str())
        .await
        .map_err(|x| x.with_status(Status::NotFound))?;
    patch_it(c, conn, x.rid, x.verb, new_verb).await
}

/// Update existing Verb merging `display` fields.
#[patch("/<rid>", data = "<body>")]
async fn patch_rid(
    c: Headers,
    rid: i32,
    body: &str,
    db: &State<DB>,
    user: User,
) -> Result<WithETag, MyError> {
    debug!("----- patch_rid ----- {}", user);
    user.can_use_verbs()?;

    let new_verb = parse_verb(body)?;
    let conn = db.pool();
    // must already exist...
    let old_verb = ext_find_by_rid(conn, rid)
        .await
        .map_err(|x| x.with_status(Status::NotFound))?;
    patch_it(c, conn, rid, old_verb, new_verb).await
}

async fn patch_it(
    c: Headers,
    conn: &PgPool,
    rid: i32,
    mut old_verb: Verb,
    new_verb: Verb,
) -> Result<WithETag, MyError> {
    // proceed if pre-conditions exist + pass...
    if c.has_no_conditionals() {
        Err(MyError::HTTP {
            status: Status::Conflict,
            info: "Patching existing Verb w/ no pre-conditions is NOT allowed".into(),
        })
    } else {
        debug!("old_verb = {}", old_verb);
        let x = serde_json::to_string(&old_verb).map_err(|x| MyError::Data(DataError::JSON(x)))?;
        let etag = etag_from_str(&x);
        debug!("etag (old) = {}", etag);
        match eval_preconditions!(&etag, c) {
            s if s != Status::Ok => Err(MyError::HTTP {
                status: s,
                info: "Failed pre-condition(s)".into(),
            }),
            _ => {
                if new_verb == old_verb {
                    info!("Old + new Verbs are identical. Pass");
                    Ok(no_content(&etag))
                } else if !old_verb.extend(new_verb) {
                    info!("Old + merged versions are identical. Pass");
                    Ok(no_content(&etag))
                } else {
                    debug!("patched_verb = {}", old_verb);
                    ext_update(conn, rid, &old_verb).await?;
                    let x = serde_json::to_string(&old_verb)
                        .map_err(|x| MyError::Data(DataError::JSON(x)))?;
                    let etag = etag_from_str(&x);
                    debug!("etag (new) = {}", etag);
                    Ok(no_content(&etag))
                }
            }
        }
    }
}

#[get("/?<iri>")]
async fn get_iri(iri: &str, db: &State<DB>, user: User) -> Result<ETaggedResource, MyError> {
    debug!("----- get_iri ----- {}", user);
    user.can_use_verbs()?;

    let iri = if IriStr::new(iri).is_err() {
        warn!(
            "This <{}> is not a valid IRI. Assume it's an alias + continue",
            iri
        );
        let iri2 = format!("http://adlnet.gov/expapi/verbs/{iri}");
        // is it valid now?
        if IriStr::new(&iri2).is_err() {
            return Err(MyError::HTTP {
                status: Status::BadRequest,
                info: format!("Input <{iri}> is not a valid IRI nor an alias of one").into(),
            });
        } else {
            iri2
        }
    } else {
        iri.to_owned()
    };

    let x = ext_find_by_iri(db.pool(), &iri)
        .await
        .map_err(|x| x.with_status(Status::NotFound))?;
    tag_n_bag_it::<Verb>(x.verb)
}

#[get("/<rid>")]
async fn get_rid(rid: i32, db: &State<DB>, user: User) -> Result<ETaggedResource, MyError> {
    debug!("----- get_rid ----- {}", user);
    user.can_use_verbs()?;

    let x = ext_find_by_rid(db.pool(), rid)
        .await
        .map_err(|x| x.with_status(Status::NotFound))?;
    tag_n_bag_it::<Verb>(x)
}

#[get("/aggregates")]
async fn get_aggregates(db: &State<DB>, user: User) -> Result<ETaggedResource, MyError> {
    debug!("----- get_aggregates ----- {}", user);
    user.can_use_verbs()?;

    let x = ext_compute_aggregates(db.pool()).await?;
    tag_n_bag_it::<Aggregates>(x)
}

#[get("/")]
async fn get_some(
    q: QueryParams<'_>,
    db: &State<DB>,
    user: User,
) -> Result<ETaggedResource, MyError> {
    debug!("----- get_some ----- {}", user);
    user.can_use_verbs()?;

    debug!("q = {}", q);
    let x = ext_find_some(db.pool(), q).await?;
    tag_n_bag_it::<Vec<VerbUI>>(x)
}

fn tag_n_bag_it<T: Serialize>(resource: T) -> Result<ETaggedResource, MyError> {
    let json = serde_json::to_string(&resource).map_err(|x| MyError::Data(DataError::JSON(x)))?;
    debug!("json = {}", json);
    let etag = etag_from_str(&json);
    debug!("etag = {}", etag);

    Ok(ETaggedResource {
        inner: json,
        etag: Header::new(header::ETAG.as_str(), etag.to_string()),
    })
}