module API.Cargohold where
import API.Federator
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 GHC.Stack
import Network.HTTP.Client (Request (redirectCount))
import qualified Network.HTTP.Client as HTTP
import Test.Cargohold.API.Util
import Testlib.Prelude
import UnliftIO (catch)
type LByteString = LBS.ByteString
getFederationAsset :: (HasCallStack, MakesValue asset) => asset -> App Response
getFederationAsset :: forall asset.
(HasCallStack, MakesValue asset) =>
asset -> App Response
getFederationAsset asset
ga = do
Request
req <- Domain -> (ServiceMap -> HostPort) -> String -> App Request
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> (ServiceMap -> HostPort) -> String -> App Request
rawBaseRequestF Domain
OwnDomain ServiceMap -> HostPort
cargohold String
"federation/get-asset"
Value
bdy <- asset -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make asset
ga
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
& RequestBody -> String -> Request -> Request
addBody (ByteString -> RequestBody
HTTP.RequestBodyLBS (ByteString -> RequestBody) -> ByteString -> RequestBody
forall a b. (a -> b) -> a -> b
$ Value -> ByteString
forall a. ToJSON a => a -> ByteString
encode Value
bdy) String
"application/json"
uploadAssetV3 :: (HasCallStack, MakesValue user, MakesValue assetRetention) => user -> Bool -> assetRetention -> MIME.MIMEType -> LByteString -> App Response
uploadAssetV3 :: forall user assetRetention.
(HasCallStack, MakesValue user, MakesValue assetRetention) =>
user
-> Bool -> assetRetention -> MIMEType -> ByteString -> App Response
uploadAssetV3 user
user Bool
isPublic assetRetention
retention MIMEType
mimeType ByteString
bdy = do
String
uid <- user
user user -> (user -> App String) -> App String
forall a b. a -> (a -> b) -> b
& user -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
objId
Request
req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest user
user Service
Cargohold (Int -> Versioned
ExplicitVersion Int
1) String
"/assets/v3"
RequestBody
body <- Bool -> assetRetention -> ByteString -> MIMEType -> App RequestBody
forall assetRetention.
(HasCallStack, MakesValue assetRetention) =>
Bool -> assetRetention -> ByteString -> MIMEType -> App RequestBody
buildUploadAssetRequestBody Bool
isPublic assetRetention
retention ByteString
bdy MIMEType
mimeType
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
zUser String
uid
Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& RequestBody -> String -> Request -> Request
addBody RequestBody
body String
multipartMixedMime
uploadAsset :: (HasCallStack, MakesValue user) => user -> App Response
uploadAsset :: forall asset.
(HasCallStack, MakesValue asset) =>
asset -> App Response
uploadAsset = (user -> String -> App Response) -> String -> user -> App Response
forall a b c. (a -> b -> c) -> b -> a -> c
flip user -> String -> App Response
forall user.
(HasCallStack, MakesValue user) =>
user -> String -> App Response
uploadFreshAsset String
"Hello World!"
uploadProviderAsset :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
uploadProviderAsset :: forall domain.
(HasCallStack, MakesValue domain) =>
domain -> String -> String -> App Response
uploadProviderAsset domain
domain String
pid 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
Cargohold 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
zProvider String
pid
Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> Request -> Request
zType String
"provider"
Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& RequestBody -> String -> Request -> Request
addBody RequestBody
bdy String
multipartMixedMime
uploadFreshAsset :: (HasCallStack, MakesValue user) => user -> String -> App Response
uploadFreshAsset :: forall user.
(HasCallStack, MakesValue user) =>
user -> String -> App Response
uploadFreshAsset user
user String
payload = do
String
uid <- user
user user -> (user -> App String) -> App String
forall a b. a -> (a -> b) -> b
& user -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
objId
Request
req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest user
user Service
Cargohold Versioned
Versioned 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
zUser String
uid
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"
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 ->
LByteString ->
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
class IsAssetLocation key where
locationPathFragment :: key -> App String
instance {-# OVERLAPS #-} IsAssetLocation String where
locationPathFragment :: String -> App String
locationPathFragment = String -> App String
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
instance (MakesValue loc) => IsAssetLocation loc where
locationPathFragment :: loc -> App String
locationPathFragment loc
v =
App String
qualifiedFrag App String -> (SomeException -> App String) -> App String
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> (e -> m a) -> m a
`catch` (\(SomeException
_e :: SomeException) -> App String
unqualifiedFrag)
where
qualifiedFrag :: App String
qualifiedFrag = do
String
domain <- loc
v loc -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"domain" 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
key <- loc
v loc -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"key" 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 -> App String
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> App String) -> String -> App String
forall a b. (a -> b) -> a -> b
$ String
"v2/assets/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
domain String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
key
unqualifiedFrag :: App String
unqualifiedFrag = do
String
key <- loc -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString loc
v
String -> App String
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> App String) -> String -> App String
forall a b. (a -> b) -> a -> b
$ String
"v1/asssets/v3/" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
key
noRedirect :: Request -> Request
noRedirect :: Request -> Request
noRedirect Request
r = Request
r {redirectCount = 0}
downloadAsset' ::
(HasCallStack, MakesValue user, IsAssetLocation loc, IsAssetToken tok) =>
user ->
loc ->
tok ->
App Response
downloadAsset' :: forall user loc tok.
(HasCallStack, MakesValue user, IsAssetLocation loc,
IsAssetToken tok) =>
user -> loc -> tok -> App Response
downloadAsset' user
user loc
loc tok
tok = do
String
locPath <- loc -> App String
forall key. IsAssetLocation key => key -> App String
locationPathFragment loc
loc
Request
req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest user
user Service
Cargohold Versioned
Unversioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ String
locPath
String -> Request -> App Response
submit String
"GET" (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
& tok -> Request -> Request
forall tok. IsAssetToken tok => tok -> Request -> Request
tokenParam tok
tok Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& Request -> Request
noRedirect
downloadAsset ::
(HasCallStack, MakesValue user, MakesValue key, MakesValue assetDomain) =>
user ->
assetDomain ->
key ->
String ->
(HTTP.Request -> HTTP.Request) ->
App Response
downloadAsset :: forall user key assetDomain.
(HasCallStack, MakesValue user, MakesValue key,
MakesValue assetDomain) =>
user
-> assetDomain
-> key
-> String
-> (Request -> Request)
-> App Response
downloadAsset user
user assetDomain
assetDomain key
key String
zHostHeader Request -> Request
trans = do
String
domain <- assetDomain -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
objDomain assetDomain
assetDomain
String
key' <- key -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString key
key
Request
req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest user
user Service
Cargohold Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ String
"/assets/" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
domain String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
key'
String -> Request -> App Response
submit String
"GET"
(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
zHost String
zHostHeader
Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& Request -> Request
trans