package awso-eio

  1. Overview
  2. Docs
AWS client for Eio (400+ services, auto-generated from botocore)

Install

dune-project
 Dependency

Authors

Maintainers

Sources

awso-0.9.1.tbz
sha256=e0e7510192b7e45871cff5c60e5162a77a9fa103b6924136fb1fea5db2092b26
sha512=51a18cf74c122b021bd84ef5728b7c5b2d5e767f03d5cd2387d886edc34a68cf54a3183e2d6c4939b4c8d3e0c0e406c869d019f2ee9a0903ae4cf7ca725572f0

doc/src/awso-eio/http.ml.html

Source file http.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
open! Awso_common.Jane_compat

let to_headers req =
  req |> Awso.Http.Request.headers |> Awso.Http.Headers.to_list |> Cohttp.Header.of_list
;;

let of_version resp =
  match Cohttp.Response.version resp with
  | `HTTP_1_0 -> 1, 0
  | `HTTP_1_1 -> 1, 1
  | `Other _ -> 0, 0
;;

let of_headers resp =
  resp |> Cohttp.Response.headers |> Cohttp.Header.to_list |> Awso.Http.Headers.of_list
;;

let of_status resp =
  match Cohttp.Response.status resp with
  | #Awso.Http.Status.t as status -> (status :> Awso.Http.Status.t)
  | status -> `Code (Cohttp.Code.code_of_status status)
;;

module Io = struct
  type 'a t = 'a

  let return x = x
  let bind x f = f x
  let map x f = f x

  let resolve_cfg = function
    | Some cfg -> cfg
    | None ->
      failwith
        "awso-eio: ~cfg is required (Cfg.get_exn needs the Eio env, so we can't \
         auto-resolve here)"
  ;;

  let call ?endpoint_url ~cfg ~service meth request uri =
    let { Cfg.aws_cfg; client } = cfg in
    let { Awso.Cfg.region
        ; aws_access_key_id
        ; aws_secret_access_key
        ; aws_session_token
        ; _
        }
      =
      aws_cfg
    in
    let region =
      match region with
      | Some r -> r
      | None -> failwith "config must set 'region'"
    in
    let endpoint =
      match endpoint_url with
      | Some s -> Uri.of_string s
      | None -> Awso.Botocore_endpoints.lookup_uri ~region service `HTTPS
    in
    let uri = Uri.with_uri ~scheme:(Uri.scheme endpoint) ~host:(Uri.host endpoint) uri in
    let host =
      match Uri.host endpoint with
      | Some h -> h
      | None ->
        failwithf "could not extract 'host' from url %s" (Uri.to_string endpoint) ()
    in
    let req_body = Awso.Http.Request.body request in
    let body_length = Int64.of_int (String.length req_body) in
    let payload_hash = Awso.Auth.payload_hash req_body in
    let cohttp_meth = (meth :> Cohttp.Code.meth) in
    let cohttp_headers =
      let h = to_headers request in
      Cohttp.Header.add_unless_exists h "host" host
    in
    let signed_request =
      Cohttp.Request.make_for_client
        ~headers:cohttp_headers
        ~chunked:false
        ~body_length
        cohttp_meth
        uri
      |> Awso.Auth.sign_request
           ~region
           ~service
           ?session_token:aws_session_token
           ?aws_access_key_id
           ?aws_secret_access_key
           ~payload_hash
    in
    let signed_headers = Cohttp.Request.headers signed_request in
    (* 2026-05-18 mbac: This tautological looking match is actually converting
       Cohttp.Code.meth to Http.Method.t. It's leveraging poly-variant inference
       to avoid module name collisions: this module is called Http, not to be confused
       with the `http` opam library. *)
    let eio_meth =
      match meth with
      | `GET -> `GET
      | `POST -> `POST
      | `HEAD -> `HEAD
      | `PUT -> `PUT
      | `DELETE -> `DELETE
      | `CONNECT -> `CONNECT
      | `OPTIONS -> `OPTIONS
      | `TRACE -> `TRACE
      | `PATCH -> `PATCH
      | `Other s -> `Other s
    in
    Eio.Switch.run (fun sw ->
      let body = Cohttp_eio.Body.of_string req_body in
      let resp, resp_body =
        Cohttp_eio.Client.call
          ~sw
          client
          ~headers:signed_headers
          ~body
          ~chunked:false
          eio_meth
          uri
      in
      let body_str = Eio.Buf_read.(parse_exn take_all) resp_body ~max_size:max_int in
      Awso.Http.Response.make
        ~version:(of_version resp)
        ~headers:(of_headers resp)
        ~body:body_str
        (of_status resp))
  ;;
end