-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP.hs
50 lines (46 loc) · 1.61 KB
/
HTTP.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE UndecidableInstances #-}
module HTTP where
import Control.Lens (view)
import Control.Monad.IO.Class (liftIO)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BSL
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Time (UTCTime, addUTCTime, getCurrentTime,
secondsToNominalDiffTime)
import qualified Database.Persist as Persist
import Database.Persist.Sqlite (runMigration, runSqlite)
import Database.Persist.TH (mkMigrate, mkPersist, persistLowerCase,
share, sqlSettings)
import qualified Network.Wreq as Wreq
share
[mkPersist sqlSettings, mkMigrate "migrateAll"]
[persistLowerCase|
Cache
Id Text
responseBody ByteString
updated UTCTime
|]
getCached :: Text -> IO ByteString
getCached url =
runSqlite cacheFile do
runMigration migrateAll
cached <- Persist.get (CacheKey url)
now <- liftIO getCurrentTime
case cached of
Just Cache{cacheResponseBody, cacheUpdated}
| addUTCTime timeout cacheUpdated > now ->
pure cacheResponseBody
_ -> do
responseBody <-
liftIO $
BSL.toStrict . view Wreq.responseBody <$> Wreq.get (Text.unpack url)
Persist.repsert
(CacheKey url)
Cache{cacheResponseBody = responseBody, cacheUpdated = now}
pure responseBody
where
cacheFile = "http_cache.sqlite"
timeout = secondsToNominalDiffTime 60