Source file cognito_user.ml

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
open Ppx_yojson_conv_lib.Yojson_conv.Primitives
open! Awso.Import
module Stdlib = Stdlib

module Attribute = struct
  type raw_attribute =
    { name : string
    ; value : string option
    }
  [@@deriving yojson]

  type t =
    [ `Unknown of raw_attribute
    | `Custom of raw_attribute
    | `Gender of string
    | `Family_name of string
    | `Locale of string
    | `Middle_name of string option
    | `Nickname of string
    | `Profile of string option
    | `Website of string option
    | `Picture of string
    | `Email of string
    | `Name of string
    | `Updated_at of string
    | `Preferred_user_name of string option
    | `Given_name of string
    ]
  [@@deriving yojson]
end

type attribute = Attribute.t [@@deriving yojson]

type t =
  { username : string
  ; attributes : attribute list
  ; access_token : string
  }
[@@deriving yojson]

type msg = string

let required_attribute t ~name ~f =
  match List.find_map t.attributes ~f with
  | Some x -> Ok x
  | None -> Error (sprintf "%s attribute required but not present" name)
;;

let required_attribute_exn t ~name ~f =
  Result.ok_or_failwith (required_attribute t ~name ~f)
;;

let%expect_test "required_attribute" =
  let call ~attributes =
    let user = { username = ""; attributes; access_token = "" } in
    let name = "name" in
    let f = function
      | `Name n -> Some n
      | _ -> None
    in
    required_attribute user ~name ~f
  in
  let test ~attributes =
    match call ~attributes with
    | Ok s -> printf "Ok %s" s
    | Error s -> printf "Error %s" s
  in
  test ~attributes:[ `Name "foo" ];
  [%expect {|Ok foo|}];
  test ~attributes:[ `Email "foo" ];
  [%expect {|Error name attribute required but not present|}]
;;

let optional_attribute t ~f = List.find_map t.attributes ~f

let%expect_test "optional_attribute" =
  let test ~attributes =
    let user = { username = ""; attributes; access_token = "" } in
    let f = function
      | `Name n -> Some n
      | _ -> None
    in
    match optional_attribute user ~f with
    | Some s -> printf "Some %s" s
    | None -> printf "None"
  in
  test ~attributes:[ `Name "foo" ];
  [%expect {|Some foo|}];
  test ~attributes:[ `Email "foo" ];
  [%expect {|None|}]
;;

let email t =
  required_attribute t ~name:"email" ~f:(function
    | `Email e -> Some e
    | _ -> None)
;;

let email_exn t = Result.ok_or_failwith (email t)

let%expect_test "email" =
  let call ~attributes = email { username = ""; attributes; access_token = "" } in
  let test ~attributes =
    match call ~attributes with
    | Ok s -> printf "Ok %s" s
    | Error s -> printf "Error %s" s
  in
  test ~attributes:[ `Email "foo" ];
  [%expect {|Ok foo|}];
  test ~attributes:[ `Name "foo" ];
  [%expect {|Error email attribute required but not present|}]
;;

let preferred_name t =
  optional_attribute t ~f:(function
    | `Preferred_user_name e -> Some e
    | _ -> None)
;;

let%expect_test "preferred_name" =
  let test ~attributes =
    match preferred_name { username = ""; attributes; access_token = "" } with
    | Some (Some s) -> printf "Some (Some %s)" s
    | Some None -> printf "Some None"
    | None -> printf "None"
  in
  test ~attributes:[ `Preferred_user_name (Some "foo") ];
  [%expect {|Some (Some foo)|}];
  test ~attributes:[ `Preferred_user_name None ];
  [%expect {|Some None|}];
  test ~attributes:[ `Email "foo" ];
  [%expect {|None|}]
;;

let family_name t =
  required_attribute t ~name:"family_name" ~f:(function
    | `Family_name e -> Some e
    | _ -> None)
;;

let family_name_exn t = Result.ok_or_failwith (family_name t)

let%expect_test "family_name" =
  let call ~attributes = family_name { username = ""; attributes; access_token = "" } in
  let test ~attributes =
    match call ~attributes with
    | Ok s -> printf "Ok %s" s
    | Error s -> printf "Error %s" s
  in
  test ~attributes:[ `Family_name "foo" ];
  [%expect {|Ok foo|}];
  test ~attributes:[ `Name "foo" ];
  [%expect {|Error family_name attribute required but not present|}]
;;

let name t =
  optional_attribute t ~f:(function
    | `Name e -> Some e
    | _ -> None)
;;

let%expect_test "name" =
  let test ~attributes =
    match name { username = ""; attributes; access_token = "" } with
    | Some s -> printf "Some %s" s
    | None -> printf "None"
  in
  test ~attributes:[ `Name "foo" ];
  [%expect {|Some foo|}];
  test ~attributes:[ `Email "foo" ];
  [%expect {|None|}]
;;

module Exn = struct
  let required_attribute = required_attribute_exn
  let email = email_exn
  let family_name = family_name_exn
end