@@ -15,7 +15,7 @@ import Control.Monad.Catch (Exception, MonadThrow, throwM)
15import Data.List
15import Data.List
16import Data.Typeable
16import Data.Typeable
17import GHC.Generics
17import GHC.Generics
18−import Network.URI
18+import Network.URI hiding (path)
19import Safe
19import Safe
20import Text.Read
20import Text.Read
@@ -31,11 +31,12 @@ data BaseUrl = BaseUrl
31 { baseUrlScheme :: Scheme -- ^ URI scheme to use
31 { baseUrlScheme :: Scheme -- ^ URI scheme to use
32 , baseUrlHost :: String -- ^ host (eg "haskell.org")
32 , baseUrlHost :: String -- ^ host (eg "haskell.org")
33 , baseUrlPort :: Int -- ^ port (eg 80)
33 , baseUrlPort :: Int -- ^ port (eg 80)
34+ , baseUrlPath :: String -- ^ path (eg "/a/b/c")
34 } deriving (Show, Eq, Ord, Generic)
35 } deriving (Show, Eq, Ord, Generic)
36showBaseUrl :: BaseUrl -> String
37showBaseUrl :: BaseUrl -> String
37−showBaseUrl (BaseUrl urlscheme host port) =
38+showBaseUrl (BaseUrl urlscheme host port path) =
38− schemeString ++ "//" ++ host ++ portString
39+ schemeString ++ "//" ++ host ++ portString ++ path
40 schemeString = case urlscheme of
41 schemeString = case urlscheme of
41 Http -> "http:"
42 Http -> "http:"
@@ -52,14 +53,14 @@ parseBaseUrl :: MonadThrow m => String -> m BaseUrl
52parseBaseUrl s = case parseURI (removeTrailingSlash s) of
53parseBaseUrl s = case parseURI (removeTrailingSlash s) of
53 -- This is a rather hacky implementation and should be replaced with something
54 -- This is a rather hacky implementation and should be replaced with something
54 -- implemented in attoparsec (which is already a dependency anyhow (via aeson)).
55 -- implemented in attoparsec (which is already a dependency anyhow (via aeson)).
55− Just (URI "http:" (Just (URIAuth "" host (':' : (readMaybe -> Just port)))) "" "" "") ->
56+ Just (URI "http:" (Just (URIAuth "" host (':' : (readMaybe -> Just port)))) path "" "") ->
56− return (BaseUrl Http host port)
57+ return (BaseUrl Http host port path)
57− Just (URI "http:" (Just (URIAuth "" host "")) "" "" "") ->
58+ Just (URI "http:" (Just (URIAuth "" host "")) path "" "") ->
58− return (BaseUrl Http host 80)
59+ return (BaseUrl Http host 80 path)
59− Just (URI "https:" (Just (URIAuth "" host (':' : (readMaybe -> Just port)))) "" "" "") ->
60+ Just (URI "https:" (Just (URIAuth "" host (':' : (readMaybe -> Just port)))) path "" "") ->
60− return (BaseUrl Https host port)
61+ return (BaseUrl Https host port path)
61− Just (URI "https:" (Just (URIAuth "" host "")) "" "" "") ->
62+ Just (URI "https:" (Just (URIAuth "" host "")) path "" "") ->
62− return (BaseUrl Https host 443)
63+ return (BaseUrl Https host 443 path)
63 _ -> if "://" `isInfixOf` s
64 _ -> if "://" `isInfixOf` s
64 then throwM (InvalidBaseUrlException $ "Invalid base URL: " ++ s)
65 then throwM (InvalidBaseUrlException $ "Invalid base URL: " ++ s)
65 else parseBaseUrl ("http://" ++ s)
66 else parseBaseUrl ("http://" ++ s)