hzulip 0.1.0.1 → 0.2.0.0
raw patch · 2 files changed
+14/−2 lines, 2 files
Files
- hzulip.cabal +1/−1
- src/HZulip.hs +13/−1
hzulip.cabal view
@@ -1,5 +1,5 @@ name: hzulip-version: 0.1.0.1+version: 0.2.0.0 synopsis: A haskell wrapper for the Zulip API. description: This a Zulip API wrapper for Haskell. homepage: https://github.com/yamadapc/hzulip
src/HZulip.hs view
@@ -4,12 +4,14 @@ , defaultBaseUrl , getEvents , newZulip+ , onNewEvent , registerQueue , sendMessage ) where import Control.Lens ((.~), (&), (^.))+import Control.Monad (forever) import qualified Data.ByteString.Char8 as BS (pack) import qualified Data.Text as T (pack) import Network.Wreq@@ -75,6 +77,8 @@ return $ Queue qid lid else fail $ responseMsg body +-- |+-- Fetches new set of events from a `Queue`. getEvents :: ZulipClient -> Queue -> Bool -> IO [Event] getEvents z q b = do let opts = (reqOptions z) { WT.params = [ ("queue_id", T.pack $ queueId q)@@ -83,7 +87,7 @@ , ("dont_block", if b then "true" else "false") ]- }+ } r <- getWith opts (eventsUrl z) >>= asJSON let body = r ^. responseBody@@ -91,6 +95,14 @@ if wasSuccessful body then let Just evs = responseEvents body in return evs else fail $ responseMsg body++-- |+-- Registers an event callback for all events and keeps executing it over+-- events as they come in. Will loop forever+onNewEvent :: ZulipClient -> Bool -> EventCallback -> IO ()+onNewEvent z b f = do+ q <- registerQueue z [] b+ forever $ getEvents z q b >>= mapM_ f -- Private functions: -------------------------------------------------------------------------------