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
(* 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 =
  | CancelJob: (CancelJobInput.t, CancelJobOutput.t, CancelJobOutput.error) t
  
  | CreateJob: (CreateJobInput.t, CreateJobOutput.t, CreateJobOutput.error) t
  
  | GetShippingLabel: (GetShippingLabelInput.t, GetShippingLabelOutput.t,
  GetShippingLabelOutput.error) t 
  | GetStatus: (GetStatusInput.t, GetStatusOutput.t, GetStatusOutput.error) t
  
  | ListJobs: (ListJobsInput.t, ListJobsOutput.t, ListJobsOutput.error) t 
  | UpdateJob: (UpdateJobInput.t, UpdateJobOutput.t, UpdateJobOutput.error) t 
let method_of_endpoint : type i o e. (i, o, e) t -> _ =
  function
  | CancelJob -> `POST
  | CreateJob -> `POST
  | GetShippingLabel -> `POST
  | GetStatus -> `POST
  | ListJobs -> `POST
  | UpdateJob -> `POST
let uri_of_endpoint : type i o e. (i, o, e) t -> i -> Uri.t =
  ((fun endpoint x ->
      match endpoint with
      | CancelJob -> (Format.kasprintf Uri.of_string) "/?Operation=CancelJob"
      | CreateJob -> (Format.kasprintf Uri.of_string) "/?Operation=CreateJob"
      | GetShippingLabel ->
          (Format.kasprintf Uri.of_string) "/?Operation=GetShippingLabel"
      | GetStatus -> (Format.kasprintf Uri.of_string) "/?Operation=GetStatus"
      | ListJobs -> (Format.kasprintf Uri.of_string) "/?Operation=ListJobs"
      | UpdateJob -> (Format.kasprintf Uri.of_string) "/?Operation=UpdateJob")
  [@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
  | CancelJob ->
      let headers =
        Awso.Http.Headers.of_list
          [("content-type",
             "application/x-www-form-urlencoded; charset=utf-8")] in
      let body =
        let meta = [("Action", ["CancelJob"]); ("Version", [apiVersion])] in
        let query = (CancelJobInput.to_query req) |> Awso.Client.Query.render in
        Some (Uri.encoded_of_query (meta @ query)) in
      Awso.Http.Request.make ?body ~headers (method_of_endpoint endp)
  | CreateJob ->
      let headers =
        Awso.Http.Headers.of_list
          [("content-type",
             "application/x-www-form-urlencoded; charset=utf-8")] in
      let body =
        let meta = [("Action", ["CreateJob"]); ("Version", [apiVersion])] in
        let query = (CreateJobInput.to_query req) |> Awso.Client.Query.render in
        Some (Uri.encoded_of_query (meta @ query)) in
      Awso.Http.Request.make ?body ~headers (method_of_endpoint endp)
  | GetShippingLabel ->
      let headers =
        Awso.Http.Headers.of_list
          [("content-type",
             "application/x-www-form-urlencoded; charset=utf-8")] in
      let body =
        let meta =
          [("Action", ["GetShippingLabel"]); ("Version", [apiVersion])] in
        let query =
          (GetShippingLabelInput.to_query req) |> Awso.Client.Query.render in
        Some (Uri.encoded_of_query (meta @ query)) in
      Awso.Http.Request.make ?body ~headers (method_of_endpoint endp)
  | GetStatus ->
      let headers =
        Awso.Http.Headers.of_list
          [("content-type",
             "application/x-www-form-urlencoded; charset=utf-8")] in
      let body =
        let meta = [("Action", ["GetStatus"]); ("Version", [apiVersion])] in
        let query = (GetStatusInput.to_query req) |> Awso.Client.Query.render in
        Some (Uri.encoded_of_query (meta @ query)) in
      Awso.Http.Request.make ?body ~headers (method_of_endpoint endp)
  | ListJobs ->
      let headers =
        Awso.Http.Headers.of_list
          [("content-type",
             "application/x-www-form-urlencoded; charset=utf-8")] in
      let body =
        let meta = [("Action", ["ListJobs"]); ("Version", [apiVersion])] in
        let query = (ListJobsInput.to_query req) |> Awso.Client.Query.render in
        Some (Uri.encoded_of_query (meta @ query)) in
      Awso.Http.Request.make ?body ~headers (method_of_endpoint endp)
  | UpdateJob ->
      let headers =
        Awso.Http.Headers.of_list
          [("content-type",
             "application/x-www-form-urlencoded; charset=utf-8")] in
      let body =
        let meta = [("Action", ["UpdateJob"]); ("Version", [apiVersion])] in
        let query = (UpdateJobInput.to_query req) |> Awso.Client.Query.render in
        Some (Uri.encoded_of_query (meta @ query)) in
      Awso.Http.Request.make ?body ~headers (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 parse_aws_error error_of_xml =
    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 = None }) in
    match (error_of_xml, ((code >= 400) && (code <= 599))) with
    | (None, _) | (_, false) -> bail ()
    | (Some error_of_xml, true) ->
        (match Awso.Xml.parse_response body with
         | `Data _ -> bail ()
         | `El (((_, "ErrorResponse"), _), _) as error_response_xml ->
             let error_xml = Awso.Xml.child_exn error_response_xml "Error" in
             (try
                let error_code =
                  match Awso.Xml.child_exn error_xml "Code" with
                  | `Data error_code -> error_code
                  | `El (_, children) ->
                      (List.map children
                         ~f:(function | `Data s -> s | `El _ -> ""))
                        |> (String.concat ~sep:"") in
                error_of_xml (String.strip error_code) error_xml
              with | Failure _ -> bail ())
         | `El _ -> bail ()) in
  let _ = parse_aws_error in
  let _ = resp in
  match endpoint with
  | CancelJob ->
      if is_success
      then
        let xml = Awso.Xml.parse_response (Awso.Http.Response.body resp) in
        Ok (CancelJobOutput.of_xml xml)
      else Error (parse_aws_error (Some CancelJobOutput.error_of_xml))
  | CreateJob ->
      if is_success
      then
        let xml = Awso.Xml.parse_response (Awso.Http.Response.body resp) in
        Ok (CreateJobOutput.of_xml xml)
      else Error (parse_aws_error (Some CreateJobOutput.error_of_xml))
  | GetShippingLabel ->
      if is_success
      then
        let xml = Awso.Xml.parse_response (Awso.Http.Response.body resp) in
        Ok (GetShippingLabelOutput.of_xml xml)
      else Error (parse_aws_error (Some GetShippingLabelOutput.error_of_xml))
  | GetStatus ->
      if is_success
      then
        let xml = Awso.Xml.parse_response (Awso.Http.Response.body resp) in
        Ok (GetStatusOutput.of_xml xml)
      else Error (parse_aws_error (Some GetStatusOutput.error_of_xml))
  | ListJobs ->
      if is_success
      then
        let xml = Awso.Xml.parse_response (Awso.Http.Response.body resp) in
        Ok (ListJobsOutput.of_xml xml)
      else Error (parse_aws_error (Some ListJobsOutput.error_of_xml))
  | UpdateJob ->
      if is_success
      then
        let xml = Awso.Xml.parse_response (Awso.Http.Response.body resp) in
        Ok (UpdateJobOutput.of_xml xml)
      else Error (parse_aws_error (Some UpdateJobOutput.error_of_xml))