@@ -136,10 +136,16 @@ module HttpStore = struct
136136
137137 open Cohttp_eio
138138
139- let raise_status_error s =
140- let c = Cohttp.Code. code_of_status s in
139+ let raise_status_error e =
140+ let c = Cohttp.Code. code_of_status e in
141141 raise (Request_failed (c, Cohttp.Code. reason_phrase_of_code c))
142142
143+ let fold_response ~success resp key = match Http.Response. status resp with
144+ | #Http.Status. success -> success ()
145+ | #Http.Status. client_error as e when e = `Not_found ->
146+ raise (Zarr.Storage. Key_not_found key)
147+ | e -> raise_status_error e
148+
143149 module IO = struct
144150 module Deferred = Deferred
145151
@@ -149,31 +155,22 @@ module HttpStore = struct
149155 Eio.Switch. run @@ fun sw ->
150156 let url = Uri. with_path t.base_url key in
151157 let resp, body = Client. get ~sw t.client url in
152- match Http.Response. status resp with
153- | #Http.Status. success -> Eio.Flow. read_all body
154- | #Http.Status. client_error as e when e = `Not_found ->
155- raise (Zarr.Storage. Key_not_found key)
156- | e -> raise_status_error e
158+ fold_response ~success: (fun () -> Eio.Flow. read_all body) resp key
157159
158160 let size t key = try String. length (get t key) with
159161 | Zarr.Storage. Key_not_found _ -> 0
160162
161163 (* let size t key =
164+ let content_length resp () = match Http.Response.content_length resp with
165+ | Some l -> l
166+ | None -> String.length (get t key)
167+ in
162168 Eio.Switch.run @@ fun sw ->
163169 let url = Uri.with_path t.base_url key in
164170 let resp = Client.head ~sw t.client url in
165- match Http.Response.status resp with
166- | #Http.Status.success ->
167- begin match Http.Response.content_length resp with
168- | Some l -> l
169- | None ->
170- try String.length (get t key) with
171- | Zarr.Storage.Key_not_found _ -> 0
172- end
173- | #Http.Status.client_error as e when e = `Not_found -> Deferred.return 0
174- | e -> raise_status_error e *)
171+ fold_response ~success:(content_length resp) resp key *)
175172
176- let is_member t key = if (size t key) = 0 then false else true
173+ let is_member t key = if (size t key) > 0 then true else false
177174
178175 let get_partial_values t key ranges =
179176 let read_range ~data ~size (ofs , len ) = match len with
@@ -189,16 +186,12 @@ module HttpStore = struct
189186 let url = Uri. with_path t.base_url key in
190187 let headers = Http.Header. of_list [(" Content-Length" , string_of_int (String. length data))] in
191188 let body = Body. of_string data in
192- let resp, _ = Client. post ~sw ~headers ~body t.client url in
193- match Http.Response. status resp with
194- | #Http.Status. success -> Deferred. return_unit
195- | e -> raise_status_error e
189+ let resp, _ = Client. put ~sw ~headers ~body t.client url in
190+ fold_response ~success: (fun () -> () ) resp key
196191
197192 let set_partial_values t key ?(append =false ) rsv =
198- let size = size t key in
199- let ov = match size with
200- | 0 -> Deferred. return String. empty
201- | _ -> get t key
193+ let ov = try get t key with
194+ | Zarr.Storage. Key_not_found _ -> String. empty
202195 in
203196 let f = if append || ov = String. empty then
204197 fun acc (_ , v ) -> acc ^ v else
@@ -215,6 +208,7 @@ module HttpStore = struct
215208 let resp, _ = Client.delete ~sw t.client url in
216209 match Http.Response.status resp with
217210 | #Http.Status.success -> Deferred.return_unit
211+ | #Http.Status.client_error as e when e = `Not_found -> Deferred.return_unit
218212 | e -> raise_status_error e *)
219213
220214 let erase _ = raise Not_implemented
0 commit comments