By | fr33domlover |
At | 2015-09-19 |
Title | initial code |
Description |
Edit file funbot-client.cabal 33188 → 33188
26 26 library
27 27 exposed-modules: FunBot.Client
28 28 -- other-modules:
29 29 -- other-extensions:
30 30 build-depends: aeson
- 31 , aeson-pretty
- 32 , base >=4.7 && <5
+ 31 , aeson-pretty >=0.7
+ 32 , base >=4.7 && <5
+ 33 , bytestring
+ 34 , funbot-ext-events
+ 35 , HTTP >=4000.2
+ 36 , network-uri
33 37 hs-source-dirs: src
34 38 default-language: Haskell2010
35 39 ghc-options: -Wall
… … … … Edit file src/FunBot/Client.hs 33188 → 33188
11 11 - You should have received a copy of the CC0 Public Domain Dedication along
12 12 - with this software. If not, see
13 13 - <http://creativecommons.org/publicdomain/zero/1.0/>.
14 14 -}
15 15 + 16 -- | TODO
16 17 module FunBot.Client
- 17 (
+ 18 ( Bot ()
+ 19 , ExtEvent ()
+ 20 , mkBot
+ 21 , sendExtEvent
+ 22 , mkPushEvent
+ 23 , mkTagEvent
+ 24 , mkMergeRequestEvent
+ 25 , mkNewsEvent
+ 26 , mkPasteEvent
18 27 )
19 28 where
+ 29 + 30 import Data.Aeson (encode)
+ 31 import Data.Aeson.Encode.Pretty (encodePretty)
+ 32 import FunBot.ExtEvents
+ 33 import Network.HTTP
+ 34 import Network.URI (URI)
+ 35 + 36 import qualified Data.ByteString.Lazy as B
+ 37 + 38 -- | Reference to running FunBot instance to which you can send events.
+ 39 newtype Bot = Bot
+ 40 { botMkRequest :: ExtEvent -> Request B.ByteString
+ 41 }
+ 42 + 43 -- | Create a reference to a FunBot instance.
+ 44 --
+ 45 -- You can parse the URL at runtime using 'Network.URI.parseURI' or at compile
+ 46 -- time using 'Network.URI.Static.staticURI' (or the @uri@ quasi quoter in the
+ 47 -- same package).
+ 48 mkBot :: URI -- ^ URL at which the bot listens to client events.
+ 49 -> Bool -- ^ Whether JSON data should be sent in pretty form, i.e.
+ 50 -- indented. This may be useful for debugging, but otherwise you
+ 51 -- should probably pass 'False' here.
+ 52 -> Bot
+ 53 mkBot uri pretty =
+ 54 let f event =
+ 55 let body = if pretty then encodePretty event else encode event
+ 56 in Request
+ 57 { rqURI = uri
+ 58 , rqBody = body
+ 59 , rqHeaders =
+ 60 [ Header HdrContentType "application/json"
+ 61 , Header HdrContentLength (show $ B.length body)
+ 62 ]
+ 63 , rqMethod = POST
+ 64 }
+ 65 in Bot f
+ 66 + 67 -- | Send an event to a bot over HTTP.
+ 68 -- TODO check and report errors
+ 69 sendExtEvent :: Bot -> ExtEvent -> IO ()
+ 70 sendExtEvent bot event = do
+ 71 result <- simpleHTTP $ botMkRequest bot event
+ 72 case result of
+ 73 Left err -> error $ show err
+ 74 Right _ -> return ()
+ 75 + 76 -- | Construct a git push event.
+ 77 mkPushEvent :: Push -> ExtEvent
+ 78 mkPushEvent = GitPushEvent
+ 79 + 80 -- | Construct a git tag event.
+ 81 mkTagEvent :: Tag -> ExtEvent
+ 82 mkTagEvent = GitTagEvent
+ 83 + 84 -- | Construct a merge request event.
+ 85 mkMergeRequestEvent :: MergeRequest -> ExtEvent
+ 86 mkMergeRequestEvent = MergeRequestEvent
+ 87 + 88 -- | Construct a news item event.
+ 89 mkNewsEvent :: NewsItem -> ExtEvent
+ 90 mkNewsEvent = NewsEvent
+ 91 + 92 -- | Construct a paste event.
+ 93 mkPasteEvent :: Paste -> ExtEvent
+ 94 mkPasteEvent = PasteEvent
… … … …