Library Apps.StorageClient

Require Import FunctionApp List String System.
Import ListNotations.
Open Scope string_scope.

Definition storageURI := "https://andersk.scripts.mit.edu/pwmgr/storage".

Section StorageClient.

  Context {world0 world : Type}.
  Context {input : Type}.
  Context (httpPOST : stringlist (string × string) → (abortable world0 httpResponseinput) → action world).
  Context (storageId : string).

  Definition storageGet id cb :=
    httpPOST
      (storageURI ++ "/get") [("id", id)]
      (fun ra
         match ra with
           | started abortcb (started abort)
           | done r
             match httpResponseStatus r with
               | httpOkcb (done (inl (httpResponseBody r)))
               | _cb (done (inr r))
             end
         end).
  Definition storageSet id old new cb :=
    httpPOST
      (storageURI ++ "/set") [("id", id); ("old", old); ("new", new)]
      (fun ra
         match ra with
           | started abortcb (started abort)
           | done r
             match httpResponseStatus r with
               | httpOkcb (done (inl None))
               | httpPreconditionFailedcb (done (inl (Some (httpResponseBody r))))
               | _cb (done (inr r))
             end
         end).
  Definition storagePoll id old cb :=
    httpPOST
      (storageURI ++ "/poll") [("id", id); ("old", old)]
      (fun ra
         match ra with
           | started abortcb (started abort)
           | done r
             match httpResponseStatus r with
               | httpOkcb (done (inl (httpResponseBody r)))
               | _cb (done (inr r))
             end
         end).

End StorageClient.