{-
    Copyright (c) 2015-2023 Bardur Arantsson
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-}
{-|

  Client library for <https://github.com/ClockworkConsulting/tempgres-server Tempgres>.

  Use the 'createTemporaryDatabase' function to create databases.

-}
module Database.Tempgres.Client
    ( ConnectionInformation(..)
    , createTemporaryDatabase
    , toConnectionString
    ) where

import Data.ByteString (ByteString)
import Data.ByteString.Char8 qualified as B8
import Network.HTTP (simpleHTTP, postRequest, getResponseBody)

-- | Connection information to use for connecting to a database.
data ConnectionInformation = ConnectionInformation
    { ConnectionInformation -> String
ciHost :: String
    , ConnectionInformation -> String
ciPort :: String
    , ConnectionInformation -> String
ciDatabaseName :: String
    , ConnectionInformation -> String
ciUser :: String
    , ConnectionInformation -> String
ciPassword :: String
    }

-- | Create temporary database using the given URL to a
-- running @Tempgres@ server. Returns the connection
-- information for connecting to the newly created temporary
-- database.
createTemporaryDatabase :: String -> IO ConnectionInformation
createTemporaryDatabase :: String -> IO ConnectionInformation
createTemporaryDatabase String
url = do
  Result (Response String)
rsp <- forall ty. HStream ty => Request ty -> IO (Result (Response ty))
simpleHTTP (String -> Request_String
postRequest String
url)
  String
body <- forall ty. Result (Response ty) -> IO ty
getResponseBody Result (Response String)
rsp
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ String -> ConnectionInformation
parse String
body
  where
    parse :: String -> ConnectionInformation
    parse :: String -> ConnectionInformation
parse String
s =
      case String -> [String]
lines String
s of
        [ String
user, String
password, String
host, String
port, String
databaseName ] ->
            String
-> String -> String -> String -> String -> ConnectionInformation
ConnectionInformation String
host String
port String
databaseName String
user String
password
        [String]
_ ->
            forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Invalid response from server: " forall a. [a] -> [a] -> [a]
++ String
s

-- | Convert connection information to a @libpq@ or
-- @postgresql-simple@ compatible connection string.
toConnectionString :: ConnectionInformation -> ByteString
toConnectionString :: ConnectionInformation -> ByteString
toConnectionString (ConnectionInformation String
host String
port String
databaseName String
user String
password) =
  String -> ByteString
B8.pack forall a b. (a -> b) -> a -> b
$
    String
"host=" forall a. [a] -> [a] -> [a]
++ String
host forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++
    String
"port=" forall a. [a] -> [a] -> [a]
++ String
port forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++
    String
"dbname=" forall a. [a] -> [a] -> [a]
++ String
databaseName forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++
    String
"user=" forall a. [a] -> [a] -> [a]
++ String
user forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++
    String
"password=" forall a. [a] -> [a] -> [a]
++ String
password