Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (50 sloc) 2.04 KB
module Y2018.M01.D16.Solution where
Now that we have packet information, ... well, the funny thing is, we've always
had packet information, it's just that I'm looking at continuation and error-
recovery, but that's down the road, not for today ...
So, now that we have packet information, let's do the following.
From a packet p0, and a REST endpoint r, load in the NEXT packet of articles.
How do we do this?
The rest endpoint:
import Data.Aeson (decode)
import Data.ByteString.Lazy.Char8 (ByteString)
import qualified Data.ByteString.Lazy.Char8 as BL
import Network.HTTP.Conduit
-- below import available via 1HaskellADay git repository
import Y2017.M12.D20.Solution (Packet,next,rows)
endpoint :: FilePath
endpoint = ""
Those are a lot of arguments, but we don't need to worry about most of them.
The one that concerns us is the argument 'o' which stands for 'offset.'
For us, the offset is either 0 if we're starting with a new archive or
the offset is from the most recent, or only active audit log entry:
For now just pick an offset so we can load in a packet:
readPacket :: Integer -> IO (Either Packet String)
here is the plain-jain simpleHttp answer to the exercise:
readPacket offset = simpleHttp (endpoint ++ show offset) >>= return . decode
-- here's readPacket with a timeout:
readPacket = rp' 60
rp' :: Int -> Integer -> IO (Either Packet String)
rp' secs offset =
newManager tlsManagerSettings >>= \mgr ->
parseRequest (endpoint ++ show offset) >>= \req ->
let req' = req { responseTimeout = responseTimeoutMicro (secs * 1000000) } in
httpLbs req' mgr >>= return . eitherify decode . responseBody
eitherify :: (ByteString -> Maybe Packet) -> ByteString -> Either Packet String
eitherify f str = case f str of
Just pack -> Left pack
Nothing -> Right (BL.unpack str)
-- What is the value of next for the packet read? How many blocks were read?
>>> Just pac <- readPacket 100
>>> next pac
>>> length (rows pac)
You can’t perform that action at this time.