Skip to content

Commit

Permalink
Merge pull request #40 from imandra-ai/b/add-some-request-preconditions
Browse files Browse the repository at this point in the history
Add some request preconditions
  • Loading branch information
benbellick authored Jan 7, 2025
2 parents d01d815 + e9f04a7 commit dbaf79e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ppx_deriving_yojson
(ppx_here :with-test)
(tls :with-test)
(x509 ( >= 0.12.0)) ;; Required for the ~sloppy:true arg becoming the default
(x509 (and (>= 0.12.0) (< 1.0))) ;; Required for the ~sloppy:true arg becoming the default
yojson))

(package
Expand Down
2 changes: 1 addition & 1 deletion gcloud.opam
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ depends: [
"ppx_deriving_yojson"
"ppx_here" {with-test}
"tls" {with-test}
"x509" {>= "0.12.0"}
"x509" {>= "0.12.0" & < "1.0"}
"yojson"
"odoc" {with-doc}
]
Expand Down
46 changes: 37 additions & 9 deletions src/storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,29 @@ let get_object (bucket_name : string) (object_path : string) :
get_object_stream bucket_name object_path >>= fun stream ->
Lwt_stream.to_list stream |> Lwt.map (String.concat "") |> Lwt_result.ok

let insert_object_ bucket_name name (body : Cohttp_lwt.Body.t) :
(object_, [> Error.t ]) Lwt_result.t =
let insert_object_ ~if_generation_match ~if_generation_not_match bucket_name
name (body : Cohttp_lwt.Body.t) : (object_, [> Error.t ]) Lwt_result.t =
let open Lwt_result.Infix in
Common.get_access_token ~scopes:[ Scopes.devstorage_read_write ] ()
>>= fun token_info ->
Lwt.catch
(fun () ->
let uri =
let query =
List.concat
[
[ ("name", [ name ]); ("uploadType", [ "media" ]) ];
(match if_generation_match with
| Some v -> [ ("ifGenerationMatch", [ string_of_int v ]) ]
| None -> []);
(match if_generation_not_match with
| Some v -> [ ("ifGenerationNotMatch", [ string_of_int v ]) ]
| None -> []);
]
in
Uri.make () ~scheme:"https" ~host:"storage.googleapis.com"
~path:(Printf.sprintf "upload/storage/v1/b/%s/o" bucket_name)
~query:[ ("name", [ name ]); ("uploadType", [ "media" ]) ]
~query
in
let headers =
Cohttp.Header.of_list
Expand All @@ -80,15 +92,18 @@ let insert_object_ bucket_name name (body : Cohttp_lwt.Body.t) :
| `OK -> Error.parse_body_json object__of_yojson body |> Lwt.return
| status_code -> Error.of_response_status_code_and_body status_code body

let insert_object bucket_name name (data : string) :
(object_, [> Error.t ]) Lwt_result.t =
let insert_object ?if_generation_match ?if_generation_not_match bucket_name name
(data : string) : (object_, [> Error.t ]) Lwt_result.t =
let body = Cohttp_lwt.Body.of_string data in
insert_object_ bucket_name name body
insert_object_ ~if_generation_match ~if_generation_not_match bucket_name name
body

let insert_object_stream bucket_name name (data : string Lwt_stream.t) :
let insert_object_stream ?if_generation_match ?if_generation_not_match
bucket_name name (data : string Lwt_stream.t) :
(object_, [> Error.t ]) Lwt_result.t =
let body = Cohttp_lwt.Body.of_stream data in
insert_object_ bucket_name name body
insert_object_ ~if_generation_match ~if_generation_not_match bucket_name name
body

type rewrite_object_response = {
kind : string;
Expand Down Expand Up @@ -187,7 +202,8 @@ let list_objects ?(delimiter : string option) ?(prefix : string option)
Error.parse_body_json list_objects_response_of_yojson body |> Lwt.return
| status_code -> Error.of_response_status_code_and_body status_code body

let delete_object (bucket_name : string) (object_path : string) :
let delete_object ?if_generation_match ?if_generation_not_match
(bucket_name : string) (object_path : string) :
(unit, [> Error.t ]) Lwt_result.t =
let open Lwt_result.Syntax in
let* token_info =
Expand All @@ -197,10 +213,22 @@ let delete_object (bucket_name : string) (object_path : string) :
Lwt.catch
(fun () ->
let uri =
let query =
List.concat
[
(match if_generation_match with
| Some v -> [ ("ifGenerationMatch", [ string_of_int v ]) ]
| None -> []);
(match if_generation_not_match with
| Some v -> [ ("ifGenerationNotMatch", [ string_of_int v ]) ]
| None -> []);
]
in
Uri.make () ~scheme:"https" ~host:"storage.googleapis.com"
~path:
(Printf.sprintf "storage/v1/b/%s/o/%s" bucket_name
(Uri.pct_encode object_path))
~query
in
let headers =
Cohttp.Header.of_list
Expand Down

0 comments on commit dbaf79e

Please sign in to comment.