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)
data ConnectionInformation = ConnectionInformation
{ ConnectionInformation -> String
ciHost :: String
, ConnectionInformation -> String
ciPort :: String
, ConnectionInformation -> String
ciDatabaseName :: String
, ConnectionInformation -> String
ciUser :: String
, ConnectionInformation -> String
ciPassword :: String
}
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
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