brendanhay / amazonka

A comprehensive Amazon Web Services SDK for Haskell.
https://amazonka.brendanhay.nz
Other
605 stars 226 forks source link

v2.0 MonadResource - what is recommened approach to reuse Session #827

Closed EdmundsEcho closed 2 years ago

EdmundsEcho commented 2 years ago

Being new to the sdk (not just v2), I'm trying to piece together the recommended approach to send multiple operations over time, using the same Session.

The receiveJSON in the Response.hs module looked promising namesake. However, there is a comment in the Response.hs "Functions contained in this module fully consume the body and thus close -- the connection."

...which raises the question

What is the recommended way to maximize the reuse of a connection? I have a single server -> server, single purpose (getObject), where only the object key is expected to change between requests.

Given the currently planned approach

Apply S3.send to the operation object...

getJSONFromBucket :: WithAppContext m => S3.BucketName -> S3.ObjectKey -> m B.ByteString
getJSONFromBucket bucket path = do
    ... ask for env
    response :: S3.GetObjectResponse <- S3.send s3env' $ S3.newGetObject bucket path
    ...
    pure $ DeserializedInstanceOfMyData

What is the recommended approach to deconstructing the response, consume the json body to instantiate my local data type whilst minimizing the need to reconnect between requests? is the 2017 blog post that uses (result ^. gorsBody)sinkBodysinkLbs still relevant in v2.0?

My AWS Env ```haskell ... import qualified Amazonka as S3 import qualified Amazonka.S3 as S3 mkAppEnv :: MonadIO m => TVar Database -> Config -> m Env mkAppEnv db cfg = do logger <- S3.newLogger S3.Debug stdout let s3cfg = fileShareCfg cfg -- region' = region s3cfg manually set to "US" s3id = case S3.fromText $ accessId s3cfg of Left e -> panic ("Failed s3key: " <> pack e) Right r -> r s3secret = case S3.fromText $ secret s3cfg of Left e -> panic ("Failed secret: " <> pack e) Right r -> r credentials :: S3.Env <- S3.newEnv (S3.FromKeys s3id s3secret) let s3env' = credentials { S3._envLogger = logger , S3._envRegion = "US" } s3env'' = S3.configure (endpointInService cfg) s3env' pure $ Env { database = db , config = cfg , s3env = s3env'' } ```
endgame commented 2 years ago

You should be using the high-level functions exported from Amazonka in package amazonka; the functions receiveJson are used to implement the different service bindings (depending on whether the AWS API returns XML, JSON, or whatever else).

According to the haddock at the top of Amazonka.Response, connections are closed to stop them leaking; see #490.

gorsBody doesn't exist any more, that field has been renamed to body as part of a general cleanup of generated service names.

Note that if you use the same Env between requests, it will reuse the same http-client Manager, so you should get some kind of reuse from there.