I am fetching this JSON use it for some computations and want to return a result, using warp as well. Now getJSON is obviously not mutable and won't change at all. How can I re-evaluate getJSON on every http request? What is the cleanest way?
getJSON :: IO B.ByteString
getJSON = do
let authHeader = ("Authorization", token)
request' <- parseRequest jsonURL
let request = request' { requestHeaders = [authHeader] }
manager <- newManager tlsManagerSettings
resp <- httpLbs request manager
return $ responseBody resp
To elaborate on @Willem Van Onsem's point...
Even though the value of
getJSONis immutable, that immutable value is not a bytestring. It's an immutable IO action that returns a bytestring, but that doesn't imply that it returns the same bytestring every time. The action is an immutable constant, but that doesn't mean the bytestring is also a constant.This is similar to the way that:
while itself an immutable value (an immutable IO action), can return different strings each time the action is executed.
For example, the following self-contained program will return two different JSON bytestrings,
oneandtworesulting from two HTTP requests, both initiated by the samegetJSONaction: