Source file endpoints.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
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
(* generated by: awso-codegen generate-all --botocore-data vendor/botocore/botocore/data -o aws --runtime-dir lib/runtime/awso --cli-dir awso-cli *)
open! Awso_common.Jane_compat
open Values
type ('i, 'o, 'e) t =
  | CreateLifecyclePolicy: (CreateLifecyclePolicyRequest.t,
  CreateLifecyclePolicyResponse.t, CreateLifecyclePolicyResponse.error) t 
  | DeleteLifecyclePolicy: (DeleteLifecyclePolicyRequest.t,
  DeleteLifecyclePolicyResponse.t, DeleteLifecyclePolicyResponse.error) t 
  | GetLifecyclePolicies: (GetLifecyclePoliciesRequest.t,
  GetLifecyclePoliciesResponse.t, GetLifecyclePoliciesResponse.error) t 
  | GetLifecyclePolicy: (GetLifecyclePolicyRequest.t,
  GetLifecyclePolicyResponse.t, GetLifecyclePolicyResponse.error) t 
  | ListTagsForResource: (ListTagsForResourceRequest.t,
  ListTagsForResourceResponse.t, ListTagsForResourceResponse.error) t 
  | TagResource: (TagResourceRequest.t, TagResourceResponse.t,
  TagResourceResponse.error) t 
  | UntagResource: (UntagResourceRequest.t, UntagResourceResponse.t,
  UntagResourceResponse.error) t 
  | UpdateLifecyclePolicy: (UpdateLifecyclePolicyRequest.t,
  UpdateLifecyclePolicyResponse.t, UpdateLifecyclePolicyResponse.error) t 
let method_of_endpoint : type i o e. (i, o, e) t -> _ =
  function
  | CreateLifecyclePolicy -> `POST
  | DeleteLifecyclePolicy -> `DELETE
  | GetLifecyclePolicies -> `GET
  | GetLifecyclePolicy -> `GET
  | ListTagsForResource -> `GET
  | TagResource -> `POST
  | UntagResource -> `DELETE
  | UpdateLifecyclePolicy -> `PATCH
let uri_of_endpoint : type i o e. (i, o, e) t -> i -> Uri.t =
  ((fun endpoint x ->
      match endpoint with
      | CreateLifecyclePolicy -> (Format.kasprintf Uri.of_string) "/policies"
      | DeleteLifecyclePolicy ->
          (Format.kasprintf Uri.of_string) "/policies/%s"
            (PolicyId.to_header x.DeleteLifecyclePolicyRequest.policyId)
      | GetLifecyclePolicies ->
          Uri.add_query_params'
            ((Format.kasprintf Uri.of_string) "/policies")
            (List.filter_opt
               [Option.map
                  ~f:(fun v -> ("policyIds", (PolicyIdList.to_header v)))
                  x.policyIds;
               Option.map
                 ~f:(fun v ->
                       ("state", (GettablePolicyStateValues.to_header v)))
                 x.state;
               Option.map
                 ~f:(fun v ->
                       ("resourceTypes",
                         (ResourceTypeValuesList.to_header v)))
                 x.resourceTypes;
               Option.map
                 ~f:(fun v ->
                       ("targetTags", (TargetTagsFilterList.to_header v)))
                 x.targetTags;
               Option.map
                 ~f:(fun v ->
                       ("tagsToAdd", (TagsToAddFilterList.to_header v)))
                 x.tagsToAdd;
               Option.map
                 ~f:(fun v ->
                       ("defaultPolicyType",
                         (DefaultPoliciesTypeValues.to_header v)))
                 x.defaultPolicyType])
      | GetLifecyclePolicy ->
          (Format.kasprintf Uri.of_string) "/policies/%s"
            (PolicyId.to_header x.GetLifecyclePolicyRequest.policyId)
      | ListTagsForResource ->
          (Format.kasprintf Uri.of_string) "/tags/%s"
            (PolicyArn.to_header x.ListTagsForResourceRequest.resourceArn)
      | TagResource ->
          (Format.kasprintf Uri.of_string) "/tags/%s"
            (PolicyArn.to_header x.TagResourceRequest.resourceArn)
      | UntagResource ->
          Uri.add_query_params'
            ((Format.kasprintf Uri.of_string) "/tags/%s"
               (PolicyArn.to_header x.UntagResourceRequest.resourceArn))
            (List.filter_opt
               [Some ("tagKeys", (TagKeyList.to_header x.tagKeys))])
      | UpdateLifecyclePolicy ->
          (Format.kasprintf Uri.of_string) "/policies/%s"
            (PolicyId.to_header x.UpdateLifecyclePolicyRequest.policyId))
  [@ocaml.warning "-27"])
let to_request (type i) (type o) (type e) (endp : (i, o, e) t) (req : i) =
  let _req = req in
  match endp with
  | CreateLifecyclePolicy ->
      let (headers, body) =
        let headers =
          Some ((List.filter_opt []) |> Awso.Http.Headers.of_list) in
        let body =
          Some
            ((`Assoc
                (List.map
                   (List.filter_opt
                      [Some
                         ("ExecutionRoleArn",
                           (ExecutionRoleArn.to_value
                              req.CreateLifecyclePolicyRequest.executionRoleArn));
                      Some
                        ("Description",
                          (PolicyDescription.to_value
                             req.CreateLifecyclePolicyRequest.description));
                      Some
                        ("State",
                          (SettablePolicyStateValues.to_value
                             req.CreateLifecyclePolicyRequest.state));
                      Option.map
                        req.CreateLifecyclePolicyRequest.policyDetails
                        ~f:(fun x ->
                              ("PolicyDetails", (PolicyDetails.to_value x)));
                      Option.map req.CreateLifecyclePolicyRequest.tags
                        ~f:(fun x -> ("Tags", (TagMap.to_value x)));
                      Option.map
                        req.CreateLifecyclePolicyRequest.defaultPolicy
                        ~f:(fun x ->
                              ("DefaultPolicy",
                                (DefaultPolicyTypeValues.to_value x)));
                      Option.map
                        req.CreateLifecyclePolicyRequest.createInterval
                        ~f:(fun x ->
                              ("CreateInterval", (CreateInterval.to_value x)));
                      Option.map
                        req.CreateLifecyclePolicyRequest.retainInterval
                        ~f:(fun x ->
                              ("RetainInterval", (RetainInterval.to_value x)));
                      Option.map req.CreateLifecyclePolicyRequest.copyTags
                        ~f:(fun x ->
                              ("CopyTags", (CopyTagsNullable.to_value x)));
                      Option.map
                        req.CreateLifecyclePolicyRequest.extendDeletion
                        ~f:(fun x ->
                              ("ExtendDeletion", (ExtendDeletion.to_value x)));
                      Option.map
                        req.CreateLifecyclePolicyRequest.crossRegionCopyTargets
                        ~f:(fun x ->
                              ("CrossRegionCopyTargets",
                                (CrossRegionCopyTargetList.to_value x)));
                      Option.map req.CreateLifecyclePolicyRequest.exclusions
                        ~f:(fun x -> ("Exclusions", (Exclusions.to_value x)))])
                   ~f:(fun (x, y) ->
                         let value =
                           Awso.Botodata.Json.value_to_json_scalar y in
                         (x, value))))
               |> Yojson.Safe.to_string) in
        (headers, body) in
      Awso.Http.Request.make ?headers ?body (method_of_endpoint endp)
  | DeleteLifecyclePolicy -> Awso.Http.Request.make (method_of_endpoint endp)
  | GetLifecyclePolicies ->
      let (headers, body) = (None, None) in
      Awso.Http.Request.make ?headers ?body (method_of_endpoint endp)
  | GetLifecyclePolicy ->
      let (headers, body) = (None, None) in
      Awso.Http.Request.make ?headers ?body (method_of_endpoint endp)
  | ListTagsForResource ->
      let (headers, body) = (None, None) in
      Awso.Http.Request.make ?headers ?body (method_of_endpoint endp)
  | TagResource ->
      let (headers, body) =
        let headers =
          Some ((List.filter_opt []) |> Awso.Http.Headers.of_list) in
        let body =
          Some
            ((`Assoc
                (List.map
                   (List.filter_opt
                      [Some
                         ("Tags",
                           (TagMap.to_value req.TagResourceRequest.tags))])
                   ~f:(fun (x, y) ->
                         let value =
                           Awso.Botodata.Json.value_to_json_scalar y in
                         (x, value))))
               |> Yojson.Safe.to_string) in
        (headers, body) in
      Awso.Http.Request.make ?headers ?body (method_of_endpoint endp)
  | UntagResource -> Awso.Http.Request.make (method_of_endpoint endp)
  | UpdateLifecyclePolicy -> Awso.Http.Request.make (method_of_endpoint endp)
let of_response (type i) (type o) (type e) (endpoint : (i, o, e) t)
  (resp : Awso.Http.Response.t) : (o, e) result=
  let code = Awso.Http.Status.to_code (Awso.Http.Response.status resp) in
  let is_success = (code >= 200) && (code < 300) in
  let x_amzn_error_type =
    let headers = Awso.Http.Headers.to_list (Awso.Http.Response.headers resp) in
    match List.Assoc.find ~equal:String.Caseless.equal headers
            "x-amzn-ErrorType"
    with
    | None -> None
    | Some value ->
        (match String.lsplit2 value ~on:':' with
         | None -> Some value
         | Some (v, _) -> Some v) in
  let parse_aws_error error_of_json =
    let body = Awso.Http.Response.body resp in
    let bail () =
      raise
        (Awso.Http.Io.Error.Bad_response
           { Awso.Http.Io.Error.code = code; body; x_amzn_error_type }) in
    match (x_amzn_error_type, error_of_json,
            ((code >= 400) && (code <= 599)))
    with
    | (Some error_type, Some error_of_json, true) ->
        let json = Yojson.Safe.from_string body in
        error_of_json error_type json
    | (None, Some error_of_json, true) ->
        (try
           let json = Yojson.Safe.from_string body in
           match json |> (Yojson.Safe.Util.member "__type") with
           | `String error_type ->
               let error_type =
                 match String.lsplit2 error_type ~on:'#' with
                 | Some (_, s) -> s
                 | None -> error_type in
               error_of_json error_type json
           | `Null -> bail ()
           | _ ->
               failwithf "Error '__type' did not have string type: %s" body
                 ()
         with | _ -> bail ())
    | (None, _, _) | (_, None, _) | (_, _, false) -> bail () in
  let response_to_json resp =
    Yojson.Safe.from_string (Awso.Http.Response.body resp) in
  let _ = parse_aws_error in
  let _ = response_to_json in
  let _ = resp in
  match endpoint with
  | CreateLifecyclePolicy ->
      if is_success
      then Ok (CreateLifecyclePolicyResponse.of_json (response_to_json resp))
      else
        Error
          (parse_aws_error (Some CreateLifecyclePolicyResponse.error_of_json))
  | DeleteLifecyclePolicy ->
      if is_success
      then
        let headers =
          Awso.Http.Headers.to_list (Awso.Http.Response.headers resp) in
        Ok (DeleteLifecyclePolicyResponse.of_header_and_body (headers, ()))
      else
        Error
          (parse_aws_error (Some DeleteLifecyclePolicyResponse.error_of_json))
  | GetLifecyclePolicies ->
      if is_success
      then Ok (GetLifecyclePoliciesResponse.of_json (response_to_json resp))
      else
        Error
          (parse_aws_error (Some GetLifecyclePoliciesResponse.error_of_json))
  | GetLifecyclePolicy ->
      if is_success
      then Ok (GetLifecyclePolicyResponse.of_json (response_to_json resp))
      else
        Error
          (parse_aws_error (Some GetLifecyclePolicyResponse.error_of_json))
  | ListTagsForResource ->
      if is_success
      then Ok (ListTagsForResourceResponse.of_json (response_to_json resp))
      else
        Error
          (parse_aws_error (Some ListTagsForResourceResponse.error_of_json))
  | TagResource ->
      if is_success
      then
        let headers =
          Awso.Http.Headers.to_list (Awso.Http.Response.headers resp) in
        Ok (TagResourceResponse.of_header_and_body (headers, ()))
      else Error (parse_aws_error (Some TagResourceResponse.error_of_json))
  | UntagResource ->
      if is_success
      then
        let headers =
          Awso.Http.Headers.to_list (Awso.Http.Response.headers resp) in
        Ok (UntagResourceResponse.of_header_and_body (headers, ()))
      else Error (parse_aws_error (Some UntagResourceResponse.error_of_json))
  | UpdateLifecyclePolicy ->
      if is_success
      then
        let headers =
          Awso.Http.Headers.to_list (Awso.Http.Response.headers resp) in
        Ok (UpdateLifecyclePolicyResponse.of_header_and_body (headers, ()))
      else
        Error
          (parse_aws_error (Some UpdateLifecyclePolicyResponse.error_of_json))