From 01949c23f629444f1667ce22e5a3bc0d48c0040f Mon Sep 17 00:00:00 2001 From: Ryan Kavanagh Date: Wed, 27 Oct 2021 17:24:55 -0400 Subject: json parsing for the core API endpoints --- lib/listenbrainz_util.ml | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'lib/listenbrainz_util.ml') diff --git a/lib/listenbrainz_util.ml b/lib/listenbrainz_util.ml index 35a7af8..5056c09 100644 --- a/lib/listenbrainz_util.ml +++ b/lib/listenbrainz_util.ml @@ -1,9 +1,32 @@ open Listenbrainz_t -let validate_submission s = +let uuid_v4_re = + (* UUIDs are 8-4-4-4-12 hex digits. + * The third block of version 4 UUIDS starts with a 4. *) + let four_hex = "[0-9a-f][0-9a-f][0-9a-f][0-9a-f]" + in Str.regexp_case_fold ("^" ^ four_hex ^ four_hex + ^ "-" ^ four_hex + ^ "-4[0-9a-f][0-9a-f][0-9a-f]" + ^ "-" ^ four_hex + ^ "-" ^ four_hex ^ four_hex ^ four_hex ^ "$") +let tm_unwrap tm = + let (f, _) = Unix.mktime tm in f + +let validate_mbid mbid = Str.string_match uuid_v4_re mbid 0 + +let validate_post_submit_listens s = match s.listen_type with | `Single -> List.length s.payload = 1 | _ -> true -let validate_response r = +let validate_get_validate_token r = + r.code = 200 || r.code = 400 + +let validate_get_user_playing_now r = + match r.listens with + | [] -> true + | [l] -> l.listened_at = None + | _ -> false + +let validate_get_user_listens r = List.length r.listens = r.count -- cgit v1.2.3