1{-# OPTIONS_GHC -Wall #-}
3{-# LANGUAGE OverloadedStrings #-}
4module Reporting.Task.Http
17import Control.Concurrent (forkIO)
18import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
19import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, readMVar)
20import Control.Exception (Exception, SomeException, catch)
21import Control.Monad (forever, join, replicateM_, void)
22import qualified Network.HTTP as Http (urlEncodeVars)
23import qualified Network.HTTP.Client as Http
25import qualified Elm.Compiler as Compiler
26import qualified Elm.Package as Pkg
27import qualified Reporting.Error as Error
28import qualified Reporting.Error.Http as E
29import qualified Reporting.Progress as Progress
30import qualified Reporting.Task as Task
38 Package :: String -> [(String,String)] -> Handler a -> Fetch a
39 Anything :: String -> Handler a -> Fetch a
40 AndThen :: Fetch a -> (a -> Fetch b) -> Fetch b
41 Parallel :: [Fetch a] -> Fetch [a]
42 Report :: Progress.Progress -> (Progress.Outcome -> Progress.Progress) -> Fetch a -> Fetch a
46 Http.Request -> Http.Manager -> IO (Either E.Error a)
49package :: String -> [(String,String)] -> Handler a -> Fetch a
54anything :: String -> Handler a -> Fetch a
59andThen :: Fetch a -> (a -> Fetch b) -> Fetch b
64parallel :: [Fetch a] -> Fetch [a]
69report :: Progress.Progress -> (Progress.Outcome -> Progress.Progress) -> Fetch a -> Fetch a
78run :: Fetch a -> Task.Task a
80 Task.runHttp $ \manager tell ->
82 replicateM_ 4 $ forkIO $ forever $ join (readChan chan)
83 readMVar =<< runHelp chan manager tell fetch
86runHelp :: Chan (IO ()) -> Http.Manager -> (Progress.Progress -> IO ()) -> Fetch a -> IO (MVar (Either Error.Error a))
87runHelp chan manager tell fetch =
89 Package path params handler ->
90 do mvar <- newEmptyMVar
91 let url = makePackageUrl path params
92 writeChan chan $ putMVar mvar =<< fetchSafe url manager handler
95 Anything url handler ->
96 do mvar <- newEmptyMVar
97 writeChan chan $ putMVar mvar =<< fetchSafe url manager handler
100 AndThen subFetch callback ->
101 do subMVar <- runHelp chan manager tell subFetch
104 do result <- readMVar subMVar
110 readMVar =<< runHelp chan manager tell (callback value)
114 do mvars <- mapM (runHelp chan manager tell) fetches
117 do results <- mapM readMVar mvars
118 putMVar mvar (sequence results)
121 Report start toEnd subFetch ->
123 subMVar <- runHelp chan manager tell subFetch
126 do result <- readMVar subMVar
128 either (\_ -> Progress.Bad) (\_ -> Progress.Good) result
137packageDomain :: String
139 "http://localhost:8000"
142makePackageUrl :: String -> [(String,String)] -> String
143makePackageUrl path params =
149 "?" ++ Http.urlEncodeVars (versionParam : params)
151 packageDomain ++ "/" ++ path ++ query
154versionParam :: (String, String)
156 ( "elm-package-version"
157 , Pkg.versionToString Compiler.version
165fetchSafe :: String -> Http.Manager -> Handler a -> IO (Either Error.Error a)
166fetchSafe url manager handler =
167 fetchUnsafe url manager handler
168 `catch` \e -> handleAnyError url (e :: SomeException)
171fetchUnsafe :: String -> Http.Manager -> Handler a -> IO (Either Error.Error a)
172fetchUnsafe url manager handler =
173 do request <- Http.parseUrlThrow url
174 result <- handler request manager
180 return (Left (Error.BadHttp url problem))
183handleAnyError :: (Exception e) => String -> e -> IO (Either Error.Error a)
184handleAnyError url exception =
185 return $ Left $ Error.BadHttp url $ E.Unknown $ show exception
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list