module API.Nginz where

import qualified Codec.MIME.Type as MIME
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LBSC
import qualified Data.Text as T
import qualified Network.HTTP.Client as HTTP
import Test.Cargohold.API.Util (buildMultipartBody, multipartBoundary)
import Testlib.Prelude

getSystemSettingsUnAuthorized :: (HasCallStack, MakesValue domain) => domain -> App Response
getSystemSettingsUnAuthorized :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> App Response
getSystemSettingsUnAuthorized domain
domain = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest domain
domain Service
Nginz Versioned
Versioned String
"/system/settings/unauthorized"
  String -> Request -> App Response
submit String
"GET" Request
req

login :: (HasCallStack, MakesValue domain, MakesValue email, MakesValue password) => domain -> email -> password -> App Response
login :: forall domain email password.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password) =>
domain -> email -> password -> App Response
login domain
domain email
email password
pw = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Unversioned String
"/login"
  String
emailStr <- email -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make email
email App Value -> (Value -> App String) -> App String
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String
pwStr <- password -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make password
pw App Value -> (Value -> App String) -> App String
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String -> Request -> App Response
submit String
"POST" (Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [Pair] -> Request -> Request
addJSONObject [String
"email" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
emailStr, String
"password" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
pwStr, String
"label" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"auth"])

loginWith2ndFactor :: (HasCallStack, MakesValue domain, MakesValue email, MakesValue password, MakesValue sndFactor) => domain -> email -> password -> sndFactor -> App Response
loginWith2ndFactor :: forall domain email password sndFactor.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password, MakesValue sndFactor) =>
domain -> email -> password -> sndFactor -> App Response
loginWith2ndFactor domain
domain email
email password
pw sndFactor
sf = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Unversioned String
"/login"
  String
emailStr <- email -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make email
email App Value -> (Value -> App String) -> App String
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String
pwStr <- password -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make password
pw App Value -> (Value -> App String) -> App String
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String
sfStr <- sndFactor -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make sndFactor
sf App Value -> (Value -> App String) -> App String
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String -> Request -> App Response
submit String
"POST" (Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [Pair] -> Request -> Request
addJSONObject [String
"email" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
emailStr, String
"password" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
pwStr, String
"label" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"auth", String
"verification_code" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
sfStr])

access :: (HasCallStack, MakesValue domain, MakesValue cookie) => domain -> cookie -> App Response
access :: forall domain cookie.
(HasCallStack, MakesValue domain, MakesValue cookie) =>
domain -> cookie -> App Response
access domain
domain cookie
cookie = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Unversioned String
"/access"
  String
cookieStr <- cookie -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make cookie
cookie App Value -> (Value -> App String) -> App String
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String -> Request -> App Response
submit String
"POST" (Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> Request -> Request
setCookie String
cookieStr)

logout :: (HasCallStack, MakesValue domain, MakesValue cookie, MakesValue token) => domain -> cookie -> token -> App Response
logout :: forall domain email password.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password) =>
domain -> email -> password -> App Response
logout domain
d cookie
c token
t = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
d Service
Nginz Versioned
Unversioned String
"/access/logout"
  String
cookie <- cookie -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make cookie
c App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String
token <- token -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make token
t App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  String -> Request -> App Response
submit String
"POST" (Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> Request -> Request
setCookie String
cookie Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Authorization" (String
"Bearer " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
token))

getConversation :: (HasCallStack, MakesValue user, MakesValue qcnv, MakesValue token) => user -> qcnv -> token -> App Response
getConversation :: forall domain email password.
(HasCallStack, MakesValue domain, MakesValue email,
 MakesValue password) =>
domain -> email -> password -> App Response
getConversation user
user qcnv
qcnv token
t = do
  (String
domain, String
cnv) <- qcnv -> App (String, String)
forall a. (HasCallStack, MakesValue a) => a -> App (String, String)
objQid qcnv
qcnv
  String
token <- token -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make token
t App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  Request
req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest user
user Service
Nginz Versioned
Versioned ([String] -> String
joinHttpPath [String
"conversations", String
domain, String
cnv])
  String -> Request -> App Response
submit String
"GET" (Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Authorization" (String
"Bearer " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
token))

uploadProviderAsset :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
uploadProviderAsset :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> String -> App Response
uploadProviderAsset domain
domain String
cookie String
payload = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
rawBaseRequest domain
domain Service
Nginz Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ [String] -> String
joinHttpPath [String
"provider", String
"assets"]
  RequestBody
bdy <- HasCallStack => String -> App RequestBody
String -> App RequestBody
txtAsset String
payload
  String -> Request -> App Response
submit String
"POST"
    (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> Request -> Request
setCookie String
cookie
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& RequestBody -> String -> Request -> Request
addBody RequestBody
bdy String
multipartMixedMime

txtAsset :: (HasCallStack) => String -> App HTTP.RequestBody
txtAsset :: HasCallStack => String -> App RequestBody
txtAsset String
payload =
  Bool -> Maybe String -> ByteString -> MIMEType -> App RequestBody
forall assetRetention.
(HasCallStack, MakesValue assetRetention) =>
Bool -> assetRetention -> ByteString -> MIMEType -> App RequestBody
buildUploadAssetRequestBody
    Bool
True
    (Maybe String
forall a. Maybe a
Nothing :: Maybe String)
    (String -> ByteString
LBSC.pack String
payload)
    MIMEType
textPlainMime

textPlainMime :: MIME.MIMEType
textPlainMime :: MIMEType
textPlainMime = TextType -> MIMEType
MIME.Text (TextType -> MIMEType) -> TextType -> MIMEType
forall a b. (a -> b) -> a -> b
$ String -> TextType
T.pack String
"plain"

-- This case is a bit special and doesn't fit to MIMEType: We need to define
-- the boundary.
multipartMixedMime :: String
multipartMixedMime :: String
multipartMixedMime = String
"multipart/mixed; boundary=" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
multipartBoundary

buildUploadAssetRequestBody ::
  (HasCallStack, MakesValue assetRetention) =>
  Bool ->
  assetRetention ->
  LBS.ByteString ->
  MIME.MIMEType ->
  App HTTP.RequestBody
buildUploadAssetRequestBody :: forall assetRetention.
(HasCallStack, MakesValue assetRetention) =>
Bool -> assetRetention -> ByteString -> MIMEType -> App RequestBody
buildUploadAssetRequestBody Bool
isPublic assetRetention
retention ByteString
body MIMEType
mimeType = do
  Value
mbRetention <- assetRetention -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make assetRetention
retention
  let header' :: Aeson.Value
      header' :: Value
header' =
        [Pair] -> Value
Aeson.object
          [ String
"public" String -> Bool -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Bool
isPublic,
            String
"retention" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Value
mbRetention
          ]
  ByteString -> RequestBody
HTTP.RequestBodyLBS (ByteString -> RequestBody) -> App ByteString -> App RequestBody
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> ByteString -> MIMEType -> App ByteString
forall header.
(HasCallStack, MakesValue header) =>
header -> ByteString -> MIMEType -> App ByteString
buildMultipartBody Value
header' ByteString
body MIMEType
mimeType