Created
December 6, 2017 16:00
-
-
Save jabaraster/9fa1a524155c8fa6934282d48c300e46 to your computer and use it in GitHub Desktop.
amazonka-dynamodbの使い方を調査. ローカルのDynamoDBに接続できるようになった。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
module Lib where | |
import Control.Lens | |
import Data.Aeson | |
import Data.Aeson.Lens | |
import Data.HashMap.Strict as M | |
import Data.Maybe | |
import Data.Monoid | |
import Data.Text | |
import Data.UUID | |
import Data.UUID.V4 | |
import Network.AWS | |
import Network.AWS.DynamoDB | |
import Network.AWS.DynamoDB.Types | |
runMain = do | |
e <- env | |
runPutItem e | |
scanRes <- runScan e | |
mapM_ (\item -> do | |
print "----------" | |
putStrLn $ unpack $ fromMaybe "**Nothing**" $ (toJSON item) ^? key "requestId" . key "S" . _String | |
putStrLn $ show $ fromMaybe M.empty $ (toJSON item) ^? key "payload" . key "M" . _Object | |
putStrLn $ unpack $ fromMaybe "**Nothing**" $ processItem item | |
) $ scanRes^.srsItems | |
processItem item = (toJSON item) ^? key "payload" . key "M" . key "parameters" . key "M" . key "conversation" . key "S" . _String | |
runScan :: Env -> IO ScanResponse | |
runScan e = run e $ scan "some-table" | |
runPutItem :: Env -> IO PutItemResponse | |
runPutItem e = do | |
ri <- nextRandom | |
let payload = M.fromList [ | |
("parameters", attributeValue&avM .~ M.fromList[("conversation", attributeValue&avS .~ Just "会話内容です。")]) | |
] | |
item = M.fromList [ | |
("requestId", attributeValue&avS .~ (Just $ toText ri)) | |
, ("timestamp", attributeValue&avS .~ Just "2017-12-07 00:00:00") | |
, ("meetingId", attributeValue&avS .~ Just "100") | |
, ("payload" , attributeValue&avM .~ payload) | |
] | |
run e $ (putItem "some-table")&piItem .~ item | |
{-- FlexibleContexts for this function --} | |
run e = runResourceT . runAWS e . send | |
env :: IO Env | |
env = do | |
e <- newEnv $ FromEnv "AWS_ACCESS_KEY" "AWS_SECRET_ACCESS_KEY" Nothing Nothing | |
pure $ e&envRegion .~ Tokyo &envOverride .~ localOverride | |
localOverride :: Dual (Endo Service) | |
localOverride = Dual $ Endo localService | |
localService :: Service -> Service | |
localService svc = svc { _svcEndpoint = \_ -> localEndpoint } | |
localEndpoint :: Endpoint | |
localEndpoint = Endpoint { | |
_endpointHost = "localhost" | |
, _endpointSecure = False | |
, _endpointPort = 8000 | |
, _endpointScope = "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment