Skip to content

Commit

Permalink
Add tags & decoder. Add tags to getObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
jans-fp committed Mar 19, 2021
1 parent ff22553 commit bdc98f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, dependencies =
[ "aff-promise"
, "argonaut"
, "argonaut-codecs"
, "console"
, "datetime"
, "effect"
Expand Down
15 changes: 14 additions & 1 deletion src/AWS/Core/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ module AWS.Core.Types where

import Data.Maybe (Maybe)
import Data.Newtype (class Newtype)
import Prelude (class Show)
import Prelude (class Show, bind, ($), pure, (>>>), map)
import Data.Argonaut.Encode (class EncodeJson)
import Data.Argonaut (class DecodeJson, decodeJson)
import Foreign.Object as F
import Data.Map as Map

newtype AccessKeyId
= AccessKeyId String
Expand Down Expand Up @@ -75,6 +78,16 @@ derive newtype instance showEndpoint :: Show Endpoint

derive newtype instance encodeEndpoint :: EncodeJson Endpoint

newtype Tags = Tags (Map.Map String String)
derive newtype instance showTags :: Show Tags

instance tagsDecoder :: DecodeJson Tags where
decodeJson = decodeAsMap >>> map Tags
where
decodeAsMap str = do
obj <- decodeJson str
pure $ Map.fromFoldable $ (F.toUnfoldable obj :: Array _)

type BasicClientPropsR r
= ( accessKeyId :: Maybe AccessKeyId
, secretAccessKey :: Maybe SecretAccessKey
Expand Down
36 changes: 24 additions & 12 deletions src/AWS/S3/S3.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ module AWS.S3
) where

import AWS.Core.Client (makeClientHelper)
import AWS.Core.Types (DefaultClientProps, Region(..))
import AWS.Core.Types (DefaultClientProps, Region(..), Tags)
import AWS.Core.Util (raiseEither)
import Control.Bind ((>>=))
import Control.Promise (Promise, toAffE)
import Data.Argonaut (Json)
import Data.Argonaut (Json, JsonDecodeError, decodeJson)
import Data.Bifunctor (lmap)
import Data.Either (Either)
import Data.Function.Uncurried (Fn2, runFn2, Fn3, runFn3)
import Data.Int (round)
import Data.Newtype (class Newtype)
import Data.Show (show)
import Data.Time.Duration (Seconds(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Justifill (justifillVia)
import Justifill.Fillable (class Fillable)
import Justifill.Justifiable (class Justifiable)
import Node.Buffer (Buffer)
import Prelude (Unit, ($), class Show, (#), (<#>))
import Prelude (class Show, Unit, (#), ($), (<#>), (>>>))
import Type.Proxy (Proxy(..))

foreign import data S3 :: Type
Expand Down Expand Up @@ -69,6 +74,7 @@ type InternalGetObjectResponse
, "ContentLength" :: Int
, "ContentEncoding" :: String
, "ContentType" :: String
, "Metadata" :: Json
}

foreign import getObjectImpl :: Fn2 S3 InternalGetObjectParams (Effect (Promise InternalGetObjectResponse))
Expand All @@ -85,29 +91,35 @@ type GetObjectParams

type GetObjectResponse
= { body :: Buffer
, contentLenght :: Int
, contentLength :: Int
, contentEncoding :: String
, contentType :: String
, tags :: Tags
}

getObject :: S3 -> GetObjectParams -> Aff GetObjectResponse
getObject client { bucket: BucketName name, key: BucketKey key } =
runFn2 getObjectImpl client params
# toAffE
<#> convert
>>= (convert >>> lmap show >>> raiseEither)
where
params =
{ "Bucket": name
, "Key": key
}

convert :: InternalGetObjectResponse -> GetObjectResponse
convert internalResponse =
{ body: internalResponse."Body"
, contentLenght: internalResponse."ContentLength"
, contentEncoding: internalResponse."ContentEncoding"
, contentType: internalResponse."ContentType"
}
convert :: InternalGetObjectResponse -> Either JsonDecodeError GetObjectResponse
convert internalResponse = parse internalResponse."Metadata" <#> addTags
where
addTags tags = { body: internalResponse."Body"
, contentLength: internalResponse."ContentLength"
, contentEncoding: internalResponse."ContentEncoding"
, contentType: internalResponse."ContentType"
, tags : tags
}
parse :: Json -> Either JsonDecodeError Tags
parse = decodeJson


type InternalGetSignedUrlParams
= { "Bucket" :: String
Expand Down

0 comments on commit bdc98f9

Please sign in to comment.